
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Check Armstrong Number
An armstrong number is a positive integer that is equal to the sum of its digits, each raised to the power of the total number of digits in the number. Our goal here is to check armstrong numbers in C++. In simple terms, for a number with n digits:
abcd... = a^n + b^n + c^n + d^n + ...
If this condition is satisfied, we can say the number is an armstrong number.
Let's understand with examples:
153 is an Armstrong number because: 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 1634 is also an Armstrong number because: 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634 But 123 is not an Armstrong number because: 1^3 + 2^3 + 3^3 = 1 + 8 + 27 = 36 ? 123
Checking Armstrong Number
To check whether a given number is an armstrong number or not in C++, we use a loop. The loop helps us go through each digit of the number one by one.
Here's how we do it:
- First, we take the number and count its digits.
- Then, using a loop, we extract each digit and raise it to the power of the total number of digits.
- After processing all the digits, we compare the sum with the original number. If they match, the number is an armstrong number.
Example to Check Armstrong Number in C++
Below is the complete C++ program where we check if a number is an Armstrong number:
#include <iostream> #include <cmath> using namespace std; int main() { int num = 153; // Number to check int originalNum = num; int remainder, result = 0, n = 0; // Count number of digits while (originalNum != 0) { originalNum /= 10; ++n; } originalNum = num; // Calculate the sum of digits raised to the power n while (originalNum != 0) { remainder = originalNum % 10; result += pow(remainder, n); originalNum /= 10; } // Display number cout << "Number to be checked is: " << num << endl; // Print result if (result == num) cout << num << " is an Armstrong number." << endl; else cout << num << " is not an Armstrong number." << endl; return 0; }
The program checks if the number 153 is an armstrong number and gives the output:
Number to be checked is: 153 153 is an Armstrong number.
Time Complexity: O(d), where dis the number of digits in the number.
Space Complexity: O(1) bcause no extra space is used.
Find the Nth Armstrong Number
Here, we are going to find the nth Armstrong number, where n is entered by the user. Armstrong numbers don't appear consecutively, they occur at specific positions, like:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407...
So, if the user enters n = 1, the result will be 0. For n = 10, the result will be 153. If n = 13, the result will be 407, and so on.
To find this, we loop through numbers, check if each one is an armstrong number, and stop when we reach the nth one.
C++ Program to Find the Nth Armstrong Number
Here's a complete C++ program to find the nth armstrong number. You just need to input the value of n, and it will calculate the corresponding armstrong number.
#include <bits/stdc++.h> #include <math.h> using namespace std; // Function to find the nth Armstrong number int findNthArmstrongNumber(int nthPosition) { int count = 0; // Start checking numbers from 1 for (int currentNum = 1; currentNum <= INT_MAX; currentNum++) { int temp = currentNum; int digitCount = (int)log10(temp) + 1; int sum = 0; // Calculate sum of each digit raised to the power of digit count while (temp > 0) { int digit = temp % 10; sum += pow(digit, digitCount); temp /= 10; } // Check if it's an Armstrong number if (sum == currentNum) { count++; if (count == nthPosition) return currentNum; } } return -1; } int main() { int nth = 13; cout << "Number to find is: " << nth << endl; int armstrongNumber = findNthArmstrongNumber(nth); // Output heading and result cout << "The " << nth << "th Armstrong number is: " << armstrongNumber << endl; return 0; }
The output below shows the nth armstrong number based on the given input.
Number to find is: 13 The 13th Armstrong number is: 407
Time Complexity: O(n * m), where n is the number of iterations and m is the number of digits in the number being checked.
Space Complexity: O(1), only a few variables are used, no extra space needed.