Open In App

PHP Program to Print All Even Numbers in a Range

Last Updated : 19 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given two numbers, Start and End, the task is to print all even numbers within a given range using PHP. Even numbers are integers that are divisible by 2, meaning they have no remainder when divided by 2.

Examples:

Input: start = 1, end = 10
Output: 2 4 6 8 10

Input: start = 21, end = 35
Output: 22 24 26 28 30 32 34

These are the following approaches:

Approach 1: Using a for loop

The basic method to print even numbers in a range is by using a for loop to iterate through the range and check if each number is even. The printEvenNumbers() function takes two arguments, $start and $end, which define the range. Inside the function, a for loop iterates from $start to $end. For each number, the loop checks if it is even by using the modulo operator %. If the remainder is 0 when divided by 2, the number is even and is printed.

Example: This example shows the use of for loop for printing the even numbers between the given range.

PHP
<?php
function printEvenNumbers($start, $end) {
    for ($i = $start; $i <= $end; $i++) {
        if ($i % 2 == 0) {
            echo $i . " ";
        }
    }
}

// Driver code
$start = 1;
$end = 10;

echo "Even numbers between $start and $end are: ";
printEvenNumbers($start, $end);

?>

Output
Even numbers between 1 and 10 are: 2 4 6 8 10 

Approach 2: Using a while loop

An alternative method is to use a while loop to achieve the same result. This approach is similar to the first approach, but instead of a for loop, a while loop is used. The variable $i is initialized with the start value and incremented in each iteration of the loop. The condition $i % 2 == 0 is used to check for even numbers.

Example: This example shows the use of while loop for printing the even numbers between the given range.

PHP
<?php

function printEvenNumbers($start, $end) {
    $i = $start;
    while ($i <= $end) {
        if ($i % 2 == 0) {
            echo $i . " ";
        }
        $i++;
    }
}

// Driver code
$start = 1;
$end = 10;

echo "Even numbers between $start and $end are: ";
printEvenNumbers($start, $end);

?>

Output
Even numbers between 1 and 10 are: 2 4 6 8 10 

Approach 3: Using array_filter() function

For a more concise solution, we can use the range function in PHP to generate an array of numbers within the specified range and then filter out the even numbers. The range function generates an array of numbers from $start to $end. The array_filter() function is used to filter out the even numbers. It takes the generated array and a callback function that returns true for even numbers. The implode function is used to convert the array of even numbers into a string, separated by spaces, for printing.

Example: This example shows the use of array_filter() function for printing the even numbers between the given range.

PHP
<?php

function printEvenNumbers($start, $end) {
    $numbers = range($start, $end);
    $evenNumbers = array_filter($numbers, function($number) {
        return $number % 2 == 0;
    });

    echo implode(" ", $evenNumbers);
}

// Driver code
$start = 1;
$end = 10;

echo "Even numbers between $start and $end are: ";
printEvenNumbers($start, $end);

?>

Output
Even numbers between 1 and 10 are: 2 4 6 8 10


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads