Today we solve 10 basic problems using PHP, it’s very easy to learn, also a trending language today.

1. To Add Two Numbers

We’re currently practicing performing basic addition operations with two integer numbers.

<?php
$firstNum = (int)readline('Enter an integer: ');
$secondNum = (int)readline('Enter an integer: ');

function sum($a,$b) {
  $c = $a+$b;
  echo $c;
}

sum($firstNum, $secondNum)
?>

# input 10 , 8
# output 18

2. Convert Celsius To Fahrenheit

We can implement the formula for converting Celsius to Fahrenheit in Python code by using the expression (0°C × 9/5) + 32 = 32°F.

<?php
$c_temp = (int)readline('Enter an integer: ');
$f_temp = (($c_temp*9)/5)+32;

echo "Fahrenheit: $f_temp";
?>

# input 36
# output 96.8

3. Convert Kilometers To Miles in javascript

We know 1 kilometer is approximately equal to 0.62 miles, we can easily calculate the distance in miles by multiplying the distance in kilometers by 0.62.

<?php

function kmToMiles($km) {
  return $km*0.62;
}

$km = (int)readline('Enter Km:  ');

echo "Miles: ".kmToMiles($km);
?>

# input 5
# output 3.1

4. Is A Number Positive Or Negative

We can easily determine whether a number is positive or negative using this logic: if the number is equal to or greater than zero, then it’s positive; otherwise, it’s negative.

<?php

function isPositive($num) {
    $check = $num >=0 ? "positive" : "negetive";
  return $check;
}

$num = (int)readline('Enter Number:  ');

echo isPositive($num);
?>

# input 2
# output positive
# input -3
# output negetive

5. Is A Number Even Or Odd

We can determine whether a number is even or odd by dividing it by 2 and checking the remainder (modulus). If the remainder is zero, then the number is even; otherwise, it’s odd.

<?php

function evenOrOdd($num) {
    if ($num != 0) {
  $check = $num%2 == 0 ? "Even" : "Odd";
  echo $check;
} else {
  echo "not Even or Odd";
}

}

$num = (int)readline('Enter Number:  ');

echo evenOrOdd($num);
?>

# input 2
# output even
# input 3
# output odd

6. Find the Largest Number from a List

Here, we’re leveraging PHP default function max(). Later on, we’ll explore implementing custom methods using loops or recursion.

<?php

$numArray = [2,5,3,1,4];
echo "max number is " . max($numArray);
?>

# input [2,5,3,1,4]
# output 5

7. Determine whether a year is a leap year or not

We can determine whether a year is a leap year by the following rules: if the year is divisible by 400, it’s a leap year. If it’s not divisible by 400 but divisible by 4 and not divisible by 100, then it’s also a leap year; otherwise, it’s not leap year.

<?php

function isLeapYear($year) {
    if ($year%400==0 or $year%4==0 and $year%100!=0) {
    echo "leap year";
} else {
  echo "not leap year";
}

}

$year = (int)readline('Enter the Year:  ');

isLeapYear($year)
?>

# input 1996
# output leap year
# input 2300
# output not leap year

8. Find The Factorial Of A Number

The factorial of a non-negative integer 𝑛n, denoted by 𝑛!n!, is the product of all positive integers less than or equal to 𝑛n. For example:

𝑛!=𝑛×(𝑛−1)×(𝑛−2)×…×3×2×1n!=n×(n−1)×(n−2)×…×3×2×1

For instance, 5!=5×4×3×2×1=1205!=5×4×3×2×1=120.

<?php

function factorial($num) {
    $output = 1;
    while ($num>1) {
        $output = $output * $num;
        $num = $num - 1;
}
echo $output;
}

$num = (int)readline('Enter a number:  ');
echo factorial($num)
?>

# input 5
# output 120

see same problems solutions in python

If you don’t understand the syntax properly, then learn from here

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *