10. Program that inputs a number from the user and displays all perfect numbers up to the number entered.

Chapter 6: 

Looping Structures

Programming Exercise

Problem # 10:

Write a program that inputs a number from the user and displays all perfect numbers up to the number entered.

Solution:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int i, t_num, num=1, sum;
    cout<<"Enter the number upto which perfect numbers are required:";
    cin>>t_num;
    while(num<=t_num)
    {
        sum=0;
        for(i=1;i<num;i++)
        {
            if(num%i==0)
            sum=sum+i;
        }
        if(sum==num)
        cout<<endl<<num<<" is a perfect number.";
        else
        cout<<endl<<num<<" is not a perfect number.";
        num++;
    }
    return 0;
}

Let me know in the comment section if you have any question.

Previous Post:
9. Program that inputs a number from the user and displays all Armstrong numbers up to the number entered.

Comments

Post a Comment