#include <stdio.h> int main() { int k,sum ; printf("number of terms in series = %d", k); scanf("%d",&k); sum = k*(k-1)/2; print | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

#include <stdio.h> int main() { int k,sum ; printf("number of terms in series = %d", k); scanf("%d",&k); sum = k*(k-1)/2; print

Whenever i am run it & enters any value of k. It only show no. Of terms =8

12th Aug 2019, 3:44 AM
Rahul Sikarwar
2 Answers
+ 2
Since you didn't initialize variable <k>, its value is unpredictable, also, you are printing the value of <k> before you read the input for it. Try to swap the line `printf("number of terms ...` with the `scanf("%d", &k);` line, then see if it helps. P.S. Your last line says `print` only, either the code is truncated or what I'm not sure. For future reference, put code in Description section (if it is small <= 10 lines). Or save the code in your profile and share the code link instead, if it's big (>= 10 lines, again in Description section).
12th Aug 2019, 4:11 AM
Ipang
+ 2
If this is an arithmetic series, shouldn't the sum be k*(k+1)/2 ?
12th Aug 2019, 4:27 AM
Sonic
Sonic - avatar