Friday, 10 November 2017

C++ Program for Armstrong Number

A positive integer of n digit is called an Armstrong number of order n if "abcd=pow(a,n)+pow(b,n)+pow(c,n)+pow(d,n)"

For example:
Let the number be 371
result=pow(3,3)+pow(7,3)+pow(1,3)

Here, result=371. Therefore, 371 is an Armstrong number.

C++ Program along with output is given to check if the given number is an Armstrong number or not.


PROGRAM:

#include <bits/stdc++.h>
using namespace std;

int get_power(int n){
   
   int order=0;
   while(n){
           order++;
           n=n/10;
   }
   return order;
}

int main()
{
    int n;
    printf("\n\nEnter the number: \n\n");
    cin>>n;
    int temp=n;
    int order=get_power(temp); /*func to get the order of number */
    int result=0;
    while(temp){

            int rem=temp%10;
            result=result+pow(rem,order);
            temp=temp/10;
    }

    if(result==n){

          cout<<"\n\nThe given number is Armstrong number\n";

    }
    else
    {
          cout<<"\n\nThe given number is not Armstrong number\n";
    }
    return 0;
}


OUTPUT:




EmoticonEmoticon