Under linux environment can u please provide a c code for
computing sum of series 1-2+3-4+5......n terms and
-1+2-3+4-5...n terms..

Answers were Sorted based on User's Feedback



Under linux environment can u please provide a c code for computing sum of series 1-2+3-4+5......n ..

Answer / sudhanshu_kmr

#include<stdio.h>

int main()
{

int i,n,sum=0;
printf("\n Enter the n: " );
scanf("%d",&n);

for(i=1;i<=n;i++)
{
if(i%2==0) /* for series is -1 +2 -3.......*/
sum= sum+i;
else
sum = sum-i;
}

printf(" Sum of %d terms is %d \n",n,sum);

return 0;
}



/* For series 1 - 2 +3 ....
then use

for(i=1;i<=n;i++)
{
if(i%2==0)
sum= sum -i;
else
sum = sum +i;
}

*/

Is This Answer Correct ?    5 Yes 1 No

Under linux environment can u please provide a c code for computing sum of series 1-2+3-4+5......n ..

Answer / sai prasad raju

I will not tell u.
Why should i tell?
off

Is This Answer Correct ?    1 Yes 1 No

Post New Answer

More C Code Interview Questions

Write a program that reads a dynamic array of 40 integers and displays only even integers

2 Answers  


C statement to copy a string without using loop and library function..

2 Answers   Persistent, TCS,


String copy logic in one line.

11 Answers   Microsoft, NetApp,


#include<stdio.h> main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d..%d",*p,*q); }

1 Answers  


What is the hidden bug with the following statement? assert(val++ != 0);

1 Answers  






write a program in c language to get the value of arroy keys pressed and display the message which arrow key is pressed?

1 Answers  


main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); }

6 Answers  


main() { unsigned int i=10; while(i-->=0) printf("%u ",i); }

2 Answers   HP,


/*what is the output for*/ void main() { int r; printf("Naveen"); r=printf(); getch(); }

4 Answers  


#include<conio.h> main() { int x,y=2,z,a; if(x=y%2) z=2; a=2; printf("%d %d ",z,x); }

1 Answers  


main() { int i = 3; for (;i++=0;) printf(ā€œ%dā€,i); }

1 Answers   CSC,


Predict the Output: int main() { int *p=(int *)2000; scanf("%d",2000); printf("%d",*p); return 0; } if input is 20 ,what will be print

2 Answers  


Categories