Perl Program to Find Out Odd and Even Numbers From a List of Numbers
Last Updated :
24 Feb, 2023
The article focuses on discussing different ways to find out how to segregate odd and even numbers from a given list of numbers in Perl.
Prerequisite: The user must be familiar with Perl and has an idea about conditional statements and loops.
Problem Statement
Given a list of integers, find out which of them are odd and which of them are even.
Input: arr = (10, 5, 3, 8, 9)
Output:
10 is an even number
5 is an odd number
3 is an odd number
8 is an even number
9 is an odd number
Implementation
Approach 1: Using for loop with if-else statements
- Use a variable of type list to store all the integers which will check whether it is odd or even.
- Then using another variable to store the length of that list and use that variable later in for loop while iterating over the list.
- Start a for loop with an iterator i, which starts from 0 and goes till the (length of list-1), and it gets incremented in each step.
- Immediately following the loop for each element check whether that particular element at the ith index is completely divisible by 2 or not (It returns 0 as the remainder).
- If so then that number is even, otherwise odd.
Below is the code of the above approach:
Perl
#!/usr/bin/perl
@arr = (10,15,21,69,42,5,3,11,28,63,7,11);
$size = @arr ;
for ( $i = 0; $i < $size ; $i ++){
if ( @arr [ $i ] % 2 == 0){
print ( "@arr[$i] is an even number\n" );
}
else {
print ( "@arr[$i] is an odd number\n" );
}
}
|
Output:
Approach 2: Use foreach loop with if-else Statement
- Use a variable to store the integers, then start a foreach loop with any variable of the user’s choice, there is no concept of taking the size of that list into consideration as we will iterate through the list until we reach the last element.
- Check each of those elements if they are perfectly divisible by 2 or not to find out if they are odd or even.
Below is the Perl program to implement the above approach:
Perl
#!/usr/bin/perl
@arr = (10,15,21,69,42,5,3,11,28,63,7,11);
foreach $num ( @arr ){
if ( $num % 2 == 0){
print ( "$num is an Even Number\n" );
}
else {
print ( "$num is an Odd Number\n" );
}
}
|
Output:
Approach 3: Using while Loop with if-else Statements
- First, take a variable that starts from 0 (to mark the first index of the list) and it will go to the size-1 of that list, so here like the first approach, we will use another variable to store the size of the list.
- Then iterating over the list and using that variable that started from 0 will check each index of the list and check if they are odd or even (perfectly divisible by 2).
- After deciding whether it is odd or even we will just increment that variable.
Below is the Perl program to implement the above approach:
Perl
#!/usr/bin/perl
@arr = (10,15,21,69,42,5,3,11,28,63,7,11);
$size = @arr ;
$count = 0;
while ( $count < $size ){
if ( @arr [ $count ] % 2 == 0){
print ( "@arr[$count] is Even Number\n" );
}
else {
print ( "@arr[$count] is Odd Number\n" );
}
$count ++;
}
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...