CrazyEngineers
  • 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
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • c.deepak257

    MemberOct 8, 2008

    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)
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorOct 8, 2008

    I'd not ask if I didn't know.
    Are you sure? This action cannot be undone.
    Cancel
  • c.deepak257

    MemberOct 8, 2008

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • sriramchandrk

    MemberOct 13, 2008

    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 <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
    Are you sure? This action cannot be undone.
    Cancel
  • OrAnGeWorX

    MemberNov 16, 2008

    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;
                            }
                    }
              }
    
    
    Are you sure? This action cannot be undone.
    Cancel
  • Ashutosh_shukla

    MemberNov 17, 2008

    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<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();
    }
    
    
    Are you sure? This action cannot be undone.
    Cancel
  • Raviteja.g

    MemberNov 17, 2008

    what about powerful number?
    can you design the program which generates powerful number less than 100
    Are you sure? This action cannot be undone.
    Cancel
  • OrAnGeWorX

    MemberNov 17, 2008

    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<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..
    Are you sure? This action cannot be undone.
    Cancel
  • Ashutosh_shukla

    MemberNov 17, 2008

    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<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();
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorNov 17, 2008

    Everyone - please enclose the code in
     ... [ /code ] tags
    Are you sure? This action cannot be undone.
    Cancel
  • OrAnGeWorX

    MemberNov 17, 2008

    ok here's what i got so far but i have an error problem... line 41

    #include <stdio.h>
    #include <stdlib.h>
    
    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)
    Are you sure? This action cannot be undone.
    Cancel
  • OrAnGeWorX

    MemberNov 17, 2008

    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 . . .
    Are you sure? This action cannot be undone.
    Cancel
  • shack

    MemberDec 7, 2010

    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<i;j++)
    {
    if(i%j==0)
    sum=sum+j;
    }
    if(sum==i)
    printf("%d\n",i);
    sum=0;
    }
    getch();
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Minar

    MemberOct 31, 2011

    hi, can any one tell me whats wrong in my codes here..its not showing any output.😔

    #include<stdio.h>
    #include<conio.h>
    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;
    }
    Are you sure? This action cannot be undone.
    Cancel
  • YashSoni501

    MemberJan 16, 2015

    I have made a QBasic Programme to find all the perfect numbers between 1 to 100.... as much you want
    Are you sure? This action cannot be undone.
    Cancel
  • YashSoni501

    MemberJan 16, 2015

    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
    Are you sure? This action cannot be undone.
    Cancel
  • mitta vignesh

    MemberFeb 2, 2018

    #include <stdio.h>

    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<i;j++)
    {
    if(i%j==0)
    sum=sum+j;
    }

    if(sum==i)
    printf("\nperfect number:%d",sum);
    }
    }
    Are you sure? This action cannot be undone.
    Cancel
  • mitta vignesh

    MemberFeb 2, 2018

    how to bring c editor.
    can anyone help me.
    Are you sure? This action cannot be undone.
    Cancel
  • mitta vignesh

    MemberFeb 2, 2018

    #include <stdio.h>
    
    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<i;j++)
            {
                if(i%j==0)
                sum=sum+j;
            }
        
        if(sum==i)
        printf("\nperfect number:%d",sum);
        }
    }
    
    
    #include <stdio.h>
    
    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<i;j++)
            {
                if(i%j==0)
                sum=sum+j;
            }
        
        if(sum==i)
        printf("\nperfect number:%d",sum);
        }
    }
    
    
    Are you sure? This action cannot be undone.
    Cancel
  • mitta vignesh

    MemberFeb 2, 2018

    #include <stdio.h>
    
    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<i;j++)
            {
                if(i%j==0)
                sum=sum+j;
            }
        
        if(sum==i)
        printf("\nperfect number:%d",sum);
        }
    }
    
    
    Are you sure? This action cannot be undone.
    Cancel
  • Abdul Hisham

    MemberJul 8, 2018

    c++ program find all amstrong numbers below 1000?

    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register