FIND THE PERFECT NUMBER IN C++

FIND THE PERFECT NUMBER IN C++
Di Posting Oleh : Simple Learning
Kategori : Find Perfect Number in C++ Finding Perfect Number Perfect Number C++ Perfect Number Program

What is a perfect number?
"Perfect number is a positive number which sum of all positive divisors excluding that number.
For example 6 is Perfect Number since divisor of 6 are 1, 2 and 3. Sum of its divisor is
1 + 2+ 3 =6
and  28 is also a Perfect Number
 since 1+ 2 + 4 + 7 + 14= 28
Other perfect numbers: 496, 8128




  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()                 //Start of main
  4. {
  5.   clrscr();
  6.    int i=1, u=1, sum=0;
  7.    while(i<=500)
  8.  {                                  // start of first loop.

  9.    while(u<=500)
  10.    {                               //start of second loop.
  11.      if(u<i)
  12.      {
  13.       if(i%u==0 )
  14.       sum=sum+u;
  15.      }                          //End of if statement
  16.     
  17.      u++;
  18.    }                           //End of second loop

  19.    if(sum==i)
  20.    {
  21.     cout<<i<<" is a perfect number."<<"\n";
  22.    }

  23.    i++;
  24.    u=1;  sum=0;
  25.  }                             //End of First loop
  26.    getch();
  27.  }                            //End of main

Sample output

is a perfect number.
28 is a perfect number.
496 is a perfect number.

Note:
If you want to calculate perfect number within your desire limit simply take a variable and replaced 500 with it.

0 Response to "FIND THE PERFECT NUMBER IN C++"

Post a Comment