19 September 2018

C++ -Calculate perfect number


Perfect number : is a positive integers which is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself

(or)

Perfect number : is a positive integers which is equal to the sum of its proper positive factors, that is, the sum of its positive factors excluding the number itself

For example 
 
 6 has factors 1,2,3 and 6(number itself)

//C++ program to calculate perfect number
#include<iostream>
using namespace std;
  int main()
    {
    int n,sum=0;
    cout<<"Enter a number: ";
    cin>>n;   
    for(int i=1;i<n;i++)
        {
        if(n%i==0)
          sum+=i;
        }
    if(n==sum)
        cout<<n<<" is a Perfect number"<<endl;
    else
        cout<<n<<" is not Perfect number"<<endl;
return 0;
}
Similar program(s) :

C++ Program to find a given number is STRONG or not.
   

No comments:

Post a Comment

To convert from Upper case to lowercase(A to a)

//converting from Upper to lower(A to a) import java.util.*; class case { public static void main(String args[]) { System.out.pr...