C++ Program To Check Armstrong Number
Last Updated :
21 Jun, 2023
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 = 153
Input: 120
Output: No
120 is not a Armstrong number.
1*1*1 + 2*2*2 + 0*0*0 = 9
Input: 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 = 723
Input: 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++
#include <bits/stdc++.h>
using namespace std;
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));
}
int order( int x)
{
int n = 0;
while (x) {
n++;
x = x / 10;
}
return n;
}
bool isArmstrong( int x)
{
int n = order(x);
int temp = x, sum = 0;
while (temp) {
int r = temp % 10;
sum += power(r, n);
temp = temp / 10;
}
return (sum == x);
}
int main()
{
int x = 153;
cout << boolalpha << isArmstrong(x) << endl;
x = 1253;
cout << boolalpha << isArmstrong(x) << endl;
return 0;
}
|
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++
#include <iostream>
using namespace std;
int main()
{
int n = 153;
int temp = n;
int p = 0;
while (n > 0) {
int rem = n % 10;
p = (p) + (rem * rem * rem);
n = n / 10;
}
if (temp == p) {
cout << ( "Yes. It is Armstrong No." );
}
else {
cout << ( "No. It is not an Armstrong No." );
}
return 0;
}
|
Output:
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: 9
Input: 10
Output : 153
Below is the C++ program to find the nth Armstrong number:
C++
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
int NthArmstrong( int n)
{
int count = 0;
for ( int i = 1; i <= INT_MAX; i++) {
int num = i, rem, digit = 0, sum = 0;
num = i;
digit = ( int ) log10 (num) + 1;
while (num > 0) {
rem = num % 10;
sum = sum + pow (rem, digit);
num = num / 10;
}
if (i == sum)
count++;
if (count == n)
return i;
}
}
int main()
{
int n = 12;
cout << NthArmstrong(n);
return 0;
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...