Thread: need a little help with ths problem

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    15

    need a little help with ths problem

    I need to compute the sum of 1 + 1 * 2 + 1 * 2 * 3.... n (n being the number). so if n was 5 it would be 1 + 1 * 2 + 1 * 2 * 3 + 1 * 2 * 3 * 4 + 1 * 2 * 3 * 4 * 5 (exiting at 5). so far I got this, but i need correction on my for loop because I think it is wrong.


    #include <stdio.h>
    main ()
    {
    long n, i, sum=0;
    printf ("enter number to be summed");
    scanf ("%d", &n);
    for (i=1; i <=n,i++)


    I'm not too sure what to put for the sum I was thinking maybe sum+= i * i but that doesn't look right. maybe i should set sum to 1? any help would be appreciated.

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Code:
    sum = 1;
    for (i=2; i <=n,i++) 
       sum += sum * i

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Okay, you have a program which needs to sum up the factorials. Here's my suggestion...

    Start up by just writing a program that will print them... here's some example output:
    Code:
    facsum.exe
    
    Enter a number to be summed: 5
    1
    2
    6
    24
    120
    Seriously, just write that program. Once you have that done, go back to your code, and at some point in it you will have a line that looks like this...
    Code:
    printf ("%d\n", num); // print the factorial
    Switch that line with something like
    Code:
    sum += num
    And viola, your program.
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    does my code work?

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    15
    nutshell's program was similar to the one i made, and it gave wrong values. question c gave good advice, but i need to do this problem with a for loop. I just can't think of a good way to go around it.

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    sum = 1;
    
    for(i = 2; i <= n; i++)
    {
    	sum2 = 1;
    
    	for(j = 2; j <= i; j++)
    	{
    		sum2 *= j;
    	}
    	sum += sum2;
    }

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    15
    nice monster, guess i should re-read the nested for loop crap again. thanks .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM