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.

Replies

  • c.deepak257
    c.deepak257
    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)
  • Kaustubh Katdare
    Kaustubh Katdare
    I'd not ask if I didn't know.
  • c.deepak257
    c.deepak257
    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.
  • sriramchandrk
    sriramchandrk
    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

    #include 
    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
  • OrAnGeWorX
    OrAnGeWorX
    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
    // 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;
                            }
                    }
              }
    
    
  • Ashutosh_shukla
    Ashutosh_shukla
    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 :

    #include
    #include
    #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>a[i];
     cout<<"The perfect nos are : \n";
     for(i=0;i
                                        
  • Raviteja.g
    Raviteja.g
    what about powerful number?
    can you design the program which generates powerful number less than 100
  • OrAnGeWorX
    OrAnGeWorX
    Ashutosh_shukla
    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 :

    #include
    #include
    #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>a[i];
     cout<<"The perfect nos are : \n";
     for(i=0;i
    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..
  • Ashutosh_shukla
    Ashutosh_shukla
    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 :
    #include
    #include
    int check_prime(int n)
    {
     for(int j=2;ji)
       cout<
                                        
  • Kaustubh Katdare
    Kaustubh Katdare
    Everyone - please enclose the code in
     ... [ /code ] tags
  • OrAnGeWorX
    OrAnGeWorX
    ok here's what i got so far but i have an error problem... line 41

    #include 
    #include 
    
    main()
    {
       // initialisation des variables
       int nbrMax=0;                        // Nbr d'entiers à saisir
       int produit=0;
       int tableauNbr[nbrMax];            // Tableau pour contenir les nombres entrés par l'usager
       int i, j, k;                       // Compteur boucle
       int somme;                   // variables pour calcul nbr parfait
       char condition;                    // Oui ou Non
    
       // Boucle do ... while, pour ne pas devoir repartir le programme pour chaque calcul
       do
          {
          printf ("Entrez le nombre d'entier (Max 10):\n");
          scanf ("%d", &nbrMax);
    
          // boucle pour la saisie des nombres et remplissage le tableau.
          for (i = 0; i < nbrMax; i++)
              {
              scanf ("%d", &tableauNbr[i]);
              }
    
          // Calculs pour trouver les nombres parfaits
          somme = 0;
    // after edit
          produit = tableauNbr[0];   // for multiplication by 0 later.
    
          for (j = 0; j < nbrMax; j++)            {
    
                for (k = 1; k < tableauNbr[j]; k++)
                    {
                        if(!tableauNbr[j]%k)
                            somme += k;
                    }
                    // Affichage des resultats
                        if (somme == tableauNbr[j])
                            printf("%d est un nombre parfait\n", tableauNbr[j]);
                            else {
                            printf("%d n'est pas un nombre parfait\n", tableauNbr[j]);
                            produit *= tableauNbr[j];        // tableauNbr undeclared, first use in this function error...
                            }
            }    
          
    
          printf("le produit des nombres non-parfaits est: %d", produit);
    
          // Condition requise pour repartir du début
          printf ("\nVoulez vous faire un autre calcul, (o/n)?");
          fflush (stdin);                        // Fonction pour vider stdin
          condition = toupper(getchar());        // Capitaliser la letter et soumettre à condition
          }
       while ( condition == 'O');                // Repartir du début si O
    }
    
    edit: error on line 41 corrected... typo (silly me)
    but as the code ran, it's still not doing it's job
    i also added a line to initialize the product to the first element of the table so that the multiplication in the for loop works (0*9 = 0)
  • OrAnGeWorX
    OrAnGeWorX
    ok i've cleaned it up a little and fixed the calculations, the results are still wrong...
    there's gotta be something with the calculations section....

    here are some results

    Entrez le nombre d'entier (Max 10):
    5
    12
    6
    28
    1
    6
    12 n'est pas un nombre parfait
    6 n'est pas un nombre parfait
    28 n'est pas un nombre parfait -- it is
    1 n'est pas un nombre parfait
    6 n'est pas un nombre parfait --- is as well
    le produit des nombres non-parfaits est: 0
    Voulez vous faire un autre calcul, (o/n)?n
    Press any key to continue . . .
  • shack
    shack
    To generate perfect numbers:
    main()
    {
    int n,sum;
    sum=0;
    printf("enter the range");
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
    for(int j=1;j {
    if(i%j==0)
    sum=sum+j;
    }
    if(sum==i)
    printf("%d\n",i);
    sum=0;
    }
    getch();
    }
  • Minar
    Minar
    hi, can any one tell me whats wrong in my codes here..its not showing any output.😔

    #include
    #include
    int main(void)
    {
    int num,i,sum;
    sum=0;
    for(num=1;num<=100;num++)
    {
    for(i=1;i<=num-1;i++)
    {
    if(!(num%i))
    {
    sum +=i;
    }
    }
    if(sum==num)printf("%d",sum);

    }

    getch();
    return 0;
    }
  • YashSoni501
    YashSoni501
    I have made a QBasic Programme to find all the perfect numbers between 1 to 100.... as much you want
  • YashSoni501
    YashSoni501
    Also i would like to specify that I am not in computer science or any else i am in class 9
    Sorry for giving wrong information while sign up
  • mitta vignesh
    mitta vignesh
    #include

    int main()
    {
    int num,i,j,sum;
    printf("enter the limit\n");
    scanf("%d",&num);
    for(i=1;i<=num;i++)
    {
    sum=0;
    for(j=1;j {
    if(i%j==0)
    sum=sum+j;
    }

    if(sum==i)
    printf("\nperfect number:%d",sum);
    }
    }
  • mitta vignesh
    mitta vignesh
    how to bring c editor.
    can anyone help me.
  • mitta vignesh
    mitta vignesh
    #include 
    
    int main()
    {
        int num,i,j,sum;
        printf("enter the limit\n");
        scanf("%d",&num);
        for(i=1;i<=num;i++)
        {
            sum=0;
            for(j=1;j
    #include 
    
    int main()
    {
        int num,i,j,sum;
        printf("enter the limit\n");
        scanf("%d",&num);
        for(i=1;i<=num;i++)
        {
            sum=0;
            for(j=1;j
                                        
  • mitta vignesh
    mitta vignesh
    #include 
    
    int main()
    {
        int num,i,j,sum;
        printf("enter the limit\n");
        scanf("%d",&num);
        for(i=1;i<=num;i++)
        {
            sum=0;
            for(j=1;j
                                        
  • Abdul Hisham
    Abdul Hisham

    c++ program find all amstrong numbers below 1000?

You are reading an archived discussion.

Related Posts

I've heard that CAT (The common admission test) will be a CBT from 2009. Those who want to take CAT in 2009, how are you preparing?
I Need Help For Finding General Specification For The Galvanised Iron ( G. I. ) Pipes As Per I.s. Code 1239. Please Help Me................
hi everyone, New here and from Germany.. nice to mee you.
Some of you might already be aware of AMD's recent announcement of splitting into two different companies: one for R&D, and spinning off the manufacturing and fabrication division. I suppose...
hai evrybody, i jus got an expt to be done. The topic goes "FILE MGMT(UPLOAD/DOWNLOAD) USING JSP " so,if anybody could help me it would be great frnds Uhhhhh. to...