Friday, June 18, 2021
Home coding Write a program to check armstrong number.

Write a program to check armstrong number.

Given a number we have to find whether number is armstrong or not.

So what is Armstrong number?

Armstrong Number is a number which is equal to the sum of cube of its digit.

for example-0,1,153,371 all are armstrong number.

Example:

input:1
output:true

input :9:
output:false

Logic:

Logic behind this question is to just find all the digit of number then calculate cube of all digits. then resultant output is armstrong number.

#include<iostream>
using namespace std;
int main()
{
 int num;
 cout<<"Enter a number : "<<endl;
 cin>>num;
 int armstrong=0;
 int original_number=num;
 int pal=0;
 while(num>0)
 {
     int rem=num%10;
    armstrong+=rem*rem*rem;
     num=num/10;
 }
 if(armstrong==original_number)
 {
     cout<<"number is armstrong "<<endl;
 }
 else
 {
     cout<<"number is not armstrong"<<endl;
 }
 

}

Output:

Enter a number : 371
number is armstrong 

Time Complexity-O(N)

Space Complexity-0(1)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments