How to print all armstrong numbers between 1 to 500 in Java

1 Answer

0 votes
public class MyClass {
    public static int armstrong(int n) {
        int result = 0, remainder;  
         
        while(n > 0) {  
            remainder = n % 10;  
            n = n / 10;  
            result += remainder * remainder * remainder;  
        }  
        return result;
    }
    public static void main(String args[]) {
        // 153 = 1*1*1 + 5*5*5 + 3*3*3 = 153 = armstrong
        
        for (int i = 1; i <= 500; i++)
            if (i == armstrong(i))  
                System.out.println(i);   
    }
}
  
  
  
/*
run:
  
1
153
370
371
407
  
*/

 



answered Aug 21, 2021 by avibootz

Related questions

1 answer 112 views
1 answer 183 views
1 answer 144 views
1 answer 220 views
1 answer 273 views
...