#15 Checking Armstrong Numbers in C: A Programming Example

In the world of programming, there are various interesting number patterns and properties that can be explored. One such fascinating concept is Armstrong numbers. In this blog post, we will delve into the concept of Armstrong numbers and present a C programming example that checks whether a given number is an Armstrong number or not.

Understanding Armstrong Numbers
An Armstrong number, named after Michael F. Armstrong, is a number that is equal to the sum of its own digits, each raised to the power of the number of digits. For instance, let's take the number 153. It has three digits: 1, 5, and 3. Now, if we calculate 13 + 53 + 33, the sum is 153, which matches the original number. Therefore, 153 is an Armstrong number.

C Program to Check Armstrong Numbers
Let's now dive into the C programming example that allows us to check whether a given number is an Armstrong number or not.


Explanation of the Program
  1. We start by declaring and initializing the variables 'sum', 'n', 'rem', and 'num'.
  2. The user is prompted to enter a number using 'printf ' and 'scanf' functions.
  3. The variable 'num' is assigned the value of 'n' to preserve the original number for later comparison.
  4. We then enter a while loop that continues until 'n' becomes 0.
  5. Inside the loop, the remainder ('rem') of 'n'  is calculated using the modulo operator (%).
  6. The cube of the remainder is added to the 'sum'.
  7. 'n' is divided by 10 to eliminate the last digit.
  8. This process is repeated until all the digits of the number have been processed.
  9. After exiting the loop, we compare the calculated 'sum' with the original 'num'.
  10. If they are equal, the number is an Armstrong number, and a corresponding message is displayed.
  11. If they are not equal, the number is not an Armstrong number and a different message is displayed.

Output
Let's run the program and see the output for a few sample inputs.



Conclusion
In this blog post, we explored the concept of Armstrong numbers and provided a C programming example to check whether a given number is an Armstrong number or not. Armstrong numbers are fascinating and demonstrate interesting number patterns. By using the provided program, you can now easily determine whether a number is an Armstrong number and further explore this captivating number property in your own programming projects.

Post a Comment

0 Comments