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







<< PreviousNext >>

How to Generate Odd Numbers in Perl for Kids




What are Odd Numbers? | Maths Explanation for Perl Kids

Odd numbers are numbers that are not divisible by 2.

They include:

1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, ...

In this beginner-friendly Maths Perl tutorial for kids, we'll explore how to list odd numbers using Perl loops and conditions - perfect for primary school students learning to code.
To generate odd numbers in Perl, we use a 'while' loop combined with a conditional statement. This simple Perl exercise will help you understand number patterns and logic.

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



Note: You can comment out the evenNumbers Perl object code in the main class from the previous lesson or simply continue from where it stopped.



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.


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 >>