In this tutorial, we will learn writing program in Python to create an array (list in case of python) and print the odd elements stored in the array (list).
Problem Statement
Our program will first take the input of array (list) size and then the elements of the array (list) from the user. And then prints all the odd numbers from the elements of the input array (list).
For example:
Case 1: if the user inputs 4 as array (list) size and the array (list) elements as 1,2,3,4.
The output should be 1, 3.
Case 2: if the user inputs 5 as array (list) size and the array (list) elements as 9,8,7,6,5.
The output should be 9, 7, 5.
Our logic to print odd numbers from array (list)
- Our program will take input to fix the size of the array (list).
- Then our program will run for a ‘for loop’ to take the inputs from the user. The inputs will be the elements (or the content) of the array (list).
- Then, for each element of the array (list), our program will use the ‘if-else’ conditional statement to check if the element is odd.
- Print the odd numbers from the array (list)
Python program to print odd numbers from array (list)
#taking the input from the user to fix the array size
size=int(input("Enter the number of elements you want in array: "))
arr=[]
sum=0
# adding elements to the array (list)
for i in range(0,size):
elem=int(input("Please give value for index "+str(i)+": "))
arr.append(elem)
print("All even number in array are ")
# check and print if any element of array (list) is odd
for i in range(0,size):
if arr[i]%2!=0:
print(arr[i],end=' ')
Output:
Enter the number of elements you want in array: 5
Please give value for index 0: 32
Please give value for index 1: 11
Please give value for index 2: 45
Please give value for index 3: 2
Please give value for index 4: 23
All even number in array are
11 45 23
Explanation:
For the input array [32, 11, 45, 2, 33], while iterating the elements of the array (list). For elements 11, 45, and 33, the if-condition is satisfied as they are odd numbers and then the numbers 11, 45, and 33 are printed as the output of the program.
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…