Tuesday, June 8, 2010

Project Euler Problem 1 in C

    I enjoy the Project Euler problems. I've never asked for help even though I've struggeled a lot at them. Even still I've only done ~10 in my preferred language, C++. I'm now learning a little C and I've decided that Project Euler would be a great place to practise what I've learned. So here's my solution to problem 1 in C:



#include <stdio.h>

int main(void)
{
int total=0;
int i=0;

while(i<1000)
{
if(i%3==0 || i%5==0)
total+=i;
i++;
}

printf("%d", total);
}

Execution time: 0.042 s


    That's all then. I'll post up my solutions to the other problems as I finish them. All comments welcome.
       NotMyFault

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. The correct answer is 233168. According to your source we will not get correct output using int data type. :) You can check at Project Euler site. I have a solution for it. I like your site. it is really helpful for us. thanks.
    visit:
    http://c-programming-practice.blogspot.com/2013/06/sum-of-all-multiples-of-3-or-5-below.html

    ReplyDelete
  3. but this is working for only a single test case (T)

    ReplyDelete