In the vibrant city of Codeville, the Grand Council of Developers often faced problems that required complex arithmetic operations, especially involving prime numbers, odd numbers, and even numbers. To solve these problems, they decided to harness the power of loops and conditional statements. Join us as we explore how they implemented these operations using while loops and if conditions in PHP.
Mayor Binary addressed the council. “To tackle our problems, we need to understand how to perform arithmetic operations and use loops and conditions effectively. Let’s start with the basics.”
Prime Numbers
A prime number is a number greater than 1 that has no positive divisors other than 1 and itself.
Odd and Even Numbers
The council decided to write a PHP script to print prime numbers, odd numbers, and even numbers within a given range using while loops and if conditions.
Prime Number Check
The council implemented a function to check if a number is prime:
php
Copy code
<?php
function isPrime($number) {
if ($number <= 1) {
return false;
}
for ($i = 2; $i <= sqrt($number); $i++) {
if ($number % $i == 0) {
return false;
}
}
return true;
}
?>
Using While Loop and If Conditions
Next, they used a while loop to iterate through a range of numbers and apply conditions to check for prime, odd, and even numbers:
php
Copy code
<?php
// Define the range
$start = 1;
$end = 50;
// Initialize the counter
$current = $start;
// Loop through the range
while ($current <= $end) {
// Check if the number is prime
if (isPrime($current)) {
echo “$current is a prime number.<br>”;
}
// Check if the number is even
if ($current % 2 == 0) {
echo “$current is an even number.<br>”;
} else {
// If it’s not even, it must be odd
echo “$current is an odd number.<br>”;
}
// Increment the counter
$current++;
}
?>
Here’s the complete script that combines the prime check function and the while loop with if conditions:
php
Copy code
<?php
function isPrime($number) {
if ($number <= 1) {
return false;
}
for ($i = 2; $i <= sqrt($number); $i++) {
if ($number % $i == 0) {
return false;
}
}
return true;
}
// Define the range
$start = 1;
$end = 50;
// Initialize the counter
$current = $start;
// Loop through the range
while ($current <= $end) {
// Check if the number is prime
if (isPrime($current)) {
echo “$current is a prime number.<br>”;
}
// Check if the number is even
if ($current % 2 == 0) {
echo “$current is an even number.<br>”;
} else {
// If it’s not even, it must be odd
echo “$current is an odd number.<br>”;
}
// Increment the counter
$current++;
}
?>
By using while loops and if conditions, the Grand Council of Developers in Codeville efficiently implemented logic to check for prime numbers, odd numbers, and even numbers within a given range. These fundamental concepts are essential for solving various arithmetic problems and can be adapted to numerous scenarios. Feel free to experiment with the provided script and modify it for different ranges or additional conditions. If you have any questions or need further assistance, I’m here to help. Happy coding!