Thread: how to print SUM of the intergers from 2 to 30

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    how to print SUM of the intergers from 2 to 30

    Hi,

    how to calculate and print the SUM of the intergers from 2 to 30 using C

    Thanks in advanced

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    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):
    Code:
    int i;
    
    for(i=0; i < 10; i++){
      printf("%d\n", i);
    }
    Otherwise you could take a look at some nice formula for doing what you want.
    Last edited by edoceo; 03-17-2009 at 06:00 AM. Reason: /* slowpoke */ (2nd edit: typo)

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    2

    C Programming Tutorials

    I have uploaded various links on this blog related to everything on C, various tips and tutorials.
    http://c-programming-tutorials.blogspot.com

  5. #5
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    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

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Snafuist View Post
    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
    I'm 99% sure that the wanted solution is one that uses loops.

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

  7. #7
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by matsp View Post
    I'm 99% sure that the wanted solution is one that uses loops.
    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

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Snafuist
    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.
    Nah, since the sum is from 2 to 30... manually unrolling the loop completely might be the most naive approach to solve it
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Banned
    Join Date
    Mar 2009
    Posts
    5
    You have to use the Loops to calculate the su of these numbers.
    Example is already given by others.

  10. #10
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by laserlight View Post
    Nah, since the sum is from 2 to 30... manually unrolling the loop completely might be the most naive approach to solve it
    Hmm... in this case, manually unrolling the loop decreases the number of machine instructions by a factor of 2-3. I like this solution. But my favorite remains puts("464");

    Poor OP...

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Snafuist View Post

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. print output problem
    By chor in forum C Programming
    Replies: 1
    Last Post: 12-29-2002, 09:45 AM
  2. The long startup.
    By Sentaku senshi in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 08-10-2002, 11:52 PM
  3. How Can I print
    By kas2002 in forum C++ Programming
    Replies: 2
    Last Post: 06-22-2002, 10:07 AM
  4. Counting Number of days from year zero
    By wireless in forum C++ Programming
    Replies: 4
    Last Post: 06-16-2002, 07:31 AM
  5. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 11:50 PM