Thread: Sumation Help

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    3

    Sumation Help

    I have this assingment:
    Write a C program that prompts the user to input the values of a0, an, and d, computes the value of the sum of the series and then displays the result. If the user inputs 1 for a0, 11 for an, and 2 for d, a typical output would look like:

    Sum(1,11,2)=36

    This is the Code that I put together but it doesn't work could someone out there help me out?? =) Thanks

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int A0, An, d, sum, x;
      printf("Enter A0, An, d : \n");
      scanf("%d %d %d",&A0,&An,&d);
      x=A0;
      sum=0;
      for(x=A0;x<=An;x+=d)
    	sum+=d;
      printf("Sum(%d,%d,%d)=%d\n",A0,An,d,sum);
    
    
      return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're doing your sum wrong. You're adding d to it, when you should be adding x.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    3
    So just change sum+=d to sum+=x ??

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    3
    AHH That worked!!! THANKS A BUNCH!!

Popular pages Recent additions subscribe to a feed