# C Program to Check Armstrong Number

In this article, you will learn to check a given number is an Armstrong number or not using the c program.

Contents

## What is an Armstrong Number?

An Armstrong number is a number that is equal to the sum of the cubes of its digits. For example, 371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371. In this tutorial, you will learn how to check a number is Armstrong or not.

## Armstrong Number in C

Here is a C program that checks whether a number is an Armstrong number:

``````#include <stdio.h>
#include <math.h>

#include <stdbool.h>

bool is_armstrong(int n) {
int original_n = n;
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += pow(digit, 3);
n /= 10;
}
return sum == original_n;
}

int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (is_armstrong(n)) {
printf("%d is an Armstrong number\n", n);
} else {
printf("%d is not an Armstrong number\n", n);
}
return 0;
}
``````
Run Program

Output

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

## Explanation

The `is_armstrong()` function uses a loop to iterate through the digits of the number, and calculates the sum of the cubes of the digits. It then checks if the sum is equal to the original number, and returns a boolean value indicating whether the number is an Armstrong number or not.

To check if a number is an Armstrong number, the function first stores a copy of the original number in a separate variable. It then initializes a variable `sum` to 0, which will be used to store the sum of the cubes of the digits.

The function then enters a loop that continues until the number becomes 0. Inside the loop, it extracts the last digit of the number using the modulus operator (%), and adds the cube of this digit to the `sum`.

It then divides the number by 10 to remove the last digit. When the loop completes, the function checks if the `sum` is equal to the original number, and returns `true` if it is, or `false` if it is not. This process determines whether the number is an Armstrong number or not.

You can find more C Programming Examples here.

11,000Fans
375Followers
89Followers
8Subscribers