+ 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; }
52 Réponses
+ 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.
+ 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]);
}
+ 2
Dhananjay Patel
I've done soo but since then output is not correct
+ 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.
+ 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
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
VAMSI ok thanks
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
0
Swim
Okkk .... But can you just please make this program and show me the output.
0
Okk
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.....
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
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.