Sunday, 11 September 2016

Armstrong Number I

C++ PROGRAMS

An Armstrong number is a number which is equal to the sum of its digits raised to the power of total number of digits.
For example: 
153 = 1^3 + 5^3 + 3^3

The following program is for determining whether the 3-digit entered by the user is an Armstrong Number or not.
In the program I will use simple if-else statement. 


Code: 

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a,b,c,d;
cout<<"Enter a 3 digit no.: ";
cin>>a;

b=a%10;  
c=a/10; 
c=c%10;
d=a/100;

b=b*b*b;
c=c*c*c;
d=d*d*d;

if(a==b+c+d)
cout<<"An Armstrong Number"<<"\n";
else
cout<<"Not an Armstrong Number"<<"\n";

getch();

}


Output:

First Run:

Second Run:





We can also find all the 3-digit Armstrong numbers. For that please go through Armstrong Number II

For any queries or suggestions please comment !

No comments:

Post a Comment