求1到100所有偶数的和,为什么while (b<=100)为什么无法运行:?

求1到100所有偶数的和,为什么while (b<=100)为什么无法运行:?

int main()
{
    system("color 0a");
    int a,b;
    a=0;
    b=2
    while (b<=100)
    {
        if (b%2==0 || b%10==2)
        {
            a=a+b;
        }
        b=b+1;
    }
    printf("%d",a);
   
    return 0;
}


第6行b=2后面少了个分号,编译时会报错,补上分号,然后加上所需的各函数头文件即可。

修改如下:

参考链接:

#include <stdio.h>
#include <stdlib.h> 

int main()
{
    system("color 0a");
    int a,b;
    a=0;
    b=2;  //此行后面补上分号即可 
    while (b<=100)
    {
        if (b%2==0 || b%10==2)
        {
            a=a+b;
        }
        b=b+1;
    }
    printf("%d",a);
   
    return 0;
}
 
 

img

你少个分号哈,把分号补上就行了,望采纳

#include <stdio.h>
#include <stdlib.h> 
int main()
{
    system("color 0a");
    int a,b;
    a=0;
    b=2;  //此行后面补上分号即可 
    while (b<=100)
    {
        if (b%2==0 || b%10==2)
        {
            a=a+b;
        }
        b=b+1;
    }
    printf("%d",a);
   
    return 0;
}
 
 
 

两处问题,见注释,供参考:

#include <stdio.h>
int main()
{
    system("color 0a");
    int a,b;
    a=0;
    b=1;  // 这句缺了分号 ';'  ,既然是 1100,所以b = 1;
    while (b<=100)
    {
        if (b%2 == 0)//if (b%2==0 || b%10==2) 偶数只要 b%2==0就可以了
        {
            a=a+b;
        }
        b=b+1;
    }
    printf("%d",a);
    return 0;
}