How to check Armstrong Number ? A program to check Armstrong Number in C++

Armstrong Number : 

          If we consider a 3 digit number, if the sum of cubes of each digit is equal to a number itself then it will be called as an Armstrong Number.

Program to check Armstrong Number 

Logic :

  • Input Number.
  • Make function to check Armstrong number.
  • Find cube and keep on adding the numbers.
  • Lastly check whether the sum and original is same or not.
  • If same print Armstrong else print Not Armstrong.

Code :

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

int armstrong(int x)
{
    int temp,y=0,a=0;
    temp=x;
    while(temp)
    {
        a=temp%10;
        y=y+a*a*a;
        temp=temp/10;
    }
    if(x==y)
    {
        return 1;
    }
    return 0;
}

int main()
{
    int n;
    cin>>n; 
    if(armstrong(n))
    {
        cout<<"armstrong";
    }
    else
    {
        cout<<"not armstrong";
    }
    return 0;
}


***

Note : The above program is applicable for 3 digit numbers only.


Post a Comment

0 Comments