+ 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; }
11 ответов
+ 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👍
+ 2
But it's not working properly
+ 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;
}
+ 2
Yet all cases going wrong
+ 2
Thanks to all of you... ☺
+ 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.....
+ 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
+ 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;
}
+ 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.
+ 1
Hello
+ 1
The final sum should be printed outside the for loop, the rest conditions are correct