even and odd numbers while loop in php

Even and odd numbers while loop in php

Certainly! Below is an example of how you can use a while loop in PHP to print even and odd numbers:

<?php // Initialize a variable to start from $num = 1; echo "Even Numbers:\n"; // Loop to print even numbers while ($num <= 10) { // Check if the number is even if ($num % 2 == 0) { echo $num . "\n"; } $num++; // Increment the number } echo "\nOdd Numbers:\n"; // Reset the number for odd numbers $num = 1; // Loop to print odd numbers while ($num <= 10) { // Check if the number is odd if ($num % 2 != 0) { echo $num . "\n"; } $num++; // Increment the number } ?> 

In this example:

  • We initialize a variable $num to start from 1.
  • First, we print even numbers using a while loop. Inside the loop, we check if the current number is even using the modulo operator %. If the remainder when dividing by 2 is 0, then it's even, and we print it.
  • After printing even numbers, we reset $num to 1 to start from the beginning.
  • Then, we print odd numbers similarly by checking if the current number is not divisible by 2 (i.e., it has a remainder when divided by 2).

This code will output:

Even Numbers: 2 4 6 8 10 Odd Numbers: 1 3 5 7 9 

Examples

  1. How to print even numbers using a while loop in PHP?

    • Description: This snippet prints the first 10 even numbers using a while loop in PHP.
    • Code:
      $count = 0; $num = 2; // Start from the first even number while ($count < 10) { echo $num . "\n"; $num += 2; // Increment by 2 to get the next even number $count++; } 
  2. Print odd numbers using a while loop in PHP

    • Description: This snippet prints the first 10 odd numbers using a while loop in PHP.
    • Code:
      $count = 0; $num = 1; // Start from the first odd number while ($count < 10) { echo $num . "\n"; $num += 2; // Increment by 2 to get the next odd number $count++; } 
  3. Check if a number is even or odd with a while loop in PHP

    • Description: This snippet uses a while loop to check if a given range of numbers are even or odd.
    • Code:
      $num = 1; while ($num <= 10) { if ($num % 2 === 0) { echo $num . " is even\n"; } else { echo $num . " is odd\n"; } $num++; } 
  4. Generate even numbers from a range using a while loop in PHP

    • Description: This code snippet generates even numbers from 1 to 20 using a while loop.
    • Code:
      $num = 1; while ($num <= 20) { if ($num % 2 === 0) { echo $num . "\n"; } $num++; } 
  5. Generate odd numbers from a range using a while loop in PHP

    • Description: This code snippet generates odd numbers from 1 to 20 using a while loop.
    • Code:
      $num = 1; while ($num <= 20) { if ($num % 2 !== 0) { echo $num . "\n"; } $num++; } 
  6. Sum of even numbers using a while loop in PHP

    • Description: This snippet calculates the sum of the first 10 even numbers using a while loop.
    • Code:
      $count = 0; $num = 2; $sum = 0; while ($count < 10) { $sum += $num; $num += 2; $count++; } echo "Sum of first 10 even numbers: " . $sum . "\n"; 
  7. Sum of odd numbers using a while loop in PHP

    • Description: This snippet calculates the sum of the first 10 odd numbers using a while loop.
    • Code:
      $count = 0; $num = 1; $sum = 0; while ($count < 10) { $sum += $num; $num += 2; $count++; } echo "Sum of first 10 odd numbers: " . $sum . "\n"; 
  8. Find all even numbers in an array using a while loop in PHP

    • Description: This code snippet uses a while loop to find all even numbers in a given array.
    • Code:
      $arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; $index = 0; while ($index < count($arr)) { if ($arr[$index] % 2 === 0) { echo $arr[$index] . " is even\n"; } $index++; } 
  9. Find all odd numbers in an array using a while loop in PHP

    • Description: This code snippet uses a while loop to find all odd numbers in a given array.
    • Code:
      $arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; $index = 0; while ($index < count($arr)) { if ($arr[$index] % 2 !== 0) { echo $arr[$index] . " is odd\n"; } $index++; } 
  10. Generate even and odd numbers in separate arrays using a while loop in PHP

    • Description: This snippet generates two separate arrays for even and odd numbers from 1 to 10 using a while loop.
    • Code:
      $even_numbers = []; $odd_numbers = []; $num = 1; while ($num <= 10) { if ($num % 2 === 0) { $even_numbers[] = $num; } else { $odd_numbers[] = $num; } $num++; } echo "Even numbers: " . implode(", ", $even_numbers) . "\n"; echo "Odd numbers: " . implode(", ", $odd_numbers) . "\n"; 

More Tags

wordcloud2 gpo dialect bootstrap-treeview web-mediarecorder android-contacts flurl startup broken-pipe linux

More Programming Questions

More Fitness Calculators

More Fitness-Health Calculators

More Tax and Salary Calculators

More Entertainment Anecdotes Calculators