Write C++ program to check whether a number is Armstrong number or not


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


What is Armstrong number?


An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits.

For Example: 407 = 43 + 03 + 73 = 64 + 0 + 343 = 407

  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int num, sum = 0, i, r;
  9. //Reading a number from user
  10. cout<<"Enter any number to calculate factorial:";
  11. cin>>num;
  12.  
  13. //Finding armstrong number or not
  14. for(i = num; i>0; i=i/10)
  15. {
  16. r = i%10;
  17. sum = sum + r * r * r;
  18. }
  19. if ( num == sum ){
  20. cout<<num<<" is an armstrong number."<<endl;;
  21. }
  22. else{
  23. cout<<num<<" is not an armstrong number."<<endl;;
  24. }
  25.  
  26. return 0;
  27. }
  28.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts