+ 7

How to print even numbers in an array?

#include<stdio.h> int main() { int a[20],i; printf("enter array elemty"); for(i=0;i<=20;i++) { scanf("%d",&a[i]); } if(a[i]/2==0) { for(i=0;i<=20;i++) { printf("%d",a[i]); }} Return 0; }

6th Jul 2020, 3:19 AM
Navneet Kaur💫
Navneet Kaur💫 - avatar
45 Respuestas
+ 3
Navneet It is needed to print 20 also as even number then start loops with 1 and have same condition. It will need to change all index of array a[ ]. Array indexes with a[ I ] to a[ I - 1] This change should solve the required.
6th Jul 2020, 10:21 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
+ 3
main() { int i; int a[100]={10,20,30,23,44,80}; for(i=0;i<a[i];i++) if(a[i]%2==0) printf("%d\n",a[i]); }
16th Jul 2020, 1:53 PM
VAMSI
VAMSI - avatar
+ 2
Dhananjay Patel I've done soo but since then output is not correct
6th Jul 2020, 8:11 AM
Navneet Kaur💫
Navneet Kaur💫 - avatar
+ 2
~ swim ~ If even numbers between 0 .. N needs to be stored in array array size need not to be N. It requires more information. At First look it seems program tries to get number from console using scanf. 1. Problem statement is read even numbers from 0 .. N including N. Or 2. Find from N = 20 numbers all even numbers. It requires another variable to keep track of only even numbers and add them to array.
6th Jul 2020, 12:34 PM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
+ 1
Navneet It need to change only statement if (a[I]/2 == 0) { ... } By following statement if (a[I]%2 == 0) { ... } Instead of division operator use modulus operator To find even number remainder should be 0 (Zero)
6th Jul 2020, 3:33 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
+ 1
Vamsi How can I use colon after if statement
7th Jul 2020, 4:29 AM
Navneet Kaur💫
Navneet Kaur💫 - avatar
+ 1
Vamsi I've not learned python till now soo I don't have any idea about that
7th Jul 2020, 4:38 AM
Navneet Kaur💫
Navneet Kaur💫 - avatar
+ 1
Navneet Attachment is code to find even numbers from input it prints all even number in array It first read input number count after that it reads all numbers after that it finds all even numbers and print on output https://code.sololearn.com/cWOpU8cc83cb/?ref=app
7th Jul 2020, 6:49 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
+ 1
Navneet Program written is now error free and non-prone to crash. It good work. For better programming use indentations properly so program will be more readable. Form current program learn it will take 19 number form input. Make change so that it can change input from 19 to 20. Hint. Array index starts from 0 onwards
7th Jul 2020, 10:40 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
+ 1
/*how to rpint even numners in an array*/ #include<stdio.h> int main() { int n; printf("enter number of elements in an array"); scanf("%d",&n); int a[n]; printf("enter array elements "); for(int i=0;i<n;i++) { scanf("%d",&a[i]); } for(int i=0;i<n;i++) { if(a[i]%2==0) { printf("%d",a[i]); } } return 0; }
7th Jul 2020, 2:03 PM
Yamini Angel
Yamini Angel - avatar
+ 1
Using the output statement of that language 😂
7th Jul 2020, 5:17 PM
Rahid Ahmed
Rahid Ahmed - avatar
+ 1
Arda Atıcı ok thanks for suggesting...I'll definitely try it
10th Jul 2020, 11:05 AM
Navneet Kaur💫
Navneet Kaur💫 - avatar
+ 1
VAMSI ok thanks
16th Jul 2020, 3:45 PM
Navneet Kaur💫
Navneet Kaur💫 - avatar
0
Oh yah! I've just lost that ...thanks alot
6th Jul 2020, 3:56 AM
Navneet Kaur💫
Navneet Kaur💫 - avatar
0
As I want to print even number upto 20 and 20is also an even num
6th Jul 2020, 8:02 AM
Navneet Kaur💫
Navneet Kaur💫 - avatar
0
Swim Okkk .... But can you just please make this program and show me the output.
6th Jul 2020, 8:07 AM
Navneet Kaur💫
Navneet Kaur💫 - avatar
0
Okk
6th Jul 2020, 8:25 AM
Navneet Kaur💫
Navneet Kaur💫 - avatar
0
Dhananjay I've make program ,it was error free and compiled also but the output is not correct😔,I don't know why🤔. #include<stdio.h> #include<conio.h> int main() { int a[20],i; for(i=1;i<=20;i++) { if(a[i]%2==0) { printf("%d",a[i]); } } return 0; } Just see.....
6th Jul 2020, 11:43 AM
Navneet Kaur💫
Navneet Kaur💫 - avatar
0
Navneet here is the code you needed hope it helps you. just run this and if u want u can change range from 20 to any number in the for loop. https://code.sololearn.com/c323do61U90z/?ref=app
6th Jul 2020, 6:42 PM
Varun N
Varun N - avatar
0
Varun N Thanks for your help but the my question is how to print even numbers in an array ,I'm not able to print then in array....simply I can also print.
7th Jul 2020, 3:10 AM
Navneet Kaur💫
Navneet Kaur💫 - avatar