C++ Program to check Armstrong number or not.
You must have basic knowledge of C++ programming language and variable , while loop and if..ealse and for loop
What is armstrong number?
The Armstrong number n which positive number equal to sum of cube power of individual number.It is also known as Armstrong number,
Example of armstrong number.
153=[1×1×1+5×5×5+3×3×3] 153
13=[1×1×1+3×3×3]28 is not armstrong
In this programe we must understand programming language and for loop and if else statement.
Example 1: Check Armstrong Number (entered by user)
#include<iosteam>
#include <conio.h>
int main ( )
{
int onum,reminder,sum=0,num;
cout<<"please enter the any positive number";
cin>>onum;
num=onum;
//this condition is use to check num not = zero.
while(num!=0)
{
reminder=num%10;
sum=sum+reminder*reminder*reminder;
num=num/10;
}
if(sum==onum)
cout <<onum <<"This number is armstrong number";
else
cout <<onum<<"This number is not armstrong";
return 0;
getch ();
}
}
Result
------------------------
Please enter the any positive numer
153
153 This number is Armstrong
In above program ,a positive number enter by the user which value is stored in onum variable.
After that,we copied onum into num variable
num=onum;
Inside the while loop , last digit is different from num by the operation digit = num % 10;. This digit is cubed and added to the variable sum.
Now, the last digit is removed using the statement num /= 10;.
In the next cycle of while loop, second last digit is different , cubed and add into sum variable.
This simultaneous until no digits are left in num. Now, the total sum sum is compared to the original digit oum.