Answer to Question #272850 in C++ for saud

Question #272850

A perfect number is an integer that is equal to the sum of its factors. For example, 6 is a perfect number

as 6 = 3+2+1. Write down a program that takes an integer x as an input and prints all perfect numbers

from 1 to x.


1
Expert's answer
2021-11-29T00:00:44-0500


SOLUTION CODE




#include <iostream>
using namespace std;


int main()
{
  cout<<"Enter an integer to get the perfect numbers upto that point: ";
  int my_integer;
  cin>>my_integer;
  //Now let the get the perfect numbers
  cout<<"The following are perfect numbers between 1 and "<<my_integer<<endl;
  for(int i = 2; i < my_integer; i++)
  {
  	//declare a variable to store the sum of the integers
  	int sum = 0;
  	for(int j = 1; j<=((i/2)+1); j++)
  	{
  	 if(i%j==0)
	   {
	   	 sum = sum + j;
	   }	
	}
	
	if(sum==i)
	{
		cout<<i<<"  ";
	}
  }
    return 0;
}


SAMPLE PROGRAM CODE



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