+ 1
How to make program to print 1 to 1000 Armstrong number in java
Help
3 Answers
+ 1
Ranjna Gupta
Do you know the logic of Armstrong Number? If yes then you can do for 1000 numbers using loop
Show attempts.
Armstrong Number means:
number = sum of cubes of each digit
eg.- 123 = 1**3 + 2**3 + 3**3
if LHS = RHS then Armstrong Number otherwise not
0
# Input a three digit number.Say 'n'
n = int(input("Enter a three digit number. "))
# logic is
# Let's say user entered 321.
# If 3**3 + 2**3 + 1**3 (Summation of cube of all digits of the given number) == Given Number
# Then we can say it is a Armstrong number else not.
summation = 0
ans = n
while n>0:
digit = n%10
summation += digit**3
n = int(n/10)
if summation == ans:
print(True)
else:
print(False)
# Hope you got the crux :)
# Code in python (So, you convert it into Java accordingly)
0
Here is a java method to check whether a number is Armstrong number or not.
/**
* This method return true if num is armstrong number otherwise returns
* false
*/
public static boolean isArmstrongNumber(int num) {
int sum = 0, rightDigit, temp;
temp = num;
while (temp != 0) {
rightDigit = temp % 10;
sum = sum + (rightDigit * rightDigit * rightDigit);
temp = temp / 10;
}
/*
* Check if sum is equal to N, then N is a armstrong number otherwise
* not an armstrong number
*/
if (sum == num) {
// N is armstrong number
return true;
} else {
// N is not an armstrong number
return false;
}
}
I found the full program to print armstrong number here https://www.techcrashcourse.com/2017/03/java-program-print-armstrong-numbers-between-1-to-N.html