Find the Armstrong Numbers in a given Interval in C++

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++. 

amstrong number between given range

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.

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.

Find the Armstrong Numbers in a given Interval in C++

C++ Code

Run

#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:

  1. Include the required header files.
  2. Define a Function order() which takes a number as argument and returns it’s length.
  3. Define another function armstrong() which accepts the intervals low and high as arguments and prints the Armstrong numbers along the way.
  4.  Using for loop we’ll check for each number if it’s an Armstrong Number.
  5. We’ll get the length of the number using the order function.
  6. Using while loop we’ll carry on the process which is shown in the image above.
  7. check if the sum of the digits raised to the power of it’s length is equal to the number itself.
  8. 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.

Python Code

Run
#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:

  1. Include the required header files.
  2. Define another function findArmstrong() which accepts the intervals low and high as arguments and prints the Armstrong numbers along the way.
  3.  Using for loop we’ll check for each number if it’s an Armstrong Number.
  4. We’ll get the length of the number using a while loop.
  5. Using while loop we’ll carry on the process which is shown in the image above.
  6. check if the sum of the digits raised to the power of it’s length is equal to the number itself.
  7. If true, print the number else keep checking.

The output for the above code is all the Armstrong numbers in the given interval.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription