Java算法——求1到100的偶数和,奇数和


1、求1到100的偶数和

int sum = 0;
for (int i = 2; i <= 100; i += 2) {
sum += i;
}
System.out.println(“1到100的偶数和为:” + sum);


2、求1到100的奇数和

int sum = 0;
for (int i = 1; i <= 100; i += 2) {
sum += i;
}
System.out.println(“1到100的奇数和为:” + sum);

点赞