Write a program to check Armstrong number in C

Armstrong number in C is a special number which is equal to the sum of cubes of its digits. e.g. 153 = 1*1*1*+5*5*5+3*3*3=153

Program to check Armstrong number in C

#include<stdio.h>
#include<conio.h>

void main()
{
int n,rem,sum=0,tmp;
clrscr();
printf("Enter any number to check armstrong");
scanf("%d",&n);

tmp=n;//making copy of number to check for armstrong
while(n>0)
{
rem=n%10;
sum=sum+rem*rem*rem;
n=n/10;
}

if(sum==tmp)
{
printf("Given number is an Armstrong");
}
else
{
printf("Given number is not an Armstrong");
}
getch();
}

Output

Enter any number to check Armstrong 252
Given number is not an Armstrong

Enter any number to check Armstrong 153
Given number is an Armstrong

Program Code Explanation

Above we write a program on Armstrong number in C. Let’s understand step by step. First, we input a number and store it in variable n and then make a duplicate copy of that variable to compare the result in last.

Execute a while loop until n>0 and fetch reminder in rem by dividing 10. This instruction will give us one digit of number n.

As per Armstrong program, we added the cube of digit of number n.

Finally, we check the sum with tmp variable. If both are equal so given number is Armstrong otherwise the number is not an Armstrong.

You can also check these programs

Hope that you understand Armstrong number in C program. If you have any question please comment me in below comment section. Also, don’t forget to share this program in your friend’s circle.

You can also check our technical blog zerootech.com

Leave a Comment

error: Don\'t make piracy - Write your own content :)