Hi,
how to calculate and print the SUM of the intergers from 2 to 30 using C
Thanks in advanced
Hi,
how to calculate and print the SUM of the intergers from 2 to 30 using C
Thanks in advanced
What have you done so far?
We will not write the code for you (that's against this forums rules, if nothing else).
But if you post what you have done, and explain what, more specifically, you can't get your head round, we can probably guide you in the right direction.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
There are many ways of doing this. if you are looking at a school-assignment your teacher might want you to read on about loops and such. Just picture how you would do this math in your head, and then write your thoughts down in code.
example of a simple for-loop (that prints the numbers from zero to nine):
Otherwise you could take a look at some nice formula for doing what you want.Code:int i; for(i=0; i < 10; i++){ printf("%d\n", i); }
Last edited by edoceo; 03-17-2009 at 06:00 AM. Reason: /* slowpoke */ (2nd edit: typo)
I have uploaded various links on this blog related to everything on C, various tips and tutorials.
http://c-programming-tutorials.blogspot.com
You can use a loop, or more efficiently the Gaussian Sum Theorem (or Gaussian Formula, or whatever it is called in English; the English Wikipedia doesn't seem to have an entry for that), which states that n*(n+1)/2 equals the sum of all integers from 1 to n.
Greets,
Philip
All things begin as source code.
Source code begins with an empty file.
-- Tao Te Chip
Me too. I mentioned it because this problem frequently appears in exercise sheets and one is never told that a loop is actually the most naive approach to solve it. There are programmers which I consider to be more experienced than me who do it wrong, e.g. beej from "beej's Guide to Network Programming". It's a shame, because there are actually some nice problems to get used to loops, e.g. factorials (replace "+" with "*") and primality tests. There's no need to teach bad habits.
As we're off-topic now:
What is the "Gaussian Sum Theorem" called in English?
Why are there occasionally two dots on the "ï" in "naïve"? Does it affect meaning, or pronunciation?
The German word "naiv" is simply polite for "stupid".
Greets,
Philip
All things begin as source code.
Source code begins with an empty file.
-- Tao Te Chip
Nah, since the sum is from 2 to 30... manually unrolling the loop completely might be the most naive approach to solve itOriginally Posted by Snafuist
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You have to use the Loops to calculate the su of these numbers.
Example is already given by others.
All things begin as source code.
Source code begins with an empty file.
-- Tao Te Chip
Gauss Sum or Gaussian Sum:
http://en.wikipedia.org/wiki/Gaussian_sum
There are no two dots in English, but I have seen the two dots as a pronunciation helper, because the a is silent, and the i and e sounds are so strong.
Stupid would be a bit too strong for naive. Naive might be smart, but it's what you think about something *before* you have a more in-depth knowledge of the matter.
If you still have the same naive idea to the subject, after a longer investigation and study, then you'd be stupid, politely phrased.