Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   C example give ans (http://www.go4expert.com/forums/c-example-ans-t24510/)

atul55 13Jan2011 11:43

C example give ans
 
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:

xpi0t0s 17Jan2011 15:09

Re: C example give ans
 
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 18Jan2011 06:46

Re: C example give ans
 
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;
}



All times are GMT +5.5. The time now is 10:05.