PHP Arithmetic Operators and Calculations Practice Questions with Solutions

Introduction

PHP arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication, division, and finding remainders. These operators are useful for calculating student marks, employee salaries, product prices, discounts, and monthly expenses. In this chapter, you will practice PHP programs using arithmetic operators and real-world calculations, with solutions and sample outputs. PHP Arithmetic Operators and Calculations Practice Questions with Solutions to help you understand the concepts.

Question 1: Add Two Numbers

Problem Statement: Create two variables containing numbers and calculate their sum using the addition operator.

PHP Solution:

<?php
$number1 = 150;
$number2 = 250;

$sum = $number1 + $number2;

echo "First Number: " . $number1 . "<br>";
echo "Second Number: " . $number2 . "<br>";
echo "Total Sum: " . $sum;
?>

Sample Output:

First Number: 150
Second Number: 250
Total Sum: 400

Explanation: The + operator adds two numbers. The expression $number1 + $number2 calculates the sum and stores it in $sum.

Concepts Covered: Addition, variables, arithmetic expressions.

Question 2: Calculate the Difference Between Two Numbers

Problem Statement: A shop had 850 products in stock and sold 275 products. Calculate how many products remain.

PHP Solution:

<?php
$totalProducts = 850;
$soldProducts = 275;

$remainingProducts = $totalProducts - $soldProducts;

echo "Total Products: " . $totalProducts . "<br>";
echo "Sold Products: " . $soldProducts . "<br>";
echo "Remaining Products: " . $remainingProducts;
?>

Sample Output:

Total Products: 850
Sold Products: 275
Remaining Products: 575

Explanation: The - operator subtracts the sold quantity from the total stock. The remaining stock is 575 products.

Concepts Covered: Subtraction, inventory calculations.

Question 3: Calculate the Total Cost of Products

Problem Statement: A customer purchases 5 notebooks at ₹40 each. Calculate the total cost using the multiplication operator.

PHP Solution:

<?php
$productPrice = 40;
$quantity = 5;

$totalCost = $productPrice * $quantity;

echo "Price Per Notebook: Rs. " . $productPrice . "<br>";
echo "Quantity: " . $quantity . "<br>";
echo "Total Cost: Rs. " . $totalCost;
?>

Sample Output:

Price Per Notebook: Rs. 40
Quantity: 5
Total Cost: Rs. 200

Explanation: The * operator multiplies the price per notebook by the quantity purchased. This is a common calculation in billing systems.

Concepts Covered: Multiplication, product pricing.

Question 4: Divide a Total Amount Equally

Problem Statement: Four friends share a restaurant bill of ₹2,400 equally. Calculate how much each friend must pay.

PHP Solution:

<?php
$totalBill = 2400;
$friends = 4;

$amountPerPerson = $totalBill / $friends;

echo "Total Bill: Rs. " . $totalBill . "<br>";
echo "Number of Friends: " . $friends . "<br>";
echo "Amount Per Person: Rs. " . $amountPerPerson;
?>

Sample Output:

Total Bill: Rs. 2400
Number of Friends: 4
Amount Per Person: Rs. 600

Explanation: The / operator divides the total bill by the number of friends. Always ensure that the divisor is not zero.

Concepts Covered: Division, equal distribution.

Question 5: Find the Remainder Using the Modulus Operator

Problem Statement: A teacher has 47 students and wants to arrange them into groups of 6. Calculate the number of students left after forming complete groups.

PHP Solution:

<?php
$totalStudents = 47;
$groupSize = 6;

$remainingStudents = $totalStudents % $groupSize;

echo "Total Students: " . $totalStudents . "<br>";
echo "Students Per Group: " . $groupSize . "<br>";
echo "Remaining Students: " . $remainingStudents;
?>

Sample Output:

Total Students: 47
Students Per Group: 6
Remaining Students: 5

Explanation: The % operator returns the remainder after division. Since 47 divided by 6 leaves a remainder of 5, five students remain after forming complete groups.

Concepts Covered: Modulus operator, remainders, grouping.

Question 6: Calculate the Average Marks of a Student

Problem Statement: A student scores 78, 85, 92, and 81 in four subjects. Calculate the total and average marks.

PHP Solution:

<?php
$english = 78;
$maths = 85;
$science = 92;
$computer = 81;

$totalMarks = $english + $maths + $science + $computer;
$averageMarks = $totalMarks / 4;

echo "Total Marks: " . $totalMarks . "<br>";
echo "Average Marks: " . $averageMarks;
?>

Sample Output:

Total Marks: 336
Average Marks: 84

Explanation: The + operator adds the marks from all four subjects. The / operator divides the total by four to calculate the average.

Concepts Covered: Addition, division, average calculation.

Question 7: Calculate an Employee’s Monthly Salary

Problem Statement: An employee receives a basic salary of ₹30,000, an allowance of ₹5,000, and a bonus of ₹2,000. Calculate the gross monthly salary before deductions.

PHP Solution:

<?php
$basicSalary = 30000;
$allowance = 5000;
$bonus = 2000;

$grossSalary = $basicSalary + $allowance + $bonus;

echo "Basic Salary: Rs. " . $basicSalary . "<br>";
echo "Allowance: Rs. " . $allowance . "<br>";
echo "Bonus: Rs. " . $bonus . "<br>";
echo "Gross Salary: Rs. " . $grossSalary;
?>

Sample Output:

Basic Salary: Rs. 30000
Allowance: Rs. 5000
Bonus: Rs. 2000
Gross Salary: Rs. 37000

Explanation: The gross salary is calculated by adding the basic salary, allowance, and bonus. This calculation does not include deductions such as PF or income tax.

