C++ Program to Find Perfect Number




cpp tutorials
cpp tutorials

Here we will learn how to program using C++ in order to find the perfect number.
A Perfect Number is a number that has equal factors.

C++ PROGRAM TO CHECK WHETHER A NUMBER IS NOT A PERFECT NUMBER OR NOT

#include<iostream>
#include<iomanip>
using namespace std;
int main(){
  int n,i=1,sum=0;
  cout<<"Enter a number: ";
  cin >> n;
  while(i<n){
         if(n%i==0)
               sum=sum+i;
              i++;
  }
  if(sum==n)
         cout << i  <<  " is a perfect number";
  else
         cout << i << " is not a perfect number";
  system("pause");
  return 0;
}

Examples of perfect numbers

4 = 2×2
25 = 5×5
1= 1×1
64 = 8×8
81 = 9×9
36 = 6×6

The numbers in black are all perfect squares because they have 2 equal factors.

 

 

 


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





Be the first to comment

Leave a Reply

Your email address will not be published.


*