How can i make this code to give output sum of the Nth term of the series. EXAMPLE : input:2 ,output=-1, Input:3, output=2, | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

How can i make this code to give output sum of the Nth term of the series. EXAMPLE : input:2 ,output=-1, Input:3, output=2,

#include <stdio.h> int main () { int n, i,v=1,dif; printf("Enter Value: "); scanf("%d", &n); { printf("Result: "); for (i = 1; i < n; i++) { printf("%d,",i*v); v=v*-1; } printf("%d", i*v); } return 0; } Need the sum of this series

3rd Jul 2021, 6:32 AM
Siyam
Siyam - avatar
2 Answers
0
/* Siyam without loop (most efficient): https://code.sololearn.com/c3w78zdmZ0q4/?ref=app */ // sum of even numbers up to n: int m = n/2; int e = m*(m+1); // sum of odd numbers up to n: int o = m+(n&1); o = o*o; // sum of nth term of serie: int s = o-e;
3rd Jul 2021, 7:15 AM
visph
visph - avatar
0
Here https://code.sololearn.com/c96HDh3Dcd0z/?ref=app This may help you If you want to print only sum of series and not series itself then remove 'printf("%d", i*v)' from code.
3rd Jul 2021, 7:17 AM
Mayank Matwa