程序1:for语句实现
#include
void main()
{
int i;
for(i=1;i<=100;i++)
{if(i%2==0)
printf ("%d\n",i);
}
}
程序2:while语句实现
#include
void main()
{
int i=1;
while(i++<=100)
{if(i%2==0)
printf ("%d\n",i);
}
}
程序3:do....while 语句实现
#include
void main()
{
int i=1;
do
{if(i%2==0)
printf ("%d\n",i);
}while(i++<=100);
}
public class oushu {
public static void main(String[] args) {
int sum=2;
for(int i=3;i<=1000;i++){
if(i%2==0){
sum+=i;
}
}
System.out.println(sum);
}
}
public class 求和
{
public static void main(String args[])
{
int sum=0;//sum 为偶数之和
for(int i=1;i<=1000;i++)
{
if(i%2==0)
{
sum=sum+i;
}
}
System.out.printf("偶数之和为:%d 个",sum);
}
}
public class Sum{
public static void main(String args[]){
int sum=0;
for(int i=2;i<=1000;i++){
if(i%2=0)
sum+=i;
}
System.out.println(sum);
}
}