C Program to Check Armstrong Number
In this article, we will see how to check Armstrong number in the c program. We can implement two logic for checking Armstrong numbers, one for 3-digit numbers and the second one for more than 3 digits.
What is Armstrong Number
The Armstrong number can be defined as a number that is equal to the result of the summation of the nth power of each n digit. Let’s assume we have a number XYZ which is 3 digit number, so XYZ can be an Armstrong number if XYZ is equal to the result of the sum of each digit of the 3rd power.
xyz.....n = xn + yn + zn
Example:
Input: 153
Output: Yes
Explanation :
153 is an Armstrong number of 3 digits, since the sum of cubes of each digit is equal to the number itself. As shown below:
1*1*1 + 5*5*5 + 3*3*3 = 153
Input: 120
Output: No
Explanation :
120 is not a Armstrong number of 3 digits, the sum of cubes of each digit is equal to 9 but number is 120.
1*1*1 + 2*2*2 + 0*0*0 = 9
Input: 1253
Output: No
Explanation :
1253 is not a Armstrong Number of 4 digits, the sum of 4th power of each digit is equal to 723 but the number is 1253.
1*1*1*1 + 2*2*2*2 + 5*5*5*5 + 3*3*3*3 = 723
Input: 1634
Output: Yes
Explanation :
1634 is an Armstrong number of 4 digit, the sum of 4th power of each digit is equal to the number itself. As shown below:
1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1634
C Program for Armstrong Number of Three Digits
We can simply calculate the sum of individual digits by dividing the number by 10 and getting reminders. And if it will be equal to the number itself then it is an Armstrong number. Here time complexity will be reduced to O(log(n)).
C
// C program to check given number is // Armstrong number or not using Sum // of Digits #include <stdio.h> // Driver code int main() { int n = 153; int temp = n; int p = 0; // Calculating the sum of individual digits while (n > 0) { int rem = n % 10; p = (p) + (rem * rem * rem); n = n / 10; } // Condition to check whether the // value of P equals to user input // or not. if (temp == p) { printf ( "Yes. It is Armstrong No." ); } else { printf ( "No. It is not an Armstrong No." ); } return 0; } |
Yes. It is Armstrong No.
- Time Complexity: O(logn)
- Auxiliary Space: O(1)
C Program for Armstrong Number of n Digits
The idea is to first count the number of digits (or find the order). Let the number of digits be n. For every digit r in input number x, compute rn. If the sum of all such values is equal to n, then return true, else false.
C
// C program to check given number // is Armstrong number or not // using Functions #include <stdio.h> // math.h is used for pow function. #include <math.h> // Function to calculate // order of the number int order( int x) { int n = 0; while (x) { n++; x = x / 10; } return n; } // Function to check whether the // given number is Armstrong // number or not int isArmstrong( int x) { // Calling order function int n = order(x); int temp = x, sum = 0; while (temp) { int r = temp % 10; sum += pow (r, n); temp = temp / 10; } // If satisfies Armstrong condition if (sum == x) return 1; else return 0; } // Driver Program int main() { int x = 153; if (isArmstrong(x) == 1) printf ( "True\n" ); else printf ( "False\n" ); x = 1253; if (isArmstrong(x) == 1) printf ( "True\n" ); else printf ( "False\n" ); return 0; } |
true false
- Time Complexity: O(logx*log(logx))
- Auxiliary Space: O(1)