Header Ads Widget

Ticker

6/recent/ticker-posts

Armstrong Number in C++ Notes+Coding by Digitalise Data

 

Armstrong Number in C++ Notes+Coding by Digitalise Data

Armstrong Number in C++

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.h>  
  2. #include <conio.h>  
  3. void 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. getch();  
  20. }  

Output:

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

Post a Comment

0 Comments