A little trouble with C programming please help!

A little trouble with C programming please help!
0.0 0

#1

I don’t know if it’s okay to post about C programming here.

But I’ve been stuck with this one problem my whole day.

And it’s making me frustrated that I want to punch thru the monitor :frowning:

What I’m trying to do is. input three numbers k , m , n.
and calculate the sum of all the multiples of k between m and n.

so it’s like m < multiple of k < n


int main (){

int i, k, m, n, sum = 0;

printf("Input 3 integers that are bigger than 1:   \n");
scanf("%d%d%d", &k, &m, &n);

for(i = 1; (i > m) && (i < n); ++i ){
    if(i % k == 0)
        sum += i;
}

printf("%d\n", sum);

return 0;

}


output:
Input 3 integers that are bigger than 1:
3
6
20
0Program ended with exit code: 0

please help :frowning:


#3

Thank you very much for your help :slight_smile:

I got a hint from your code.

And came up with this and it works.

#include <stdio.h>

int main (){

int i, k, m, n, sum = 0;

printf("Input 3 integers that are bigger than 1:   \n");

printf("input k:   ");
scanf("%d", &k);

printf("input m:   ");
scanf("%d", &m);

printf("input n:   ");
scanf("%d", &n);

for(i = ++m; i < n; ++i){
    if(i % k == 0)
        sum += i;
}

printf("%d\n", sum);

return 0;

}

Why wouldn’t C programming take this bold part right here:

for(i = 1; (i > m) && (i < n); ++i ){
if(i % k == 0)
sum += i;
}


#5

omg… I thought if

i was smaller than m, it would just keep getting bigger and bigger until it was finally bigger than m.

and start adding to the sum.

thanks!

you saved my life


#7

Awesome!

Thanks for your time and help.

Really Appreciate it.:slight_smile:


#8

I remain ever confused. Was gonna jump into C++ but found fcc instead.


#10

Yea. C++ has been on my mind for quite some time now. I really want to make games and after getting so far in javascript, it seems like a bad idea to try to learn C++ at the same time.