Concepts Covered: Addition, salary calculations, financial arithmetic.

Question 8: Calculate a Discounted Product Price

Problem Statement: A customer purchases a product priced at ₹2,000 and receives a 15% discount. Calculate the discount amount and the final price.

PHP Solution:

<?php
$originalPrice = 2000;
$discountPercentage = 15;

$discountAmount = ($originalPrice * $discountPercentage) / 100;
$finalPrice = $originalPrice - $discountAmount;

echo "Original Price: Rs. " . $originalPrice . "<br>";
echo "Discount Percentage: " . $discountPercentage . "%<br>";
echo "Discount Amount: Rs. " . $discountAmount . "<br>";
echo "Final Price: Rs. " . $finalPrice;
?>

Sample Output:

Original Price: Rs. 2000
Discount Percentage: 15%
Discount Amount: Rs. 300
Final Price: Rs. 1700

Explanation: Multiplying the original price by the discount percentage and dividing by 100 gives the discount amount. Subtracting that amount from the original price gives the final price.

Concepts Covered: Multiplication, division, percentage calculations, subtraction.

Question 9: Calculate Simple Interest

Problem Statement: A person invests ₹10,000 at an annual simple interest rate of 5% for 3 years. Calculate the simple interest and total amount.

PHP Solution:

<?php
$principal = 10000;
$rate = 5;
$time = 3;

$simpleInterest = ($principal * $rate * $time) / 100;
$totalAmount = $principal + $simpleInterest;

echo "Principal Amount: Rs. " . $principal . "<br>";
echo "Interest Rate: " . $rate . "%<br>";
echo "Time: " . $time . " years<br>";
echo "Simple Interest: Rs. " . $simpleInterest . "<br>";
echo "Total Amount: Rs. " . $totalAmount;
?>

Sample Output:

Principal Amount: Rs. 10000
Interest Rate: 5%
Time: 3 years
Simple Interest: Rs. 1500
Total Amount: Rs. 11500

Explanation: Simple interest is calculated using the formula:

\[ \text{Simple Interest}=\frac{P \times R \times T}{100} \]

Here, \(P\) is the principal, \(R\) is the annual interest rate, and \(T\) is the time in years. Adding the interest to the principal gives the total amount.

Concepts Covered: Multiplication, division, percentage calculations, financial formulas.

Question 10: Calculate the Total Bill with GST

Problem Statement: A customer purchases 2 keyboards priced at ₹800 each. Calculate the subtotal, add 18% GST, and display the final bill.

PHP Solution:

<?php
$keyboardPrice = 800;
$quantity = 2;
$gstPercentage = 18;

$subtotal = $keyboardPrice * $quantity;
$gstAmount = ($subtotal * $gstPercentage) / 100;
$finalBill = $subtotal + $gstAmount;

echo "Keyboard Price: Rs. " . $keyboardPrice . "<br>";
echo "Quantity: " . $quantity . "<br>";
echo "Subtotal: Rs. " . $subtotal . "<br>";
echo "GST Amount: Rs. " . $gstAmount . "<br>";
echo "Final Bill: Rs. " . $finalBill;
?>

Sample Output:

Keyboard Price: Rs. 800
Quantity: 2
Subtotal: Rs. 1600
GST Amount: Rs. 288
Final Bill: Rs. 1888

Explanation: The subtotal is calculated by multiplying the price by the quantity. The GST amount is 18% of the subtotal. Adding GST to the subtotal gives the final bill. This example assumes the stated GST rate applies to the purchase.

Concepts Covered: Multiplication, percentage calculations, addition, billing calculations.

Key Takeaways

  • + adds two or more values.
  • - subtracts one value from another.
  • * multiplies values.
  • / divides values and can return a decimal result.
  • % returns the remainder after division.
  • ** calculates a number raised to a power.
  • intdiv() returns the integer quotient of a division.
  • Parentheses help organize calculations and control evaluation order.
  • Arithmetic operators are useful for marks, salaries, discounts, GST, and financial calculations.
  • Always check for division by zero and validate user input before calculating.

Frequently Asked Questions

1. What are arithmetic operators in PHP?

Arithmetic operators perform mathematical operations such as addition, subtraction, multiplication, division, and finding remainders.

2. Which operator is used for addition in PHP?

The + operator adds two or more numeric values.

$total = 100 + 200;
echo $total;

Output: 300

3. What is the difference between / and %?

The / operator performs division, while % returns the remainder after division. For example, 10 / 3 produces approximately 3.3333, while 10 % 3 returns 1.

4. How do you calculate a percentage in PHP?

Multiply the original value by the percentage and divide by 100.

$amount = 2000;
$percentage = 15;

$result = ($amount * $percentage) / 100;
echo $result;

Output: 300

5. How do you calculate an average in PHP?

Add all the values and divide their total by the number of values.

$average = (70 + 80 + 90) / 3;
echo $average;

Output: 80

6. What does the exponentiation operator ** do?

The ** operator raises a number to a specified power.

echo 2 ** 3;

Output: 8

7. What is intdiv() in PHP?

The intdiv() function returns the integer quotient of a division.

echo intdiv(17, 5);

Output: 3

8. What happens if you divide a number by zero?

Division by zero is not a valid arithmetic operation. In PHP, dividing by zero with / throws a DivisionByZeroError. Check that the divisor is not zero before dividing.

9. Can PHP arithmetic operators work with variables?

Yes. You can use variables in arithmetic expressions to calculate totals, averages, salaries, discounts, and many other results.

10. What is the difference between * and **?

The * operator multiplies two values, while ** calculates exponentiation. For example, 4 * 3 returns 12, while 4 ** 3 returns 64.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top