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

main() { printf("%x",-1<<4); }

3 Answers   HCL, Sokrati, Zoho,


main() { register int a=2; printf("Address of a = %d",&a); printf("Value of a = %d",a); }

3 Answers  


Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution)

7 Answers   Microsoft,


Extend the sutherland-hodgman clipping algorithm to clip three-dimensional planes against a regular paralleiepiped

1 Answers   IBM,


To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates. String contains only lowercase characters ['a'-'z']

0 Answers  






given integer number,write a program that displays the number as follows: First line :all digits second line : all except the first digit . . . . Last line : the last digit

8 Answers  


main() { struct date; struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }

1 Answers  


void main() { int x,y=2,z; z=(z*=2)+(x=y=z); printf("%d",z); }

4 Answers  


can u give me the c codings for converting a string into the hexa decimal form......

1 Answers  


Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.

1 Answers  


program to find the roots of a quadratic equation

14 Answers   College School Exams Tests, Engineering, HP, IIIT, Infosys, Rajiv Gandhi University of Knowledge Technologies RGUKT, SSC,


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

1 Answers  


Categories