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

How to count a sum, when the numbers are read from stdin and stored into a structure?

1 Answers  


Write a program to receive an integer and find its octal equivalent?

7 Answers  


1. const char *a; 2. char* const a; 3. char const *a; -Differentiate the above declarations.

3 Answers  


how to create a 3x3 two dimensional array that will give you the sums on the left and bottom columns

0 Answers  


write a simple calculator c program to perform addition, subtraction, mul and div.

0 Answers   United Healthcare, Virtusa,






How can u say that a given point is in a triangle? 1. with the co-ordinates of the 3 vertices specified. 2. with only the co-ordinates of the top vertex given.

1 Answers  


&#8206;#define good bad main() { int good=1; int bad=0; printf ("good is:%d",good); }

2 Answers  


write a c program to Create a registration form application by taking the details like username, address, phone number, email along with password and confirm password (should be same as password).Ensure that the password is of 8 characters with only numbers and alphabets. Take such details for 5 users and display the details. In place of password display “****”. (Use Structures).

0 Answers   CDAC, College School Exams Tests,


Set up procedure for generating a wire frame display of a polyhedron with the hidden edges of the object drawn with dashed lines

0 Answers   IBM,


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

25 Answers   EA Electronic Arts, Google, Motorola,


main() { main(); }

1 Answers  


find A^B using Recursive function

2 Answers  


Categories