series problem stucked in the last step

This is a discussion on series problem stucked in the last step within the C Programming forums, part of the General Programming Boards category; I am facing some problems sloving this problem... Code: sum (2^i * i! ) that means user inputs an number ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    1

    Question series problem stucked in the last step

    I am facing some problems sloving this problem...

    Code:
    sum (2^i * i! )
    that means user inputs an number i then the program will calculate 2^i*i! and add the previous i values...

    This should be like this
    Code:
    2^1* 1! + 2^2*2! + 2^3*3! +........+2^n*n!

    i am almost solve to the third steps that is calculating ( 2^i * i ! )

    but in the last step i cannot add the previous values..i know what to do to get the result that i will need a variable x initialised to 0(zero) and which will increase until it is equal to i .
    that is
    Code:
    for (x=0;x <= i; i++)
    {
        x+= (previous value);
    }
    But i donot know where to put this and i donot know how to make a fuction with this loop..please help me out..i am pasting my existing code that is calculating up to (2^i * i! )

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int factorial(int i);
    int power( int base, int exp );
    
    main()
    {
        int n,i,b=2,s=0,multiply;
       	scanf("%d", &i);
    	for (n=1;n<=i;n++)
          {
              s=((int) factorial(n));
          }
        multiply = s*power( b, i );
        printf ("The Factorial of %d is\t     : %d\n2 to The Power of %d is\t     : %d\n(Factorial x Exponent) is    : %d\n",i, s,i, power( b, i ), multiply);
        getch();
    }
    
    int factorial(int i)
     {
      if (i<=1)
    	return(1);
      else
    	i=i*factorial(i-1);
    	return i;
     }
    int power( int base, int exp )
    {
    	int i, p = 1;
    	for( i = 1; i <= exp; i++ )
    	     {
                 p *= base;
             }
    	return p;
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    What your looking at is something like this

    Code:
    	for(n=1;n<=i;n++)
    	{
           s = factorial(n);
          
            multiply = s* pow( b, n );
            
            printf ("The Factorial of %d is\t     : %d\n\
                     2 to The Power of %d is\t     : %d\n\
                     (Factorial x Exponent) is    : %d\n",i, s,i, power( b, i ), multiply);
        }
    
    /* my output
    2
    The Factorial of 2 is        : 1
                     2 to The Power of 2 is      : 4
                     (Factorial x Exponent) is    : 2
    The Factorial of 2 is        : 2
                     2 to The Power of 2 is      : 4
                     (Factorial x Exponent) is    : 8
    
    */
    I could also see few other things which are you will have to improve in your coding practice. But if you get your head around why your didn’t work, perhaps i shall post the other. Now tell me why dint your work? Compare it with mine and yours.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    int sum = 0;    // cannot hold value more than INT_MAX defined in limits.h 
    int i ;
    // sum ( 2 ^ i * i! )
    for(i = 1; i <= n; i++) {
      sum += power(2,i) * factorial( i );
      // sum = sum + power(2,i) * factorial( i );
    }
    i,j,k are usually used for counter. Take note that for large value of n, integer may not be big enough.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Your thinking is wrong here:
    Code:
    for (x=0;x <= i; i++)
    {
        x+= (previous value);
    }
    You don't add the previous value to x.
    x already holds the partial sum of all the previous values, and here you now add the current term to it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

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