+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13

Write a program to display all perfect numbers between 1 - 100

This is a discussion on Write a program to display all perfect numbers between 1 - 100 within the Computer Science & IT Engineering forums, part of the CE : Technical Discussions category; Here's another programming question I've asked in interviews to CS/IT Toppers in interviews when they tell me they are interested ...

  1. #1
    Good Administrator The_Big_K's Avatar
    Join Date
    26th November 2005
    I'm a crazy: Electrical engineer

    Location
    Terra-Firma
    Posts
    12,889

    Wink Write a program to display all perfect numbers between 1 - 100

    Here's another programming question I've asked in interviews to CS/IT Toppers in interviews when they tell me they are interested in Maths & Programming :


    Write a program to display all perfect numbers between 1 - 100
    Computer Language Choice: Whatever, doesn't matter. I'm happy even if you can come up with an algorithm.
    PS: If you are interested in mathematics and don't know what Perfect Numbers are, you don't impress me.
    Founder & Administrator | CrazyEngineers® | admin{@}crazyengineers{dawt}com
    About CE | Small Talk | Official Blog 'VoiCE' | Advertise On CE
    The Big K's SuperBlogmt | Join CE! | Guide: How to post on CE

  2. #2
    CE - Newbie c.deepak257's Avatar
    Join Date
    3rd October 2008
    I'm a crazy: computer engineer

    Location
    Chennai
    Posts
    5

    Default Re: Write a program to display all perfect numbers between 1 - 100

    2^(n-1)(2^n -1)
    this means (2 raise to the power of (n-1)) into (2 raise to the power n)-1

    this is the euclid equation for calculating the perfect numbers. The first value of the n is 2 because n-1 should be greater than 1 and n is a prime number i.e. 2,3,5,7 and so on.
    use this formula to calculate the perfect numbers between 1 and any number.try yourself. This is the hint.

    (P.S.= ASSUMED THAT U KNOW WHAT IS PERFECT NUMBER)
    Last edited by c.deepak257; 9th October 2008 at 04:10 PM.

  3. #3
    Good Administrator The_Big_K's Avatar
    Join Date
    26th November 2005
    I'm a crazy: Electrical engineer

    Location
    Terra-Firma
    Posts
    12,889

    Default Re: Write a program to display all perfect numbers between 1 - 100

    I'd not ask if I didn't know.
    Founder & Administrator | CrazyEngineers® | admin{@}crazyengineers{dawt}com
    About CE | Small Talk | Official Blog 'VoiCE' | Advertise On CE
    The Big K's SuperBlogmt | Join CE! | Guide: How to post on CE

  4. #4
    CE - Newbie c.deepak257's Avatar
    Join Date
    3rd October 2008
    I'm a crazy: computer engineer

    Location
    Chennai
    Posts
    5

    Default Re: Write a program to display all perfect numbers between 1 - 100

    perfect number are those in which the whose divisors proper sum is equal to the number.for eg: 6 has 1,2,3 as the proper divisors.sum of 1,2, and 3 is 6.

  5. #5
    CE - Regular Member
    Join Date
    25th September 2008
    I'm a crazy: CSE engineer

    Posts
    65

    Thumbs up Re: Write a program to display all perfect numbers between 1 - 100

    Hi,

    This program finds all perfect numbers within 1 to 100

    I have hardoded n = 100!
    The inner for loop finds all factors for a number

    This is simple way any one would do, but if you think of speed, then you should use euclids formula.



    Thanks & Regards
    Sriram

    Code:
    #include <iostream>
    using namespace std;
    main()
    {
        int n = 100,sum = 0;
        cout << "Searching for perfect number less than 100..." << endl;
        for(int num = 1; num <= n; num++)
        {
        sum = 0;
        for(int i = 1; i < num; i++)
        {
           if(!(num%i))
           {
              sum+=i;
           }
        }
        if(sum == num)
            cout <<  num << " Is perfect number" << endl;
        }
    }
    
    Test run
    >./a.out
    Searching for perfect number less than 100...
    6 Is perfect number
    28 Is perfect number

  6. #6
    CE - Newbie
    Join Date
    17th November 2008
    I'm a crazy: Telecom engineer

    Posts
    9

    Default Re: Write a program to display all perfect numbers between 1 - 100

    it's funny i was asked this same question a week ago in C and it's been kinda haunting me... the truth of the matter is that i don't understand the maths behind it... i was never math savvy so when it gets to things like these i get confused really easy and my code ... let's not go there.. for now at least
    though the question had a different twist, user would enter a number, that creates a table with n amount of rows to be filled by the user with his choice numbers to be tested as perfect numbers, y/n, and the non-perfect numbers need to be multiplied together.

    i believe there's another way to do it with modulo (the perfect number calculation itself) but i'm sorta stuck...

    here it is
    Code:
    // loop to fill table with numbers to be checked.
          for (i = 0; i < nbrMax; i++)
              {
              scanf ("%d", &tableNbr[i]);
              }
    
          // calculations to find perfect numbers
          sum = 0;
          for (j = 0; j < nbrMax; j++)
              {
                for (k = 1; k < tableau[j]; k++)
                    {
                        if(tableau[j]%k==0)
                            {
                            sum += k;
                            }
                    }
              }
    

  7. #7
    CE - Regular Member Ashutosh_shukla's Avatar
    Join Date
    16th November 2008
    I'm a crazy: Computer Science & Engineering engineer

    Location
    India
    Posts
    75

    Default Re: Write a program to display all perfect numbers between 1 - 100

    Hi OrAnGeWorX what I could make out of your question is that user enters N numbers and you want the perfect numbers. You also want the product of non perfect numbers.I have tried something I hope you will go through it and reply me.
    The code is :

    Code:
    #include<iostream.h>
    #include<conio.h>
    #define MAXSIZE 100
    void main()
    {
     int a[MAXSIZE],n,i,j,sum;
     long int prod=1;
     clrscr();
     cout<<"Enter the number of elements to be checked : ";
     cin>>n;
     cout<<"Enter the elements : ";
     for(i=0;i<n;i++)
      cin>>a[i];
     cout<<"The perfect nos are : \n";
     for(i=0;i<n;i++)
     {
      sum=0;
      for(j=1;j<a[i];j++)
       if(a[i]%j==0)
        sum+=j;
      if(sum==a[i])
       cout<<a[i]<<endl;
      else
       prod*=a[i];
     }
     cout<<"The product of non perfect numbers is : "<<prod;
     getch();
    }
    
    Last edited by The_Big_K; 17th November 2008 at 08:12 PM. Reason: Adding [code] tags

  8. #8
    CE - Enthusiast Raviteja.g's Avatar
    Join Date
    13th August 2008
    I'm a crazy: computer science engineer

    Location
    India
    Posts
    190

    Default Re: Write a program to display all perfect numbers between 1 - 100

    what about powerful number?
    can you design the program which generates powerful number less than 100
    Innovation isn’t made by hard workers, but by lazier people trying to find easier ways to do the same!Crazy4Technology

  9. #9
    CE - Newbie
    Join Date
    17th November 2008
    I'm a crazy: Telecom engineer

    Posts
    9

    Default Re: Write a program to display all perfect numbers between 1 - 100

    Quote Originally Posted by Ashutosh_shukla View Post
    Hi OrAnGeWorX what I could make out of your question is that user enters N numbers and you want the perfect numbers. You also want the product of non perfect numbers.I have tried something I hope you will go through it and reply me.
    The code is :

    Code:
    #include<iostream.h>
    #include<conio.h>
    #define MAXSIZE 100
    void main()
    {
     int a[MAXSIZE],n,i,j,sum;
     long int prod=1;
     clrscr();
     cout<<"Enter the number of elements to be checked : ";
     cin>>n;
     cout<<"Enter the elements : ";
     for(i=0;i<n;i++)
      cin>>a[i];
     cout<<"The perfect nos are : \n";
     for(i=0;i<n;i++)
     {
      sum=0;
      for(j=1;j<a[i];j++)
       if(a[i]%j==0)
        sum+=j;
      if(sum==a[i])
       cout<<a[i]<<endl;
      else
       prod*=a[i];
     }
     cout<<"The product of non perfect numbers is : "<<prod;
     getch();
    }
    
    Ashutosh_shukla, thanks for replying.. i should rephrase myself as far as the problem and coding this in C shouldn't be problematic (i guess)

    The user enters N for number of integers he'd like to check if or not they are perfect.
    stage 1: how many numbers do u want to check?
    user enters up to 10 and hits enter
    stage 2: program now waits for user to input his n numbers that will be saved in a table.
    stage 3: program goes through table[0] to table[n-1] and calculates if integer in each position is perfect or not, display a message accordingly, table[n] = integer is / is not a perfect number and in that same loop calculate the product (multiplication) of the non perfect numbers
    finally to display that last number.

    Thanks in advance.
    Marc
    i'll try convertin this to C and see if that works.... having some problems with dev-c++, compiling is fine but when i execute, program is crashing with windows send report window..
    Last edited by OrAnGeWorX; 17th November 2008 at 10:31 PM.

  10. #10
    CE - Regular Member Ashutosh_shukla's Avatar
    Join Date
    16th November 2008
    I'm a crazy: Computer Science & Engineering engineer

    Location
    India
    Posts
    75

    Default Re: Write a program to display all perfect numbers upto 1000

    Hi I have got the definition of powerful no from wikipedia as follows:
    A powerful number is a positive integer m that for every prime number p dividing m, p^2 also divides m. Equivalently, a powerful number is the product of a square and a cube, that is, a number m of the form m = a^2 * b^3, where a and b are positive integers.

    The code that gives powerful nos less than 1000 is :
    Code:
    #include<iostream.h>
    #include<conio.h>
    int check_prime(int n)
    {
     for(int j=2;j<n;j++)
     {
      if(n%j==0)
      {
       return 0;
      }
     }
     return 1;
    }
    void main()
    {
     int i,j;
     cout<<"the powerful nos upto 100 are : \n";
     for(i=1;i<=1000;i++)
     {
      for(j=2;j<=i;j++)
       if(check_prime(j))
       {
        if((i%j==0 && i%(j*j)!=0)||(i%j!=0 && i%(j*j)==0))
         break;
       }
      if(j>i)
       cout<<i<<"\t";
     }
     getch();
    }
    
    Last edited by The_Big_K; 17th November 2008 at 11:17 PM.

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. Mouse programming in C
    By prabhat kumar in forum Computer Science & IT Engineering
    Replies: 9
    Last Post: 13th February 2010, 12:21 PM
  2. Representing number as sum of smaller numbers
    By uday.bidkar in forum Computer Science & IT Engineering
    Replies: 28
    Last Post: 20th July 2009, 09:28 PM
  3. Can you guess the numbers?
    By The_Big_K in forum CE - Quiz | Puzzles | Mathematics
    Replies: 120
    Last Post: 4th May 2009, 09:31 AM
  4. PC Tutorial: Parallel Port Interfacing Techniques - Part 2
    By xheavenlyx in forum Computer Science & IT Engineering
    Replies: 26
    Last Post: 11th January 2009, 02:01 AM
  5. How to write a Successful Blog, MUST READ...
    By friendster7 in forum Chillax : Chit-Chat
    Replies: 7
    Last Post: 14th April 2008, 09:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts