Ad
  • Custom User Avatar

    I have a problem with my code. It is my first assignment so maybe I'm doing something wrong with the return value. I really don't know. The program is written in C.

    ASSIGNMENT:
    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.

    Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.

    Note: If the number is a multiple of both 3 and 5, only count it once.

    CODE:

    int solution(int number) {
    // code here
    int k;
    scanf("%d", &k);
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int i = 0;
    int j = 0;
    int sum = 0;
    if(k % 3 != 0 && k % 5 == 0){
    a = k / 3;
    b = k / 5;
    for(i=1; i<=a; i++){
    c += 3 * i;
    //printf("c u koracima = %d\n", c);
    }
    for(j=0; j<b; j++){
    d += 5 * j;
    //printf("d u koracima = %d\n", d);
    }
    sum = c + d;
    }
    else if(k % 3 != 0 && k % 5 != 0){
    a = k / 3;
    b = k / 5;
    for(i=1; i<=a; i++){
    c += 3 * i;
    //printf("c u koracima = %d\n", c);
    }
    for(j=0; j<=b; j++){
    d += 5 * j;
    //printf("d u koracima = %d\n", d);
    }
    sum = c + d;
    }
    else if(k % 3 == 0 && k % 5 == 0){
    a = k / 3;
    b = k / 5;
    for(i=0; i<a; i++){
    c += 3 * i;
    //printf("c u koracima = %d\n", c);
    }
    for(j=0; j<b; j++){
    d += 5 * j;
    //printf("d u koracima = %d\n", d);
    }
    sum = c + d;
    }
    else{
    a = k / 3;
    b = k / 5;
    for(i=0; i<a; i++){
    c += 3 * i;
    //printf("c u koracima = %d\n", c);
    }
    for(j=1; j<=b; j++){
    d += 5 * j;
    //printf("d u koracima = %d\n", d);
    }
    sum = c + d;
    }
    //printf("a = %d\n", a);
    //printf("b = %d\n", b);
    //printf("c = %d\n", c);
    //printf("d = %d\n", d);
    //printf("sum = %d\n", sum);
    return sum;
    }

    Would you be so kind and review my code?
    What am I doing wrong?