• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need help in oldschool C programming

Jimmah

Golden Member
I have an assignment, idea is to use 100 and increment it with 1, then check to see if it divides with 3 and 4, outputting if either do or do not divide evenly, stopping the program once a number is reached that both divide into.

Problem is, I'm clueless where to start. I understand I need to use 'for' then 'if', but I'm unsure how to word it (and wth is % exactly? the book doesn't touch on it much - or at all that I can find - so I'm very frustrated with it atm).

I can program increments, and silly stuff like that, just can't figure out for the life of me how to divide by two things and output the needed lines depending on if its true or false.


*not looking for the program written, just some pointers or a direction where to look*

Thanks for any help 🙂
 
The % operator is the modulo operator for C/C++/Java/C#. When referring to it we usually say "x mod y". The operation provides the remainder of a division, so 10 % 2 == 0, 10 % 5 == 0, 10 % 3 == 1, and 10 % 4 == 2.

So your algorithm can simply loop, incrementing the test value on each iteration, and if the result of x % 3 and x % 4 are both 0, do whatever you do on success. Here's a snip...

int test = 100;
while ( test % 3 != 0 || test % 4 != 0 )
test++;
printf( "%i", test );

 
Back
Top