java循环练习:水仙花数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package  practiceGO;
/*
  *5、算水仙花数(100-999):表示三位数的数字,个位的三次方+十位的三次方+百位的三次方=这个数本身 
  */
public  class  Cto {
     public  static  void  main(String[] args) {
         int  first,second,third;
         for ( int  i= 100 ; i<= 999 ; i++){
             first = (i/ 100 );
             second = (i-first* 100 )/ 10 ;
             third = i% 10 ;    
             if  (Math.pow(first,  3 )+Math.pow(second,  3 )+Math.pow(third,  3 ) == i) {
                 System.out.println(i+ "是水仙花数" );
             }
         }
     }
}

运行结果:

1
2
3
4
153 是水仙花数
370 是水仙花数
371 是水仙花数
407 是水仙花数



本文转自yeleven 51CTO博客,原文链接:http://blog.51cto.com/11317783/1762315

你可能感兴趣的:(java)