C++ Program To Check Armstrong Number
Given a number x, determine whether the given number is Armstrong’s number or not. A positive integer of n digits is called an Armstrong number of order n (order is a number of digits) if:
abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + ....
Examples:
Input: 153
Output: Yes
153 is an Armstrong number.
1*1*1 + 5*5*5 + 3*3*3 = 153Input: 120
Output: No
120 is not a Armstrong number.
1*1*1 + 2*2*2 + 0*0*0 = 9Input: 1253
Output: No
1253 is not a Armstrong Number
1*1*1*1 + 2*2*2*2 + 5*5*5*5 + 3*3*3*3 = 723Input: 1634
Output: Yes
1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1634
Methods to Check Armstrong Number
There are certain methods to check if a number is mentioned below:
1. Simple Approach
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.
Below is the C++ program to implement the above approach:
C++
// C++ program to determine whether // the number is Armstrong number // or not #include <bits/stdc++.h> using namespace std; // Function to calculate x // raised to the power y int power( int x, unsigned int y) { if (y == 0) return 1; if (y % 2 == 0) return (power(x, y / 2) * power(x, y / 2)); return (x * power(x, y / 2) * power(x, y / 2)); } // 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 bool isArmstrong( int x) { // Calling order function int n = order(x); int temp = x, sum = 0; while (temp) { int r = temp % 10; sum += power(r, n); temp = temp / 10; } // If satisfies Armstrong // condition return (sum == x); } // Driver code int main() { int x = 153; cout << boolalpha << isArmstrong(x) << endl; x = 1253; cout << boolalpha << isArmstrong(x) << endl; return 0; } |
true false
The complexity of the above method
Time Complexity: O(log2 n) + O((log10 n)*(log2 d)), where n is the number to be checked and d is the number of digits in the input number.
Space Complexity: O(log2 d), where d is the number of digits.
2. Efficient Approach
The above approach can also be implemented in a shorter way:
C++
// C++ program to implement // the above approach #include <iostream> using namespace std; // Driver code int main() { int n = 153; int temp = n; int p = 0; // Function to calculate // 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) { cout << ( "Yes. It is Armstrong No." ); } else { cout << ( "No. It is not an Armstrong No." ); } return 0; } |
Yes. It is Armstrong No.
The complexity of the above method
Time Complexity: The time complexity of the given program is O(d), where ‘d’ is the number of digits in the input number ‘n’. This is because the while loop runs for ‘d’ iterations, performing constant time operations in each iteration.
Auxiliary Space: The space complexity of the program is O(1) because only a few integer variables are being used and no additional space is required for arrays, lists, or other data structures.
3. Find the nth Armstrong Number
Examples:
Input: 9
Output: 9Input: 10
Output : 153
Below is the C++ program to find the nth Armstrong number:
C++
// C++ Program to find Nth // Armstrong Number #include <bits/stdc++.h> #include <math.h> using namespace std; // Function to find Nth // Armstrong Number int NthArmstrong( int n) { int count = 0; // Upper limit from integer for ( int i = 1; i <= INT_MAX; i++) { int num = i, rem, digit = 0, sum = 0; // Copy the value for num in num num = i; // Find total digits in num digit = ( int ) log10 (num) + 1; // Calculate sum of power of digits while (num > 0) { rem = num % 10; sum = sum + pow (rem, digit); num = num / 10; } // Check for Armstrong number if (i == sum) count++; if (count == n) return i; } } // Driver code int main() { int n = 12; cout << NthArmstrong(n); return 0; } |
371
Contact Us