Answer to Question #270258 in C++ for print

Question #270258

perfect number is an integer that is equal to the sum of its factors. write down a c++ program that takes an integer x as an input and prints all perfect numbers from 1 to x.


1
Expert's answer
2021-11-23T05:18:33-0500
#include <iostream> 
#include <cctype>
using namespace std; 
int perfectNumber(int n){
	int i=1,sum=0;
	   while(i<n){
       if(n%i==0)
       sum=sum+i;
       i++; 
}
 
if(sum==n)
    return 1;
else
   return 0;
}
int main(){
    int n;
    cout << "Enter a number: ";
    cin >> n;
    for(int i=1; i<=n; i++){
    	if(perfectNumber(i)==1){
    		cout<<i<<endl;
		}
	}
    
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS