Search Your Topic

config
Thursday, 22 June 2017 12:47

Armstrong Number in C++

Written by
Rate this item
(0 votes)

Before going to write the C++ program to check whether the number is Armstrong or not, let's understand what is Armstrong number.

Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.

Let's try to understand why 371 is an Armstrong number.

 
  1. 371 = (3*3*3)+(7*7*7)+(1*1*1)    
  2. where:    
  3. (3*3*3)=27    
  4. (7*7*7)=343    
  5. (1*1*1)=1    
  6. So:    
  7. 27+343+1=371    

 

Let's see the C++ program to check Armstrong Number.

 
  1. #include <iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5. int n,r,sum=0,temp;    
  6. cout<<"Enter the Number=  ";    
  7. cin>>n;    
  8. temp=n;    
  9. while(n>0)    
  10. {    
  11. r=n%10;    
  12. sum=sum+(r*r*r);    
  13. n=n/10;    
  14. }    
  15. if(temp==sum)    
  16. cout<<"Armstrong Number."<<endl;    
  17. else    
  18. cout<<"Not Armstrong Number."<<endl;   
  19. return 0;  
  20. }  

Output:

Enter the Number= 371
Armstrong Number.
Enter the Number= 342   
Not Armstrong Number.	

 

 

Read 231 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.