Printing Partial Sums

This is a discussion on Printing Partial Sums within the C Programming forums, part of the General Programming Boards category; Trying to print something like this: 1 sum = 1 1 2 sum = 3 etc...all the way too 1 ...

  1. #1
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72

    Printing Partial Sums

    Trying to print something like this:

    1 sum = 1

    1 2 sum = 3

    etc...all the way too

    1 2 3 4 5 6 7 8 9 10 sum = 55

    I dont know how to make it add up the numbers.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
        int a, b,
            sum = 0;
    
          for(a = 1; a <= 10; a++)
            for(b = 1; b <= 10; b++)
    
            if(b == a)
            {
                printf("%d\n", b);
                    break;
            }
            else
                printf("%d", b);
    
    }
    White now I am getting is just.

    1
    12
    123
    etc...

    I know there should be a sum somewhere just dont know where.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    is this just the summation ("Sigma") from 1 to 10? if so then can you just use the mathematical equation to do this without looping? google something like summation identities, to find what it is.

    EDIT: if you cant do it this easier and more efficient way, i.e. youre required to use looping for an assignment or something, then theres no way around that (obviously). if this is the case, then youre outer loop is correct but your inner loop is not. hopefully the following pseudocode helps:
    Code:
    for outer loop a=1
      inner loop b does 1 = 1
    
    for outer loop a=2
      inner loop b does 1 + 2 = 3
    
    for outer loop a=3
      inner loop b does 1 + 2 + 3 = 5
    
    ...
    
    for outer loop a=10
      inner loop b does 1 + 2 + ... + 10 = 55
    hopefully you see the pattern and understand it.
    Last edited by nadroj; 10-04-2009 at 10:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 02:13 AM
  2. Printing Lines to .txt File
    By Programmer3922 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 01:45 PM
  3. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 03:20 AM
  4. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM
  5. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21