Customizable training materials to learn C++

Looking for C++ Tutorial Visit Nano teach c++!
  • C++ Keywords
    09/07/2014 - 0 Comments
    The C + + Keywords must be in your information because you can not use them as variable name. C++ Keywords List You can’t use below mentioned keyword as identifier in your C++…
  • C++ Objects and Classes
    08/07/2014 - 0 Comments
    In object-oriented programming languages like C++, the data and functions (procedures to manipulate the data) are bundled together as a self-contained unit called an object. A…
Nano teach C++ found result for your search here:

C++ Program to find 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
Example:
#include<iostream> 
#include<cctype>
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\n"; 
else
    cout << i << " is not a perfect number\n";
    system("pause"); 

return 0;

}
Program Output:
6 is a perfect number
15 is not a perfect number
28 is a perfect number
496 is a perfect number

No comments:

Post a Comment