+ 2

What's wrong in this prog? it's even no adder prog....

include <stdio.h> int main() { int n,sum=0,i=1; printf("\n How many elements u want:"); scanf("%d",&n); int a[n]; printf("\n enter array elements"); for(i=1;i<=n;i++) { scanf("%d\n",&a[i]); } for(i=1;i<=n;i++) { if(a[i]%2==0) sum=sum+a[i]; else sum=sum; } printf("value of sum is %d",sum); return 0; }

30th Apr 2020, 10:51 AM
Sakshi Pharande
Sakshi Pharande - avatar
18 Réponses
+ 1
Sakshi Pharande if you wanna use the type of input like in pc you can do it in dcoder or geniee app available on Play Store
2nd May 2020, 5:03 AM
Reca Resu
Reca Resu - avatar
+ 3
Sakshi Pharande The input system here (SoloLearn) is not like the one in our system. remove else sum = sum it's doing nothing. # is missing in include<stdio.h>
30th Apr 2020, 11:05 AM
minirkk
minirkk - avatar
+ 2
@RKK I already said the program is working right ,you can even start with i=1,i<=n or I=0,i<n that is not the cause of error
30th Apr 2020, 11:10 AM
Abhay
Abhay - avatar
+ 2
Since I don't know the error ,I will assume you forgot to put hash # sign before include ,
30th Apr 2020, 11:11 AM
Abhay
Abhay - avatar
+ 1
It is working right Input 4 1 2 3 4
30th Apr 2020, 10:56 AM
Abhay
Abhay - avatar
+ 1
but it's not working for i/p 9 1 2 3 4 5 6 7 8 9
30th Apr 2020, 11:01 AM
Sakshi Pharande
Sakshi Pharande - avatar
+ 1
You can't enter the numbers that way ,each input has to be in next line
30th Apr 2020, 11:01 AM
Abhay
Abhay - avatar
+ 1
yes, I imputed it by using enter key....but it's not working
30th Apr 2020, 11:02 AM
Sakshi Pharande
Sakshi Pharande - avatar
+ 1
What error you are getting?
30th Apr 2020, 11:03 AM
Abhay
Abhay - avatar
+ 1
What error you are getting? use this code for "That's odd" in code coach in sololearn coding....
30th Apr 2020, 11:09 AM
Sakshi Pharande
Sakshi Pharande - avatar
+ 1
I know but value are being enters from 1 so it doesn't matter if they are retrieved in correct order with starting from 1 not 0 ,so there aren't any errors in the program ,but I am not into c so i believe you maybe right
30th Apr 2020, 11:26 AM
Abhay
Abhay - avatar
1st May 2020, 3:48 AM
Nikhil Maroju
Nikhil Maroju - avatar
0
Remove \n from scanf scanf("%d", &a[i]) ; Now you can enter in any way.. Edit: Add # #include
30th Apr 2020, 11:11 AM
Jayakrishna 🇮🇳
0
Put # before include<studio.h>
30th Apr 2020, 11:46 AM
Neha Parashari
Neha Parashari - avatar
0
For(I=0;I<n;I++);//use this Else Sum=sum;//delet this 2 line
1st May 2020, 2:28 PM
Ash Akshay
Ash Akshay - avatar
0
Please correct the first line. Type # before include<stdio.h> And there should be space between "include" and <stdio.h> Remove "else" part in if condition
2nd May 2020, 3:04 AM
Dasarath Singh
0
yes! it successfully worked thanks to everyone for helping....
2nd May 2020, 4:21 AM
Sakshi Pharande
Sakshi Pharande - avatar
0
if you use the "java" language. In your program. The size of the array is "n" The for loop is looping from 1 to n We know that array index start from "0". And your for loop is reaching " n+1" elements of the array "a". Which does not exist. Which is beyond the size of the array " a". So my advice is to start your variable "i" with 0.
2nd May 2020, 8:36 AM
Dasarath Singh