usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

Listing Odd Numbers in Perl - Fun Coding Tutorial for Kids



Listing Odd Numbers Using Perl

This tutorial explains how to list odd numbers using Perl in a simple and beginner-friendly way. It is designed for students and learners who are new to programming and want to understand how odd numbers can be generated using basic Perl logic.

By the end of this lesson, you will know how to generate odd numbers in Perl, use loops effectively, and apply simple conditions to filter numbers.

What Are Odd Numbers? | Maths Explanation for Perl Kids

Odd numbers are whole numbers that are not divisible by 2. Examples include 1, 3, 5, 7, and 9. In mathematics, an odd number always leaves a remainder of 1 when divided by 2.

Understanding odd numbers is an important part of primary mathematics, and Perl provides a practical way to explore this concept using code.


How to Generate Odd Numbers in Perl

To generate odd numbers in Perl, we use a loop to go through a range of numbers and a condition to check whether each number is odd.

A number is considered odd if:

number % 2 !== 0

This condition checks whether the remainder after dividing by 2 is not zero.

Perl Odd Number Program (Beginner Example)

The following example shows a simple Perl odd number program. It lists odd numbers within a given range and displays them on the page.

This type of example is commonly used in Perl beginner tutorials and helps students learn both maths concepts and programming basics at the same time.

Create a new Perl Module File;
Call it oddNumbers.pm.
Type out the adjoining Perl code for listing odd numbers.



Code for Odd Number List with User Input in Perl

For a little more flexibility, let's add an input form to our Perl code for odd numbers.

All we need is a way to ask the user for input.
For this purpose, we'll use the <STDIN>(<>) object in Perl.

Using a Loop to Display Odd Numbers in Perl

A loop allows Perl to repeat an action multiple times. In this case, the loop checks each number in a range and displays only the odd ones.

This approach demonstrates:

  • How to use a loop in Perl
  • How to apply conditions
  • How to list odd numbers with Perl clearly and efficiently

It is an excellent example for learners studying Perl maths code for the first time.


We have used a function here.
A function is a chunk of code that is executed when it is called upon, such as when an event occurs.



Why Learn Odd Numbers with Perl?

Learning how to work with odd numbers in Perl helps students to:

  • Understand number patterns
  • Practice logical thinking
  • Learn basic programming structures
  • Combine primary maths with coding skills

This makes Perl a useful tool for teaching and reinforcing mathematical concepts in an interactive way.

Who Is This Perl Lesson For?

This Perl lesson is suitable for:

  • Primary school students
  • Beginners learning Perl
  • Teachers looking for simple coding examples
  • Anyone learning how to generate odd numbers in Perl

No prior Perl programming experience is required.


Key Takeaways from Listing Odd Numbers Using Perl

  • Odd numbers are numbers not divisible by 2
  • Perl can be used to generate and display odd numbers
  • Loops and conditions are essential programming tools
  • This tutorial provides a clear Perl odd numbers tutorial for beginners

Summary: Listing Odd Numbers Using Perl

Learning how to generate odd numbers in Perl is a fun and practical way to combine math and coding. Whether you're a teacher, parent, or student, these Perl tutorials for kids make programming approachable and engaging.

So! Perl Fun Practice Exercise - List Odd Numbers

As a fun practice exercise, feel free to try out your own boundary values, and see how the Perl code lists the odd numbers between those boundary values.










Perl Code for odd Numbers - Module File.

package ODDNUMBERS;

BEGIN {
    require Exporter;

    # for the sake of standard
    our $VERSION = 2016.12;

    # Inherit from exporter to export functions and variables
    our @ISA = qw(Exporter);

    # Functions and variables to be exported by default
    our @EXPORT_OK = qw(prepResult);
}

use warnings;
use strict;

my ($start$stop);
my @result;

# simulate an object construct
# takes two arguments  -- besides its name;
# start and stop values for the range
sub new {
    my $this = shift;
    my $parameters = {@_};
    bless $parameters$this;
    $this->_init(@_);
    return $this;
}

# simulate a constructor
sub _init {
    my $self = shift;
    ($start$stop) = @_;
}

# Returns an array reference of the desired set odd numbers
sub prepResult {
    # Loop from start to stop and rip out odd numbers;
    until ($start > $stop) {
        if (($start % 2) != 0) { # modulo(%) is explained later
            push @result$start;
        }
        $start += 1# increase start by 1
    }
    return \@result;
}

1;


Perl Code for odd Numbers - Main Class.

#!/usr/bin/perl;
use strict;
use warnings;
use ODDNUMBERS;

# Useful variables
my ($lower_boundary$upper_boundary$answer);

# Use the odd number module/class
$lower_boundary = 1;
$upper_boundary = 100;
my $odd_list = ODDNUMBERS->new($lower_boundary$upper_boundary);
$answer = $odd_list->prepResult();
print "Odd numbers between $lower_boundary and $upper_boundary are:\n@{$answer}\n";


print "\n\n";


Perl Code for odd Numbers - Main class for Collecting Input.

#!/usr/bin/perl;
use strict;
use warnings;
use ODDNUMBERS;

# Useful variables
my ($lower_boundary$upper_boundary$answer);

# Collect Input
print "\n\nEnter the range for your odd numbers.\n";

print "Enter Start Number: ";
$lower_boundary = <>;
chomp $lower_boundary;

print "Enter Stop Number: ";
$upper_boundary = <>;
chomp $upper_boundary;

my $odd_list = ODDNUMBERS->new($lower_boundary$upper_boundary);
$answer = $odd_list->prepResult();
print "Odd numbers between $lower_boundary and $upper_boundary are:\n@{$answer}\n";


print "\n\n";





<< PreviousNext >>