Armstrong of a Number in the Given Range

Definition & Explanation

An Armstrong number (also known as a narcissistic number, plenary number, or plenary power) is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3+5^3+3^3=153

Program Logic

  1. Input:
    • lower and upper are variables representing the lower and upper bounds of the range within which Armstrong numbers are to be found.
  2. Loop through the Range:
    • The program iterates over each number in the range from lower to upper (inclusive).
  3. Check Armstrong Number:
    • For each number num, the program calculates the number of digits (order) in num using the len() function.
    • It then initializes sum to 0 and stores a copy of num in temp.
    • Inside a while loop, the program extracts each digit of temp using the modulus operator % and calculates its orderth power. The result is added to sum.
    • After processing all digits, if num is equal to sum, then num is an Armstrong number, and it is printed.
  4. Output:
    • The program prints all Armstrong numbers found within the specified range.

You can adjust the values of lower and upper to find Armstrong numbers within different ranges.

C

Method 1 :

#include <stdio.h>
#include <math.h>

int main() {
    int lower = 100, upper = 1000;
    printf("Armstrong numbers in the range %d to %d are:\n", lower, upper);

    for (int num = lower; num <= upper; ++num) {
        int order = 0, temp = num, digit, sum = 0;
        
        // Count the number of digits in 'num'
        while (temp ! = 0) {
            temp / = 10;
            ++order;
        }
        
        temp = num;
        while (temp ! = 0) {
            digit = temp % 10;
            sum += pow(digit, order);
            temp /= 10;
        }
        
        if (num == sum) {
            printf("%d\n", num);
        }
    }

    return 0;
}

Output :

C++

Method 1 :

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    int lower = 100, upper = 1000;
    cout << "Armstrong numbers in the range " << lower << " to " << upper << " are:" << endl;

    for (int num = lower; num <= upper; ++num) {
        int order = 0, temp = num, digit, sum = 0;
        
        // Count the number of digits in 'num'
        while (temp != 0) {
            temp /= 10;
            ++order;
        }
        
        temp = num;
        while (temp != 0) {
            digit = temp % 10;
            sum += pow(digit, order);
            temp /= 10;
        }
        
        if (num == sum) {
            cout << num << endl;
        }
    }

    return 0;
}

Output :

JAVA

Method 1 :

public class Main {
    public static void main(String[] args) {
        int lower = 100, upper = 1000;
        System.out.println("Armstrong numbers in the range " + lower + " to " + upper + " are:");
        
        for (int num = lower; num <= upper; ++num) {
            int order = 0, temp = num, digit, sum = 0;
            
            // Count the number of digits in 'num'
            while (temp != 0) {
                temp /= 10;
                ++order;
            }
            
            temp = num;
            while (temp != 0) {
                digit = temp % 10;
                sum += Math.pow(digit, order);
                temp /= 10;
            }
            
            if (num == sum) {
                System.out.println(num);
            }
        }
    }
}

Output :

Python

Method 1 :

low=100
upper=1000

for num in range(low,upper+1):
	order=len(str(num))
	sum=0
	temp=num
	while temp>0:
		digit=temp%10
		sum+=digit**order
		temp//=10
	if num==sum:
		print(num)

Output :

153
370
371
407