Write a C program to check an Armstrong number

Write a c program to check an Armstrong number, In this C programming example, we will learn how to check an Armstrong number program in C.

Sample input

Enter a number: 153

Sample output

It is an Armstrong Number

What is Armstrong Number?

The sum of cubes of each digit is equal to the number itself. for example, 153: 153=1*1*1+5*5*5+3*3*3.

Algorithm

  • Declare four variables and initialize one variable.
  • Then take an input.
  • Take a while loop.
  • In while, the loop takes the if-else statement.
  • Give all the conditions in loop and statement.

C program to check Armstrong Number

#include<stdio.h>
 void main (){
 int n,r,c,sum=0,temp;
 printf("enter a number: ");
 scanf("%d",&n);
 temp=n;
  while(n>0){
   r=n%10;
   c=r*r*r;
   sum=sum+c;
   n=n/10;
   }
  n=temp;
  if(n==sum){
    printf("it is a Armstrong Number ");
    }
   else
    printf("it is not a Armstrong Number ");
 }

Explanation of this C program

Step 1: Start.

Step 2: Create a header file(ProgramName.h) and include the library on it.

Step 3: Then create a void main function.

Step 4: Declare four variable ( n,r,c,and temp )and intialize one variable that is sum=0.

Step 5: Create an input with the help of scanf for taking a number from the user to check Armstrong Number.

Step 6: Create a while loop and give conditions on it that is :
–  n>0 (n=given input number from user),
–  r=n%10 (r=riminder),
– c=r*r*r (c=cubes),
–  sum=sum+c ,
–  n=n/10.

Step 7: Then create an if-else statement in statement give the condition (temp==sum) to print a suitable message which is Armstrong Number or not an Armstrong Number.

Step 8: End.

Output

In this way, we see how to write a C program to check Armstrong’s number.