You are here:

C/c program

Advertisement


Question
QUESTION: C program that computes and prints out the sum of all natural numbers below 100000 that are multiples of 3,5 or 7.

ANSWER: Hi, Coco.

I prefer people to try the code for themselves to begin with, so I'll go over what operations you need to complete this, and if you can send your code back to me as a followup, I'll look it over.

First, you have to know how to execute a loop.  A simple for loop is exactly what you need in this circumstance.  The for loop looks like:

   for (initialization; condition; counter)
       body

The initialization section is executed once before the loop begins.  Then the condition is checked before every iteration of the loop.  Finally, after every iteration of the loop, the counter code is executed.  A simple way to loop from 1 to 10 is:

   int i;
   for (i = 1; i <= 10; ++i)
   {
       // do something
   }

In the above loop, i is initialized to 1 before the loop begins.  Then the loop continues to execute as long as i is less than or equal to 10.  After each iteration of the loop, i is incremented.

The next thing you need to know is how to test if a number is a multiple of another number.  You can determine this using the modulus operator (%).  The modulus operator, or mod, is similar to the divide operator, except that instead of telling you how many times a number will divide into another number, it will tell you the remainder after that number has finished dividing.  So, 10 % 3 would be 1, since 3 goes into 10 3 times, yielding the number 9, with a remainder of 1.  So to determine if a number is divisible by some other number, you simply take the modulus and compare it to 0 (since there would be no remainder if a number is perfectly divisible by another number):

   // Is 10 divisible by 3?
   if (10 % 3 == 0)

Finally, simply console output.  In C programming, the most common way of doing console output is to use the printf function.  To print an integer variable (called intVar) to the screen and add a new line character, you would type:

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

OK, those are the key pieces you need to complete this problem.  I've hinted at the algorithm, and I would like to see what you come up with.  Please attempt a solution and send your code back to me as a followup.  Even if it isn't right, I'll either show you what you've done wrong, or just give you the completed solution, whichever is easiest.  I just want you to try it for yourself before I give you the code outright.

If you have any further questions, please do not hesitate to ask.  I'm here to help. :)

---------- FOLLOW-UP ----------

QUESTION: Now this is ma code.
#include <stdio.h>
Int N, SUM=0;
Main()  {
For(int i=0, N<1000000, i++) {
If(N%3==0//N%5==0//N%7==0)
SUM=SUM+N;
Return 0;
}
Printf(“The sum is “+%d, SUM);

}


ANSWER: Hi, Coco.

You are very close in your attempt.  There are a few semantics issues, but your algorithm is spot on.  I've taken your code and refactored it a bit to correct for compile errors and good practice.  Here is the completed code (you'll see it doesn't differ much from your code):

#include <stdio.h>

void main()  
{
   int N, SUM = 0;
   
   for (N=0; N<1000000; N++)
   {
       if (N%3==0 || N%5==0 || N%7==0)
         SUM = SUM + N;
   }

   printf("The sum is %d", SUM);
}

The first difference is that I've moved where you declare N and SUM.  Where you declared them was legal, but not good practice.  You declared them as global variables, and they really ought to be local.  Next, you must specify a return type for the main function (actually, I think many compilers will assume an integer return type and will compile without one explicitly specified, but it's a good idea to always specify one).

The next change is in the declaration of the for loop.  You'll notice that you left the initializer as i, where it should be N in your code.  Same with the counter code.  In the if statement in the for loop, I believe you intended to use the boolean or (||) operator, but instead had a comment (//), so I corrected that.  Also, the return in the for loop is out of place.  If you want your main function to return an int, that's fine, but the return needs to be the very last thing in the function.  I just removed the return entirely and instead made the main function a void function.

Finally, your printf is malformed.  The %d needs to be inside the string.  The string is parsed and arguments (indicated by special character combinations such as %d, %f, or %s) are replaced with the values in the argument list after the string.  You were very close, just a tad bit off.

Good work in putting this together.  Like I said in the beginning, your algorithm was exactly right, there were just a few problems with the specifics of C syntax, and that's just something you pick up over time as you learn the language.  If you have further questions, please, do not hesitate to ask.  I am always here to help. :)

---------- FOLLOW-UP ----------

QUESTION: on compiling, it brings 2 warning saying that variable is long and on running the compiler stopped responding

Answer
What compiler are you using, and can you copy/paste the specific warnings?  What happens when you debug the application?  Is it simply taking a long time in the for loop?  I tested and verified the code on my own machine before sending it to you, so it should run just fine.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2012 About.com, a part of The New York Times Company. All rights reserved.