use for loop sum, courtship number and, odd sum, print narcissus number, statistics narcissus number
Package loop;
public class For1 {
public static void Main (string[] args) {
int sum=0;
for (int x=0;x<=100;x++) {
Sum=sum+x;
}
System.out.println ("within 100 (including 100) integers and:" +sum);
System.out.println ("-----------------------------------------");
int sumEven=0; for(int y=0;y<=100;y++) { if(y%2==0) { sumEven=sumEven+y; } } System.out.println("100以内(含100)偶数和:"+sumEven); System.out.println("-----------------------------------------"); int sumOdd=0; for(int m=0;m<=100;m++) { if(m%2!=0) { sumOdd=sumOdd+m; } } System.out.println("100以内(含100)奇数和:"+sumOdd); System.out.println("-----------------------------------------"); int number=0; System.out.println("输出所有的水仙花数:"); for(int f=100;f<1000;f++) { int g=f%10; int s=f/10%10; int b=f/10/10%10; if(f==g*g*g+s*s*s+b*b*b) { System.out.println(f); number=number+1; } } System.out.println("水仙花数共有:"+number+"个"); System.out.println("-----------------------------------------"); }
}
Java code: Use for loop sum, courtship number and, find odd and, print Narcissus number, statistics narcissus number