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

What are segment and offset addresses?

2 Answers   Infosys,


void main() { static int i; while(i<=10) (i>2)?i++:i--; printf(“%d”, i); }

2 Answers  


Find your day from your DOB?

15 Answers   Accenture, Microsoft,


What is the problem with the following code segment? while ((fgets(receiving array,50,file_ptr)) != EOF) ;

1 Answers  


Write a routine to implement the polymarker function

0 Answers   TCS,


main() { int i=5; printf(“%d”,i=++i ==6); }

1 Answers  


create a login program that ask username and password. if you input username or password 3 times wrong, the program will terminate else the program will prompt a message "congratulations"

2 Answers  


write a program in c to merge two array

2 Answers  


You are given any character string. Find the number of sets of vowels that come in the order of aeiou in the given string. For eg., let the given string be DIPLOMATIC. The answer returned must be "The number of sets is 2" and "The sets are "IO and AI". Vowels that form a singleton set must be neglected. Try to post the program executable in gcc or g++ or in java.

3 Answers  


Give a oneline C expression to test whether a number is a power of 2?

25 Answers   EA Electronic Arts, Google, Motorola,


Program to find the largest sum of contiguous integers in the array. O(n)

11 Answers  


Is the following statement a declaration/definition. Find what does it mean? int (*x)[10];

1 Answers  


Categories