Table of Contents

Printing Even Numbers in a Given Sequence

Example Description
Print all the even numbers in a given sequence. This code uses a for loop and modulus operator to determine if each number is even or odd."

Introduction:

In this Python code, we will be printing all the even numbers in a given sequence. We will use a for loop to iterate through each number and check if it is divisible by 2 (which means it is even). If the number is even, we will print it along with a message indicating that it is an even number.

Code:

#Print even numbers in the given sequence
numbers = [1,2,3,4,5,6,7,8,9,10]
for num in numbers:
 if (num % 2) == 0:
  print(num,'is an even Number')

Logic:

  1. The code uses a for loop to iterate through each number in the given sequence.
  2. Inside the loop, it uses the modulus operator to check if the number is divisible by 2.
  3. If the remainder is 0, then the number is even, and it is printed along with the message indicating that it is an even number.

Output:

>> 2 is an even Number

>> 4 is an even Number

>> 6 is an even Number

>> 8 is an even Number

>> 10 is an even Number