 How to print even numbers in an array? | SoloLearn: Learn to code for FREE!

+8

# How to print even numbers in an array?

#include<stdio.h> int main() { int a,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; }

+7

Few logical errors in the code 1. The last valid index of an array is length of array -1 (here 19). So exit condition in both for loops should be i < 20 instead of i<=20 since indexing starts fron 0. Here your are indexing beyond the array bounds and which can lead to crash or undefined behavior. 2. value of variable 'i' at the end of scanf loop is 21, so checking if(a[i]/2==0) is wrong due to point 1 above. 3. if(a[i]/2==0) should be if(a[i]%2==0) and it's placement is wrong, as you need to check if a number is even or not for every element in the array, so this check should be inside the second for loop.

+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.

+2

That is fine but then your array size should be 21 or do not consider 0

+2

Dhananjay Patel I've done soo but since then output is not correct

+2

No i will not write code for you. I have exlplained what needs to be done in my comment. If something is not clear ask me again.

+2

Where is scanf statement? You are accessing uninitialized array. If you just want to print even numbers from 0 to 20 then you can totally remove the array, just use simple counting. If you insist on using array then first initialize it. And as for array size, no use of telling anything. Man understand this stuff properly. Array indexing starts from 0. The last valid index in any array is no of elements - 1, so if N = 20 then arr[N-1] is your last array element, the array still stores N number of elements. Since you want to process 20, then your array size must be 21 i.e N = 21 Starting from 1 will not change anything. Your code will still access array index beyond it's boundary, on the contrary you will end up wasting cpu cycles as now you need to do if(a[i - 1] % 2 == 0) // unnecessary subtraction and printf("%d", a[i - 1]); otherwise you will skip element at index 0 header <conio.h> is not supported on sololearn, you will get error.

+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.

+2

main() { int i; int a={10,20,30,23,44,80}; for(i=0;i<a[i];i++) if(a[i]%2==0) printf("%d\n",a[i]); }

+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)

+1

DHANANJAY PATEL OP is not really clear on what he wants to do but one thing is clear that he is confused about array indexing. You asked him to start from 1 which he did but didn't followed your other advise that he needs to do arr[i-1] starting from 1 till <= 20 is still accessing 21 numbers in an array of size 20

+1

Vamsi How can I use colon after if statement

+1

Vamsi I've not learned python till now soo I don't have any idea about that

+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

+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

+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; }

+1

Using the output statement of that language 😂

+1

Arda Atıcı ok thanks for suggesting...I'll definitely try it

+1

0

Oh yah! I've just lost that ...thanks alot

0

As I want to print even number upto 20 and 20is also an even num