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
ArmstrongNumbers
with amain
method. - The variables and loops have the same purpose and behavior as in the C and C++ codes.
- The
Math.floor
function is used to calculate the logarithm ofnum
and is cast to anint
. - The
Math.pow
function is used for exponentiation. - The output is done using
System.out.print
instead ofprintf
orstd::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
lower
andupper
variables define the range for finding Armstrong numbers. - The
order
variable calculates the number of digits innum
using thefloor(log10(num)) + 1
expression. - The
sum
,temp
, anddigit
variables are used for the calculations. - The
for
loop iterates through each number in the range fromlower
toupper
. - The
while
loop extracts each digit fromtemp
using modulo arithmetic and calculates the sum of digits raised to the power oforder
. - The
printf
statement 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::cout
statement is used for output instead ofprintf
. - The program returns 0 to indicate successful execution.
0 Comments