Practice 50 PHP Maths Programming Questions
Q1: Write a program to calculate the sum of two numbers.
Input: addNumbers(5, 3)
Expected Output:
8
Q2: Write a program to find the difference between two numbers.
Input: subtractNumbers(10, 4)
Expected Output:
6
Q3: Write a program to multiply two numbers.
Input: multiplyNumbers(6, 7)
Expected Output:
42
Q4: Write a program to divide one number by another.
Input: divideNumbers(15, 3)
Expected Output:
5
Q5: Write a program to calculate the remainder of two numbers.
Input: modulus(10, 3)
Expected Output:
1
Q6: Write a program to calculate the power of a number.
Input: power(2, 3)
Expected Output:
8
Q7: Write a program to find the square root of a number.
Input: squareRoot(16)
Expected Output:
4
Q8: Write a program to find the absolute value of a number.
Input: absoluteValue(-15)
Expected Output:
15
Q9: Write a program to calculate the factorial of a number.
Input: factorial(5)
Expected Output:
120
Q10: Write a program to check if a number is even or odd.
Input: isEven(4)
Expected Output:
True
Q11: Write a program to calculate the greatest common divisor (GCD) of two numbers.
Input: gcd(24, 36)
Expected Output:
12
Q12: Write a program to calculate the least common multiple (LCM) of two numbers.
Input: lcm(12, 15)
Expected Output:
60
Q13: Write a program to find the maximum of three numbers.
Input: maxOfThree(4, 7, 2)
Expected Output:
7
Q14: Write a program to find the minimum of three numbers.
Input: minOfThree(4, 7, 2)
Expected Output:
2
Q15: Write a program to round a number to the nearest integer.
Input: roundNumber(4.6)
Expected Output:
5
Q16: Write a program to round a number down to the nearest integer.
Input: floorNumber(4.8)
Expected Output:
4
Q17: Write a program to round a number up to the nearest integer.
Input: ceilNumber(4.1)
Expected Output:
5
Q18: Write a program to calculate the sine of an angle in degrees.
Input: sine(30)
Expected Output:
0.5
Q19: Write a program to calculate the cosine of an angle in degrees.
Input: cosine(60)
Expected Output:
0.5
Q20: Write a program to calculate the tangent of an angle in degrees.
Input: tangent(45)
Expected Output:
1
Q21: Write a program to convert degrees to radians.
Input: degreesToRadians(180)
Expected Output:
3.14159
Q22: Write a program to convert radians to degrees.
Input: radiansToDegrees(3.14159)
Expected Output:
180
Q23: Write a program to generate a random number between 1 and 10.
Input: randomNumber(1, 10)
Expected Output:
(e.g., 7)
Q24: Write a program to calculate the natural logarithm of a number.
Input: naturalLogarithm(2.71828)
Expected Output:
1
Q25: Write a program to calculate the base-10 logarithm of a number.
Input: logarithmBase10(1000)
Expected Output:
3
Q26: Write a program to calculate the exponential of a number.
Input: exponential(2)
Expected Output:
7.38906
Q27: Write a program to calculate the hypotenuse of a right triangle given two sides.
Input: hypotenuse(3, 4)
Expected Output:
5
Q28: Write a program to check if a number is a prime number.
Input: isPrime(7)
Expected Output:
True
Q29: Write a program to calculate the sum of all numbers in a given range.
Input: sumInRange(1, 5)
Expected Output:
15
Q30: Write a program to calculate the product of all numbers in a given range.
Input: productInRange(1, 4)
Expected Output:
24
Q31: Write a program to find the nth Fibonacci number.
Input: fibonacci(7)
Expected Output:
13
Q32: Write a program to check if a number is positive, negative, or zero.
Input: checkNumber(-10)
Expected Output:
Negative
Q33: Write a program to calculate the percentage of a number.
Input: calculatePercentage(50, 200)
Expected Output:
25
Q34: Write a program to find the average of an array of numbers.
Input: average([10, 20, 30, 40])
Expected Output:
25
Q35: Write a program to calculate the median of an array of numbers.
Input: median([5, 1, 3, 2, 4])
Expected Output:
3
Q36: Write a program to find the mode of an array of numbers.
Input: mode([1, 2, 2, 3, 4])
Expected Output:
2
Q37: Write a program to generate the first n prime numbers.
Input: generatePrimes(5)
Expected Output:
[2, 3, 5, 7, 11]
Q38: Write a program to calculate the sum of the squares of the first n natural numbers.
Input: sumOfSquares(3)
Expected Output:
14
Q39: Write a program to calculate the sum of the cubes of the first n natural numbers.
Input: sumOfCubes(3)
Expected Output:
36
Q40: Write a program to find the greatest number in an array.
Input: findMax([10, 20, 30, 5])
Expected Output:
30
Q41: Write a program to find the smallest number in an array.
Input: findMin([10, 20, 30, 5])
Expected Output:
5
Q42: Write a program to calculate the sum of the digits of a number.
Input: sumOfDigits(1234)
Expected Output:
10
Q43: Write a program to reverse the digits of a number.
Input: reverseDigits(1234)
Expected Output:
4321
Q44: Write a program to check if a number is a perfect square.
Input: isPerfectSquare(16)
Expected Output:
True
Q45: Write a program to check if a number is a perfect cube.
Input: isPerfectCube(27)
Expected Output:
True
Q46: Write a program to calculate the area of a circle given its radius.
Input: areaOfCircle(7)
Expected Output:
153.938
Q47: Write a program to calculate the perimeter of a rectangle.
Input: perimeterOfRectangle(5, 10)
Expected Output:
30
Q48: Write a program to calculate the area of a rectangle.
Input: areaOfRectangle(5, 10)
Expected Output:
50
Q49: Write a program to check if a number is divisible by another.
Input: isDivisible(20, 5)
Expected Output:
True
Q50: Write a program to calculate the average of two numbers.
Input: averageOfTwo(10, 20)
Expected Output:
15
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!