1.

Write a C program to compute the sum of first n terms (n ≥ 1) of the following series using ‘for’ loop.1 – 3 + 5 – 7 + 9 - ……

Answer»

A C program to compute the sum of first n terms of the series 1-3+5-7+9-… is listed below:

main()

{

int start=1,i=1,no=0,sum=0;clrscr();

printf ("\nEnter number of terms to be added:-> ")

scanf("%d",&no);

for (i=1;i<=no;i++)

{

if (i%2!=0)

 {

sum+=start;

if (i==1)

printf ("%d",start);

else

printf ("+%d",start);

}

else

{

sum-=start;

printf ("-%d",start);

}

start+=2;

}

printf ("=%d",sum);

getch();

}



Discussion

No Comment Found

Related InterviewSolutions