Armstrong Number in C++
Armstrong number in C++
In this post, we will write a C++ Program to check a number is Armstrong or not. We will look at various ways to code Armstrong number in C++.
What is an Armstrong Number
Any number that satisfies the following format –
- abcd… = an + bn + cn + dn + …
- Where n is the order(length/digits in number)
That is for any number num if the sum of individual digits is raised to the power of order if the result is equal to the number itself then it is an Armstrong number.
Example 1
Example = 407 (order/length = 3)
407 = 43 + 03 + 73 = 64 + 0 + 343 = 407
407 = 43 + 03 + 73 = 64 + 0 + 343 = 407
Example 2
Example = 8208 (order/length = 4)
8208 = 84 + 24 + 04 + 84 = 4096 + 16 + 0 + 4096 = 8208
8208 = 84 + 24 + 04 + 84 = 4096 + 16 + 0 + 4096 = 8208
C++ Code
#include<bits/stdc++.h> #include<math.h> using namespace std; // Armstrong number is any number following the given rule // abcd... = a^n + b^n + c^n + d^n + ... // Where n is the order(length/digits in number) // Example = 153 (order/length = 3) // 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 // Example = 1634 (order/length = 4) // 1634 = 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634 // number of digits in a number is order int order(int x) { int len = 0; while (x) { len++; x = x/10; } return len; } bool armstrong(int num, int len){ int sum = 0, temp, digit; temp = num; // loop to extract digit, find power & add to sum while(temp != 0) { // extract digit digit = temp % 10; // add power to sum sum = sum + pow(digit,len); temp /= 10; }; return num == sum; } // Driver Code int main () { //variables initialization int num = 407, len; // function to get order(length) len = order(num); // check if Armstrong if (armstrong(num, len)) cout << num << " is armstrong"; else cout << num << " is not armstrong"; return 0; }
Output:-
407 is armstrong
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
//armstrong number program that is valid for both 3 digit and 4 digit numbers
#include
#include
int main() {
int num, originalNum, remainder, n = 0;
float result = 0.0;
printf(“Enter an integer: “);
scanf(“%d”, &num);
originalNum = num;
// store the number of digits of num in n
for (originalNum = num; originalNum != 0; ++n) {
originalNum /= 10;
}
for (originalNum = num; originalNum != 0; originalNum /= 10) {
remainder = originalNum % 10;
// store the sum of the power of individual digits in result
result += pow(remainder, n);
}
// if num is equal to result, the number is an Armstrong number
if ((int)result == num)
printf(“%d is an Armstrong number.”, num);
else
printf(“%d is not an Armstrong number.”, num);
return 0;
}
#include