C example give ans

atul55's Avatar, Join Date: Jan 2011
Newbie Member
how to print the series and it's sum
1+(1+2)+(1+2+3)+(1+2+3+4)=20
if user enters 4
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
virxen's Avatar, Join Date: Nov 2009
Pro contributor
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;
}