Home » C Program » C Program to Check a Given Number is an Armstrong or not

C Program to Check a Given Number is an Armstrong or not

Last updated:

Support this post with a reaction:

2,605 views

An Armstrong number program in C is a basic programming exercise used to check whether a given number is equal to the sum of its digits raised to the power of the total number of digits.

For example, 153 is an Armstrong number because the sum of the cubes of its digits is equal to the original number.

153 = 1³ + 5³ + 3³
153 = 1 + 125 + 27
153 = 153

What is Armstrong Number?

An Armstrong number is a number that is equal to the sum of its digits, where each digit is raised to the power of the total number of digits in that number.

It is also known as a Narcissistic number.

For example:

  • 153 is an Armstrong number because 1³ + 5³ + 3³ = 153.
  • 370 is an Armstrong number because 3³ + 7³ + 0³ = 370.
  • 9474 is an Armstrong number because 9⁴ + 4⁴ + 7⁴ + 4⁴ = 9474.
  • 123 is not an Armstrong number because 1³ + 2³ + 3³ = 36, which is not equal to 123.

How to Check Whether a Number Is an Armstrong Number?

Let’s understand the calculation using the number 153.

Since 153 contains three digits, we need to calculate the cube of each digit.

Step 1: Identify the digits

The digits of 153 are 1, 5 and 3.

Step 2: Calculate the power of each digit

1³ = 1 × 1 × 1 = 1

5³ = 5 × 5 × 5 = 125

3³ = 3 × 3 × 3 = 27

Step 3: Add the calculated values

Sum = 1 + 125 + 27

Sum = 153

Step 4: Compare the sum with the original number

Original number = 153

Calculated sum = 153

Since both values are equal, 153 is an Armstrong number.

C Program to check a number is Armstrong or not

#include<stdio.h>
#include<math.h>
#include<conio.h>
void main(){
	int number,i=0,n,result=0,number1,temp;
	printf("enter the number:");
	scanf("%d",&number);
	number1=number;
	temp=number;
	while(number!=0){
		number=number/10;
		i++;
	}
	while(number1!=0){
		n=number1%10;
		result=result+pow(n,i);
		number1=number1/10;
		}
	if(temp==result)
		printf("number is armstrong");
		else
		printf("not a armstrong");
	getch();
} 

Output 1:

enter the number:153
number is armstrong

Output 2:

enter the number:321
not a armstrong

Explanation of Armstrong number validating program in c

  • In the above program, you can see that I have Math.h header file because We need pow() function to calculate power and this function is defined in Math.h header file.
  • Here I have taken almost six variable because in most of cases, we need to persist the previous value for the validation purpose.
  • So first, I have taken input from the user and stored it in the ‘number’ variable.
  • Now again, I am assigning the same variable in the ‘temp’ and ‘number1’ variable.
  • First, while a loop is used to calculate the count of digits in a given number, we will use this count to calculate the power of each digit.
  • And Second While loop is responsible for calculating the power of each digit and adding them together.
  • Now, at last, we have if statement which I have used to validate our result after the performing operations.
  • Using this if statement we are validating our original number(stored in ‘temp’ variable) is equal or not to calculated result ( stored in ‘result’ variable). 
  • Its value of temp and result is equal. It means that number is an Armstrong, and if not equal, then it is not an Armstrong number.

I hope now you have an idea of how to calculate and write a program to check number is an Armstrong or not in C.

Watch the Video Tutorial of Armstrong Program in C