# C example give ans

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

1. ### atul55New Member

Joined:
Jan 13, 2011
Messages:
1
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 2. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,009
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. ### virxenNew Member

Joined:
Nov 24, 2009
Messages:
387
90
Trophy Points:
0
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;
}
```