Finding Armstrong Numbers within a Range by deafult assign by values from and to
PYTHON,JAVA,C,CPP
Python code
lower = 1
upper = 200
for num in range(lower, 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, end=",")
Output:
1,2,3,4,5,6,7,8,9,153,
Explanation about the above code clearly explained below:
- The Python code directly assigns values to
lower and upper. - The code uses a
for loop with the range function to iterate through each number in the given range. - The
len(str(num)) expression is used to calculate the number of digits in num. - The code uses
** for exponentiation. - The output is done using the
print function. The end parameter ensures numbers are printed with a comma as a separator.
lower and upper.for loop with the range function to iterate through each number in the given range.len(str(num)) expression is used to calculate the number of digits in num.** for exponentiation.print function. The end parameter ensures numbers are printed with a comma as a separator.Java code
public class NarcissisticNumbers {
public static void main(String[] args) {
int lower = 1;
int upper = 200;
int num, order, sum, temp, digit;
for (num = lower; num <= upper; num++) {
order = (int) Math.floor(Math.log10(num)) + 1;
sum = 0;
temp = num;
while (temp > 0) {
digit = temp % 10;
sum += Math.pow(digit, order);
temp /= 10;
}
if (num == sum) {
System.out.print(num + ",");
}
}
}
}
Output:
1,2,3,4,5,6,7,8,9,153,
Explanation about the above code clearly explained below:
- The Java code defines a class named
ArmstrongNumberswith amainmethod. - The variables and loops have the same purpose and behavior as in the C and C++ codes.
- The
Math.floorfunction is used to calculate the logarithm ofnumand is cast to anint. - The
Math.powfunction is used for exponentiation. - The output is done using
System.out.printinstead ofprintforstd::cout.
C code
#include <stdio.h>
#include <math.h>
int main() {
int lower = 1;
int upper = 200;
int num, order, sum, temp, digit;
for (num = lower; num <= upper; num++) {
order = floor(log10(num)) + 1;
sum = 0;
temp = num;
while (temp > 0) {
digit = temp % 10;
sum += pow(digit, order);
temp /= 10;
}
if (num == sum) {
printf("%d,", num);
}
}
return 0;
}
Output:
1,2,3,4,5,6,7,8,9,153,
Explanation about the above code clearly explained below:
- The C code uses the
<stdio.h>and<math.h>libraries for input/output and mathematical functions, respectively. - The
loweranduppervariables define the range for finding Armstrong numbers. - The
ordervariable calculates the number of digits innumusing thefloor(log10(num)) + 1expression. - The
sum,temp, anddigitvariables are used for the calculations. - The
forloop iterates through each number in the range fromlowertoupper. - The
whileloop extracts each digit fromtempusing modulo arithmetic and calculates the sum of digits raised to the power oforder. - The
printfstatement outputs the Armstrong numbers found.
C++ code
Output:
1,2,3,4,5,6,7,8,9,153,
Explanation about the above code clearly explained below:
- The C++ code includes the
<iostream>and<cmath>libraries for input/output and mathematical functions, respectively. - The variables and loops have the same purpose and behavior as in the C code.
- The
std::coutstatement is used for output instead ofprintf. - The program returns 0 to indicate successful execution.

0 Comments