Explanation :
If i=1000, then all the Armstrong numbers below 1000 will be printed.
(If n=153 then Armstrong = 1*1*1 + 5*5*5 + 3*3*3 i.e,
Armstrong =1+125+27=153.)
Code:
import java.util.*;public class Armstrong
{
public static void main(String[] args)
{
int n, count = 0, b, sum = 0;
System.out.print("Enter the Range Limit:");
Scanner sc=new Scanner(System.in);
int input=sc.nextInt();
System.out.print("Armstrong numbers from 1 to "+input+" are:");
for(int i = 1; i <= input; i++)
{
n = i;
while(n > 0)
{
b = n % 10;
sum = sum + (b * b * b);
n = n / 10;
}
if(sum == i)
{
System.out.print(i+" ");
}
sum = 0;
}
}
}
No comments:
Post a Comment