Armstrong number program in c

 Armstrong Number 

  • Armstrong number is a number that is equal to the sum of cubes of its digits.
  • Armstrong numbers are 153, 370, 371, 407 etc... 
Problem Statement

Write a c program to check whether the given number is an Armstrong number or not. 

Solution 

#include<stdio.h>

int main()

{

int n,temp,a,b=0;

scanf("%d",&n);

temp=n;

while(n!=0)

{

a=n%10;

b=b+(a*a*a);

n=n/10;

}

if(temp==b)

{

printf("%d is an Armstrong Number",temp);

}

else

{

printf("%d is not an Armstrong Number",temp);

}

return 0;

}

Input

153

Output

153 is an Armstrong Number

Output Explanation

153 ->( 1(cube)+5(cube)+3(cube))

153->(1+125+27)

153->(153)

It matches with input given number. So It is an Armstrong Number.

Input 2

275

Output 2

275 is not an Armstrong Number

Program Explanation

  • In this Armstrong number c program, we have declared integer variables n, temp, a and b.
  • Using n variable getting the input using scanf function.
  • after getting input stored in another variable known as temp to check given number is Armstrong number or not.
  • After that in while loop convert the input number into digits.
  • Each time during conversion cube the digits and add with the b values, continue the process of digit conversion operation till the input number tends to zero.
  • we are checking the temp and b variable values for verification of Armstrong number in this c program.
  • If both are same then the given number is an Armstrong number or else the given number is not an Armstrong number. 
  • The final Output is displayed using printf function in this program.



Rajesh

This Blog will help you to find out the c programs solution easily and also you can able to understand the program.Please follow our blog for more useful updates.

Post a Comment (0)
Previous Post Next Post