In this article, you will learn to check a given number is an Armstrong number or not using the c program.
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:
Enter a number: 371 371 is an Armstrong number
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
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.