Sum of all the multiples of 3 or 5 below 1000 gives a wrong answer in C

前端 未结 7 1748
失恋的感觉
失恋的感觉 2020-12-22 05:27

Project Euler problem:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9

7条回答
  •  被撕碎了的回忆
    2020-12-22 05:58

    Direct translation of your python code:

    #include 
    
    int main(int argc, char *argv[])
    {
      int sum = 0;
    
      for (int x = 0; x < 1000; x++)
      {
        if (x % 5 == 0 || x % 3 == 0)
          sum += x;
      }
    
      printf("%d", sum);
    }
    

提交回复
热议问题