Write a program check if a list of integers contains only even numbers

Java Program to print Odd and Even Numbers from an Array

We can print odd and even numbers from an array in java by getting remainder of each element and checking if it is divided by 2 or not. If it is divided by 2, it is even number otherwise it is odd number.

Test it Now

Output:

Odd Numbers: 1 5 3 Even Numbers: 2 6 2
Next TopicJava Programs


← prev next →

Python program to print even numbers in a list

Given a list of numbers, write a Python program to print all even numbers in the given list.

Example:

Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] Input: list2 = [12, 14, 95, 3] Output: [12, 14]

Method 1: Using for loop

Iterate each element in the list using for loop and check if num % 2 == 0. If the condition satisfies, then only print the number.

Python3




# Python program to print Even Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
# iterating each number in list
for num in list1:
# checking condition
if num % 2 == 0:
print[num, end=" "]

Output:

10, 4, 66

Method 2: Using while loop

Python3




# Python program to print Even Numbers in a List
# list of numbers
list1 = [10, 24, 4, 45, 66, 93]
num = 0
# using while loop
while[num < len[list1]]:
# checking condition
if list1[num] % 2 == 0:
print[list1[num], end=" "]
# increment num
num += 1

Output:



10, 4, 66

Method 3: Using list comprehension

Python3




# Python program to print even Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
# using list comprehension
even_nos = [num for num in list1 if num % 2 == 0]
print["Even numbers in the list: ", even_nos]

Output:

Even numbers in the list: [10, 4, 66]

Method 4: Using lambda expressions

Python3




# Python program to print Even Numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 11]
# we can also print even no's using lambda exp.
even_nos = list[filter[lambda x: [x % 2 == 0], list1]]
print["Even numbers in the list: ", even_nos]

Output:

Even numbers in the list: [10, 4, 66]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python Programs
School Programming
Python list-programs
python-list
Practice Tags :
python-list
Read Full Article

Print all Even numbers from array of integers using C# program

This is a C# program, which will use to print all EVEN numbers from an array of integers, to find the Even number; we will check the remainder of each element b dividing 2, if the remainder is 0 that means elements are EVEN.

Given array of integers and we have to print all EVEN numbers.

For example we have list of integers:

18, 13, 23, 12, 27 18 is properly divisible by 2, So it is a even number. 13 is not properly divisible by 2, so it is not a even number. 23 is not properly divisible by 2, so it is not a even number. 12 is properly divisible by 2, So it is a even number. 27 is not properly divisible by 2, so it is not a even number.

Consider the example:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main[] { int i = 0; //declare array of integers int[] arr = new int[5]; //reading elements Console.WriteLine["Enter array elements : "]; for [i = 0; i

Bài Viết Liên Quan

Bài mới nhất

Chủ Đề