Write a C program given number Armstrong number or not

C Program to Check Armstrong Number

Introduction:
An Armstrong number (also known as a narcissistic number, pluperfect digital invariant, or pluperfect number) is a number that is the sum of its own digits each raised to the power of the number of digits. In other words, an n-digit number is an Armstrong number if the sum of its digits, each raised to the power of n, is equal to the number itself.
For example, let’s take the 3-digit number 153. The individual digits are 1, 5, and 3. The sum of the cubes of these digits is 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153, which is the same as the original number. Therefore, 153 is an Armstrong number.

Examples of Armstrong Numbers: 0, 1, 2, 3, 153, 370, 407 etc.

Explanation:

To check if a given number is an Armstrong number or not, you need to follow these steps:

• Find the number of digits in the given number.
• Extract each digit from the number.
• Raise each digit to the power of the total number of digits.
• Sum up these values.
• Check if the sum is equal to the original number.

Here’s a simple C program to determine if a given number is an Armstrong number or not:

```Program:

#include<stdio.h>
int main(){
int num,a,p,total=0;
printf("Enter a number:");
scanf("%d",&num);
a=num;
while(a!=0){
p=a%10;
total=total+(p*p*p);
a=a/10;
}
if(total == num){
printf("%d is a Armstrong number.\n",num);
}else{
printf("%d is not a Armstorng number.\n",num);
}
return 0;
}```

Output:

```Enter a number:371
371 is a Armstrong number.```

For N digits:

Program:

```#include <stdio.h>
#include <math.h>
int main()
{
int number, a, p, total = 0, b = 0 ;
printf("Enter an integer: ");
scanf("%d", &number);
a = number;
while (a != 0)
{
a =a/10;
++b;
}
a = number;
while (a != 0)
{
p = a%10;
total = total + pow(p, b);
a=a/10;
}
if(total==number){
printf("%d is an Armstrong number.\n",number);
}else{
printf("%d is not an Armstorng number.\n",number);
}
return 0;
}```

Output: Enter an integer: 8208 8208 is an Armstrong number.

Conclusion:
In conclusion, an Armstrong number is a special type of number where the sum of its digits, each raised to the power of the number of digits, equals the original number. The provided C program allows you to input a number and determine whether it is an Armstrong number or not, based on the described logic.

Scroll to Top