找到3所有倍数低于1000的总和或5 [英] Find the sum of all the multiples of 3 or 5 below 1000

查看:242
本文介绍了找到3所有倍数低于1000的总和或5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们列出所有低于10是3或5的倍数的自然数,我们得到3,5,6和9这些倍数的总和是23。
我有以下的code,但答案不匹配。

 #包括LT&;&stdio.h中GT;
诠释的main()
{
    长期无符号整型我,总和= 0;
    clrscr();
    对于(i = 0; I< = 1000;我++)
    {
        如果((I%5 == 0)||(I%3 == 0))
        {
            总和=总和+ 1;
        }
    }
    的printf(%d个\\ N,总和);
    的getchar();
    返回0;
}


解决方案

两件事情:


  • 你的包括的1000环,和

  • 你每次加一的总和,而不是值本身。

循环改为

 为(i = 0; I< 1000;我++)

和之行

 总和=总和+ I;

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. I have the following code but the answer does not match.

#include<stdio.h>
int main()
{
    long unsigned int i,sum=0;
    clrscr();
    for(i=0;i<=1000;i++)
    {
        if((i%5==0)||(i%3==0))
        {
            sum=sum+1;
        }
    }
    printf("%d\n",sum);
    getchar();
    return 0;
}

解决方案

Two things:

  • you're including 1000 in the loop, and
  • you're adding one to the sum each time, rather than the value itself.

Change the loop to

for(i=0;i<1000;i++)

And the sum line to

sum=sum+i;

这篇关于找到3所有倍数低于1000的总和或5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