C example give ans

Discussion in 'C' started by atul55, Jan 13, 2011.

  1. atul55

    atul55 New Member

    Joined:
    Jan 13, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    how to print the series and it's sum
    1+(1+2)+(1+2+3)+(1+2+3+4)=20
    if user enters 4:confused:
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    I'd use a for loop within a for loop. The outer for would count from 1 to the number entered (so 1-4 if the user enters 4). The inner for would count from 1 to the outer loop variable (so if the outer loop variable is 3, then the inner loop would count from 1 to 3). Each time you display a number just add it to a running total. The rest is just printf/cout statements.

    Try it and see how far you get. This is an easy task.
     
  3. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    fill the dots and you have it

    Code:
    #include <stdio.h>
    
    int main(){
        int i,j,final,result=0;
        printf("\nenter final number for series:");
        scanf("%d",&final);getchar();
        for (i=1;...;i++){
            if (i>1)
                printf("(");
            for (j=1;...;j++){
                if (...)
                    printf("%d+",j);
                else
                    printf("%d",j);
                result+=j;
            }
            if (...)
                printf(")");
            if (...)
                printf("+");
        }
        printf("=%d",result);
        getchar();
        return 0;
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice