+ 2

What's wrong in this code? ,that's odd in C

#include <stdio.h> int main() { int a,i,b; int c=0; scanf("%d",&a); for(i=1;i<=a;i++) if(i%2==0) c=c+i; printf("%d",c); return 0; }

25th May 2021, 8:09 AM
srishti gupta
srishti gupta - avatar
11 Answers
+ 4
You are writing code to get the sum of even numbers, right? I ran the code in Code Playground. I removed b variable. I typed 4 as the input. The output is 6👍
25th May 2021, 8:12 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 2
But it's not working properly
25th May 2021, 8:19 AM
srishti gupta
srishti gupta - avatar
+ 2
#include <stdio.h> int main() { int a[10],i; int c=0; for(i=0;i<10;i++){ scanf("%d",&a[10]);} for(i=0;i<10;i++){ if(a[i]%2==0) c=c+a[i];} printf("%d",c); return 0; }
25th May 2021, 10:23 AM
srishti gupta
srishti gupta - avatar
+ 2
Yet all cases going wrong
25th May 2021, 10:23 AM
srishti gupta
srishti gupta - avatar
+ 2
Thanks to all of you... â˜ș
25th May 2021, 1:23 PM
srishti gupta
srishti gupta - avatar
+ 1
srishti gupta tumne scanf bss ek baar hi chalaya hai jo ki galat hai...... Kyunki agr user dher saare input de raha hoga tab ye program work nhi karega.....
25th May 2021, 9:53 AM
Sachin Verma
Sachin Verma - avatar
+ 1
srishti gupta Jo phla input diya jaa raha h program me to ye bata raha hai ki program me kitne no. Enter hone waale hai uske baad jitne no. Enter honge wo array me jaayenge aur jab array me input lenge tab scanf("%d",&a[i]); hoga aur phle for loop utne hi no. Input lega jitne no. Usko phle input me bataye gaye the
25th May 2021, 10:37 AM
Sachin Verma
Sachin Verma - avatar
+ 1
#include <stdio.h> int main() { int a,c=0,i; int b[100]; scanf("%d",&a); for(i=0;i<a;i++) { scanf("%d",&b[i]); } for(i=0;i<a;i++) { if(b[i]%2==0) { c=c+b[i]; } } printf("%d",c); return 0; }
25th May 2021, 10:39 AM
Sachin Verma
Sachin Verma - avatar
+ 1
Actually your code is correct in but u created one variable b which is not used anywhere in program that why you getting some warnings. May be on other ide it will work fine.
25th May 2021, 12:54 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
Hello
25th May 2021, 2:59 PM
Rakesh Choudhury
Rakesh Choudhury - avatar
+ 1
The final sum should be printed outside the for loop, the rest conditions are correct
27th May 2021, 4:44 AM
Tarun R Jain
Tarun R Jain - avatar