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

Chapter 6: 

Looping Structures

Programming Exercise

Problem # 9:

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

Solution:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int num, r, n,sum, e_num;
    cout<<"Enter a number for required Armstrong numbers:";
    cin>>e_num;
    for(num=1;num<=e_num;num++)
    {
        n=num;
        sum=0;
        while(n!=0)
        {          
            r=n%10;
            sum=sum+(r*r*r);
            n=n/10;
        }
        if(sum==num)
        cout<<endl<<num<<" is an armstrong number.";
        if(sum!=num)
        cout<<endl<<num<<" is not an armstrong number.";
    }      
    return 0;
}

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

Previous Post:
8. Program that inputs a number from the user and displays Fibonacci series up to the number entered.

Comments