作业帮 > 综合 > 作业

编程求1到n中能被3或7整除的数之和.分别用for循环语句和while循环语句完成

来源:学生作业帮 编辑:百度作业网作业帮 分类:综合作业 时间:2024/04/30 05:40:06
编程求1到n中能被3或7整除的数之和.分别用for循环语句和while循环语句完成
编程求1到n中能被3或7整除的数之和.分别用for循环语句和while循环语句完成
int sum(int n)
{
    List<int> list = new List<int>();
    for(int x = 0; x <= n; x++)
    {
        if(x % 3 == 0  || x % 7 == 0)
        {
            list.Add(x);
        }
    }
    int x = 0;
    int result = 0;
    while(x < list.Count)
    {
        result += list[x++];
    }
    return result;
}