Find the Armstrong Numbers in a given Interval in C++
September 28, 2022
Find the Armstrong Numbers in a given Range in C++
Given two integers high and low for limits as inputs, the objective is to write a code to Find the Armstrong Numbers in a given Interval in C++.
For Instance,Input : 100 400
Output : 153 370 371
Find the Armstrong Numbers in a given Interval in C++
Given two integer inputs as high and low, the objective is to find all the Armstrong Numbers that falls under the limits [Low,High] interval. Before Going into the Explanation let’s understand the problem.
Armstrong NumbersIn number theory, a narcissistic number in a given number base b is a number that is the sum of its own digits each raised to the power of the number of digits.
Let’s take the above example and try to understand the definition of Armstrong Numbers, The input for the code as mentioned is the range within which we search for the Armstrong Numbers. The Armstrong Number we Found were 153, 370 and 371. According to the Definition an Armstrong number is any number that can be expressed an the sum of it’s digits raised to the power of it’s length. And as the above mentioned numbers satisfy the conditions, They are printed out as Armstrong Numbers lying with the range [100,400].
Find the Armstrong Numbers in a given Range in C++
As mentioned above, given two integer inputs as limits or range, write a C++ code to find all the Armstrong Numbers lying in the given range. In order to do so we have two different approaches,
Method 1: Using Order function
Method 2: Without Order function
We’ll discuss these approaches in depth in the sections below.
Method 1: Using Order Function
In this method we define the order() function which takes in an integer value x and returns the number of digits of the number.
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
// number of digits in a number is order
int order(int x)
{
int len = 0;
while (x)
{
len++;
x = x/10;
}
return len;
}
void armstrong(int low, int high){
for(int num = low; num <= high; num++){
int sum = 0, temp, digit, len;
temp = num;
// function to get order(length)
len = order(num);
// loop to extract digit, find cube & add to sum
while(temp != 0)
{
// extract digit
digit = temp % 10;
// add power to sum
sum = sum + pow(digit,len);;
temp /= 10;
};
if(sum == num)
cout << num << " ";
}
}
// Driver Code
int main ()
{
//variables initialization
int low = 100, high = 400;
// get armstrong numbers
armstrong(low, high);
return 0;
}
Output
153 370 371
Algorithm
The objective of the above code is to find all the Armstrong Numbers in the given input interval in C++. In this Method we’ll use a user defined function order() to solve the problem.
The algorithm for the above code is as follows:
Include the required header files.
Define a Function order() which takes a number as argument and returns it’s length.
Define another function armstrong() which accepts the intervals low and high as arguments and prints the Armstrong numbers along the way.
Using for loop we’ll check for each number if it’s an Armstrong Number.
We’ll get the length of the number using the order function.
Using while loop we’ll carry on the process which is shown in the image above.
check if the sum of the digits raised to the power of it’s length is equal to the number itself.
If true, print the number else keep checking.
The output for the above code is all the Armstrong numbers in the given interval.
Method 2: Without using Order Function
In this method we will perform both length calculation and breaking down of the number input within the actual function.
#include <bits/stdc++.h>
using namespace std;
// Prints Armstrong Numbers in given range
void findArmstrong(int low, int high)
{
for (int i = low+1; i < high; ++i) {
// number of digits calculation
int x = i;
int n = 0;
while (x != 0) {
x /= 10;
++n;
}
// compute sum of nth power of
// its digits
int pow_sum = 0;
x = i;
while (x != 0) {
int digit = x % 10;
pow_sum += pow(digit, n);
x /= 10;
}
// checks if number i is equal to the
// sum of nth power of its digits
if (pow_sum == i)
cout << i << " ";
}
}
// Driver code
int main()
{
int num1 = 100;
int num2 = 400;
findArmstrong(num1, num2);
cout << '\n';
return 0;
}
Output
153 370 371
Algorithm
The objective of the above code is to find all the Armstrong Numbers in the given input interval in C++.
The algorithm for the above code is as follows:
Include the required header files.
Define another function findArmstrong() which accepts the intervals low and high as arguments and prints the Armstrong numbers along the way.
Using for loop we’ll check for each number if it’s an Armstrong Number.
We’ll get the length of the number using a while loop.
Using while loop we’ll carry on the process which is shown in the image above.
check if the sum of the digits raised to the power of it’s length is equal to the number itself.
If true, print the number else keep checking.
The output for the above code is all the Armstrong numbers in the given interval.
Login/Signup to comment