//Generate Amstrong Number upto limit import java.io.*; class AmstrongNum { int sum=0,rem,t; void check(int num) { System.out.println("The following numbers are amstrong numbers:"); for(int i=1;i<num;i++) { t= i; while( t !=0 ) { rem= t % 10; sum = sum + rem*rem*rem; t = t/10; } if( sum == i) System.out.println(i+"\n"); sum=0; } } public static void main(String args[])throws IOException { AmstrongNum am= new AmstrongNum(); int n; BufferedReader b= new BufferedReader(new InputStreamReader(System.in)); System.out.println(" Enter the range upto which u want to find amstrong numbers:"); n=Integer.parseInt(b.readLine()); am.check(n); } }
Output: |
---|
Enter the range upto which u want to find amstrong numbers: |
1000 |
The following numbers are amstrong numbers: |
1 |
153 |
370 |
371 |
407 |