Armstrong Number in C++

We can check whether a number is a Armstrong number or not in C++ language. Let’s we discuss about what is Armstrong number and how to check whether a number is Armstrong or not.

Armstrong number is a number that is equal to sum of the cube of its digits. Let we clear this by an example.

371 = (3*3*3)+(7*7*7)+(1*1*1)    
where:    
(3*3*3)=27    
(7*7*7)=343    
(1*1*1)=1    
So:    
27+343+1=371   

So 371 is a Armstrong number.

Armstrong Number in C++

Armstrong Number in C++

#include <iostream>  
using namespace std;  
int main()  
{  
int n,r,sum=0,temp;    
cout<<"Enter the Number=  ";    
cin>>n;    
temp=n;    
while(n>0)    
{    
r=n%10;    
sum=sum+(r*r*r);    
n=n/10;    
}    
if(temp==sum)    
cout<<"Armstrong Number."<<endl;    
else    
cout<<"Not Armstrong Number."<<endl;   
return 0;  
}  

While(n>0) condition iterates to take the digits of given number from first to last. sum=sum+(r*r*r); condition finds the cube of each digit and sum all of these answers.

Then if(temp==sum) condition checks whether the answer is equal to the original number. If it is equal then it is a Armstrong number. Otherwise, it is not a Armstrong number.

Read Also : HCF and LCM Program in C++

Output

Enter the Number= 371
Armstrong Number.

Enter the Number= 342   
Not Armstrong Number.	

Final Words

I hope this article helps you to check whether a number is a Armstrong Number or not in C++ Program. If you face any issues please let me know via the comment section. Share this article with other C++ program developers via social networks.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment