Given a very long list of numbers, you need to print the even numbers from the list. The list ends when -1 is encountered.
1
4
-1
Sample output:
4
Implementation:
Method 1:
In a loop, start reading numbers one by one, & for every number read, check whether it is even number or odd number.1. If it is even number, print it.
2. Stop reading numbers when -1 is encountered.
#include <stdio.h>
int main()
{
int num=0;
do
{
scanf("%d",&num);
if(num%2==0)
printf("%d\n",num);
} while(num!=-1);
return 0;
}
Sample Input:
1
4
-1
Sample output:
4
Method 2:
It is similar to Method 1, except that checking for atleast one input integer from user & it is not equal to "-1", printing numbers that are even.Implementation:
#include<stdio.h>
int main()
{
int N;
while(scanf("%d",&N)>0 && N!=-1)
if(N%2==0)
printf("%d\n",N);
return 0;
}