Thread: Absolutely stumped with running sum problem.

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    27

    Absolutely stumped with running sum problem.

    I've been at this for hours, and cant seem to get a grasp on what I should use here. The problem is that I have to add the numbers from 100 counting down, to equal 5005. For example 100+99+98+97 and so on. This problem is probably a lot easier to do than I'm making it out to be.

    I tried a do while loop, and several others, but I just dont know which loop to use. Here's my sorry excuse of a program, and if I could just get hints as to what I should use, that'll be appreciated. I want to still try to figure it out, so I'll learn:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    int sum = 0;
    int i = 100;
    
    do
    {
    sum = sum + i;
    i = i - 1;
    }
    while(sum < 5005);
    printf("%d  %d, ", i, sum);
    
    }
    Lol, you can see how stumped I was. XD Its supposed to print "100+99+98,...... equal 5005" listing all the integers counting down from 100.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Put your print sum line of code, inside the do while loop.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    For loop is compatible for this
    Try this ,
    Code:
    for ( ; sum <= 5005 ; sum =sum+i , i=i-1 )
    {
    printf("%d  %d, \n", i, sum);
    
    
    }

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    27
    Damn, it really WAS simple. XD I basically had the right ideas, just didnt work with the right loop functions. And here I thought I was good at FOR. XD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 09-22-2009, 11:38 AM
  2. Problem running a Cboard C++ tutorial!
    By Monkey Liar in forum C++ Programming
    Replies: 6
    Last Post: 02-15-2002, 03:32 AM
  3. ff7 running problem...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 11-22-2001, 08:02 PM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM
  5. problem , need pointers. thanks
    By mobius in forum C Programming
    Replies: 4
    Last Post: 09-03-2001, 05:12 AM