Customizable training materials to learn C++

Looking for C++ Tutorial Visit Nano teach c++!
  • C++ Loops, Creating a Pyramid
    05/07/2014 - 0 Comments
    C++ program to create a pyramid using for loop and if condition. Example: #include<iostream> using namespace std; int main() { int i,j,k,space=10; // to print the…
  • C++ Introduction
    15/07/2014 - 0 Comments
    C++ is a multi-paradigm programming language that supports object oriented programming (OOP), created by BJarne Stroustrup in 1983 at Bell Labs, C++ is an extension (superset)…
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