Write a program to print all Armstrong numbers from 1-500
IT SHOULD BE A JAVA PROGRAM
Answers
Answer:
biatch java armstrong is what
Answer:
package com.company;
public class problem {
public static void main(String[] args) {
int num1=1;
int num2= 500;
for(int i=num1; i<=num2; i++) {
int rem, check, sum = 0;
check = i;
while (check != 0) {
rem = check % 10;
sum = sum + (rem * rem * rem);
check = check / 10;
}
if (sum == i){
System.out.println(""+i+" IS A ARMSTRONG NO.");
}
}
}
}
Explanation:
a no. is an Armstrong no. if the no. is equal to sum of cubes of its digits. Like 153. if we take 153 as 153 = (1*1*1) + (5*5*5) + (3*3*3). so in this way it is an Armstrong no.
the main logic of program is this:
"rem = check % 10;
sum = sum + (rem * rem * rem);
check = check / 10;"
rem denotes the remander, let you took the first integer as "1" then rem = 1
and then sum becomes = 1.