### Armstrong Number In C, All You Need To Know About The Program For Armstrong Numbers

An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits. These numbers are named after Michael F. Armstrong, who introduced them in 1969. In this article, we will discuss Armstrong numbers in C and how to write a program to check if a number is an Armstrong number.

To check if a number is an Armstrong number in C, we can write a program that calculates the sum of the cubes of each digit of the number and compare it with the original number. If they are equal, the number is an Armstrong number. If you are interested in gaining more knowledge in this field you can go through the Online C Courses and Certifications listed on our website.

##### What is Armstrong number in C?

An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 is an Armstrong number because 1$^3$ + 5$^3$ + 3$^3$ = 153. Armstrong numbers are named after Michael F. Armstrong, who introduced them in 1969.

##### Program for Armstrong numbers

Here is a simple program in C to check if a number is an Armstrong number:

#####include <stdio.h>

#####include <math.h>

int main() {

int num, originalNum, remainder, result = 0, n = 0;

printf(“Enter an integer: “);

scanf("%d”, &num);

originalNum = num;

// store the number of digits of num in n

for (originalNum = num; originalNum != 0; ++n) {

originalNum /= 10;

}

for (originalNum = num; originalNum != 0; originalNum /= 10) {

remainder = originalNum % 10;

// store the sum of the power of individual digits in result

result += pow(remainder, n);

}

// if num is equal to result, the number is an Armstrong number

if ((int)result == num)

printf("%d is an Armstrong number.”, num);

else

printf("%d is not an Armstrong number.", num);

return 0;

}

This program first takes an integer input from the user. Then, it calculates the number of digits in the input number and stores it in n. Next, it calculates the sum of the power of individual digits and stores it in result. Finally, it checks if the input number is equal to result. If they are equal, the input number is an Armstrong number; otherwise, it is not.

Output of this code looks like :

Enter an integer: 153

153 is an Armstrong number.

##### C program to check Armstrong number

Here is another program in C to check if a number is an Armstrong number:

#####include <stdio.h>

int main() {

int num, originalNum, remainder, result = 0;

printf(“Enter a three-digit integer: “);

scanf("%d”, &num);

originalNum = num;

while (originalNum != 0) {

// remainder contains the last digit

remainder = originalNum % 10;

result += remainder * remainder * remainder;

// removing last digit from the original number

originalNum /= 10;

}

if (result == num)

printf("%d is an Armstrong number.”, num);

else

printf("%d is not an Armstrong number.", num);

return 0;

}

This program first takes a three-digit integer input from the user. Then, it
calculates the sum of the cube of each digit and stores it in result. Finally,
it checks if the input number is equal to result. If they are equal, the input
number is an Armstrong number; otherwise, it is not.

Here is how the Output of this code looks like :

Enter a three-digit integer: 123

123 is not an Armstrong number.

##### Armstrong number in C using function

Here is a program in C to check if a number is an Armstrong number using a function:

#####include <stdio.h>

#####include <math.h>

int isArmstrong(int number) {

int lastDigit = 0;

int power = 0;

int sum = 0;

int n = number;

while (n != 0) {

lastDigit = n % 10;

power = pow(lastDigit, 3);

sum += power;

n /= 10;

}

if (sum == number)

return 0;

else

return 1;

}

int main() {

int number;

printf(“Enter number: “);

scanf("%d”, &number);

if (isArmstrong(number) == 0)

printf("%d is an Armstrong number.\n”, number);

else

printf("%d is not an Armstrong number.\n", number);

return 0;

}

This program first takes an integer input from the user. Then, it checks if the input number is an Armstrong number using the isArmstrong function. Finally, it prints whether the input number is an Armstrong number or not. The output of this code would be similar to the ones explained above.However, this approach is more modular, thereby creating a scalable code.

##### Armstrong number in C using for loop

Here is a program in C to check if a number is an Armstrong number using a for loop:

#####include <stdio.h>

#####include <math.h>

int main() {

int x, y, z, n, i, j, k, sum;

printf(“Armstrong numbers between 1 and 1000 are: “);

for (i = 1; i <= 1000; ++i) {

sum = 0;

// find the number of digits in i

n = 0;

x = i;

while (x > 0) {

x /= 10;

++n;

}

// calculate sum of nth power of each digit

y = i;

while (y > 0) {

z = y % 10;

sum += pow(z, n);

y /= 10;

}

// check if i is an Armstrong number

if (i == sum) {

printf("%d “, i);

}

}

return 0;

}

This program prints all Armstrong numbers between 1 and 1000. It first initialises variables x, y, z, n, i, j, k, and sum. Then, it loops through all numbers between 1 and 1000. For each number, it calculates the number of digits and stores it in n. Next, it calculates the sum of the nth power of each digit and stores it in sum. Finally, it checks if the number is an Armstrong number and prints it if it is.

##### Conclusion

In this article, we discussed Armstrong numbers in C and how to write a program to check if a number is an Armstrong number. We covered several methods to check if a number is an Armstrong number, including using a for loop and a function. We also provided a program to print the Armstrong number in c between 1 to 1000. We hope this article was helpful in understanding Armstrong numbers in C.