C Program to check if the given number is an Armstrong Number

In this C Programming example, we will implement the program to check whether the given number is an Armstrong Number or not by using the user’s input and print it on screen.

1. What are Armstrong Numbers?

A positive integer is called an Armstrong number if the sum of the cubes of the digits is equal to the number itself. For example, 153 is an Armstrong number because the Armstrong number is a number that is equal to the sum of cubes of its digits.

For example 0, 1, 153, 370, 371 and 407 are Armstrong numbers.

Let’s have a look at 153 and understand why it is considered an Armstrong number.

153 = (1*1*1) + (5*5*5) + (3*3*3)
where:
(1*1*1) = 1
(5*5*5) = 125
(3*3*3) = 27
So:
1 + 125 + 27 = 153

Now, let us continue with this article on Armstrong Number in C and take a look at how to implement a program for the same.

Here are some helpful topics to understand this program better.


2. C Program to find the Armstrong Number

Let’s discuss the execution(kind of pseudocode) for the program to check whether an integer entered by the user is an Armstrong number or not in C.

  1. Initially, take the integer as input and assign it to the variable number and pass it to int checkForArmstrong(int number).
  2. From right to left, fetch a single digit(remainder) of the number and find its cube
  3. Then add all the cube values together.
  4. Check whether the sum of the cube of digits is equal to the number itself.
  5. Finally, print the relevant output to the console.

Let us implement this concept in C Program to find the armstrong number.

#include <stdio.h>

int checkForArmstrong(int number) {
  int sum = 0, remainder;
  while (number != 0) {
    remainder = number % 10;
    sum = sum + (remainder * remainder * remainder);
    number = number / 10;
  }
  return sum;
}

int main() {
  int number, sum = 0;
  printf("\nEnter a number:");
  scanf("%d", &number);

  sum = checkForArmstrong(number);

  // Condition is evaluated for a number to be an armstrong number.
  if (number == sum) {
    printf("\n%d is an Armstrong Number", number);
  } else {
    printf("\n%d is not an Armstrong Number", number);
  }
  return (0);
}
Enter a number:407
407 is an Armstrong Number

3. Conclusion

In this C Programming example, we have discussed how to check whether the given number is an Armstrong Number or not by using the user’s input and finally, print the result to the console.


Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index