0% found this document useful (0 votes)
4 views5 pages

Q1. Find The Simple Interest Where P 10000, R 3, T 4 Output

The document contains a series of PHP programming assignments with solutions. It covers topics such as calculating simple interest, printing the day of the week, swapping numbers, finding the sum of digits, identifying prime numbers, calculating factorials, reversing strings, and computing areas of geometric shapes. Additionally, it includes tasks for printing patterns, finding odd numbers, typecasting, and counting binary 1s and uppercase letters in a string.

Uploaded by

daxom27234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Q1. Find The Simple Interest Where P 10000, R 3, T 4 Output

The document contains a series of PHP programming assignments with solutions. It covers topics such as calculating simple interest, printing the day of the week, swapping numbers, finding the sum of digits, identifying prime numbers, calculating factorials, reversing strings, and computing areas of geometric shapes. Additionally, it includes tasks for printing patterns, finding odd numbers, typecasting, and counting binary 1s and uppercase letters in a string.

Uploaded by

daxom27234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 3

Output
Q1. Find the simple interest where P=10000, R=3, T=4
Ans - <?php Output 1
$P = 10000;
The simple interest is: 1200
$R = 3;

$T = 4;
Output 2
$simple_interest = ($P * $R * $T) / 100;
Today is Thursday.
echo "The simple interest is: " . $simple_interest;

?>

Q2. Print the day of the week using switch case


Ans - <?php

$dayOfWeek = date("l");

switch ($dayOfWeek) {

case "Monday":

echo "Today is Monday.";

break;

case "Tuesday":

echo "Today is Tuesday.";

break;

case "Wednesday":

echo "Today is Wednesday.";

break;

case "Thursday":

echo "Today is Thursday.";

break;

case "Friday":

echo "Today is Friday.";

break;

case "Saturday":

echo "Today is Saturday.";

break;

case "Sunday":

echo "Today is Sunday.";

break;

default:

echo "Invalid day of the week.";

?>
Q3. Swap two given numbers
Ans - <?php

$num1 = 10;
Output 3
$num2 = 20;
Before swapping: num1 = 30, num2 = 40
echo "Before swapping: num1 = " . $num1 . ", num2 = " . $num2;
A er swapping: num1 = 40, num2 = 30
$temp = $num1;
Output 4
$num1 = $num2;
The sum of digits is: 28
$num2 = $temp;
Output 5
echo "\nA er swapping: num1 = " . $num1 . ", num2 = " . $num2;
2 3 5 7 11 13 17 19 23 29 31 37 41 43
?>
47
Q4. Find the sum of digits of a given number

Ans - <?php
$num = 45865;

$sum = 0;

while ($num > 0) {

$digit = $num % 10;

$sum += $digit;

$num = (int)($num / 10);

echo "The sum of digits is: " . $sum;

?>

Q5. Find the prime numbers between 1-50

Ans - <?php
for ($num = 2; $num <= 50; $num++) {

$isPrime = true; // Assume the number is prime

for ($i = 2; $i <= sqrt($num); $i++) {

if ($num % $i == 0) {

$isPrime = false; // It's not a prime number

break;

if ($isPrime) {

echo $num . " ";

?>
Q6. Find the factorial of a given number

Ans - <?php
Output 6
$num = 7; // Given number
The factorial of 7 is: 5040
$factorial = 1; // Variable to store the factorial
Output 7
for ($i = 1; $i <= $num; $i++) {
dlroW olleH
$factorial *= $i;

} Output 8

echo "The factorial of " . $num . " is: " . $factorial; The area of rectangle is: 50
?> The area of the square is: 25
Q7. Reverse a given string The area of the circle is 78.53981633
Ans - <?php

$str = "Hello World";

$reversedStr = "";

for ($i = strlen($str) - 1; $i >= 0; $i--) {

$reversedStr .= $str[$i];

echo "The reversed string is: " . $reversedStr;

?>

Q8. Area of rectangle, square and circle


Ans- Rectangle

<?php

$length = 10; // Length of the rectangle

$width = 5; // Width of the rectangle

$area = $length * $width;

echo "The area of the rectangle is: " . $area;

?>

Square

<?php

$side = 5; // Side length of the square

$area = $side * $side;

echo "The area of the square is: " . $area;

?>

Circle

<?php

$radius = 5; // Radius of the circle

$area = pi() * pow($radius, 2);


echo "The area of the circle is: " . $area;

?>

Q9. Print pa ern


Ans - <?php
Output 9
for ($i = 1; $i <= 5; $i++) {

for ($j = 1; $j <= $i; $j++) {


1
12
echo $j; 123
} 1234
12345
echo "</br>";

} Output 10
1
?> 23
Q10. Print pa ern 456
7 8 9 10
Ans - <?php

$n = 1; Output 11
11 13 15 17 19
for ($i = 0; $i < 4; $i++)

for ($j = 0; $j <= $i; $j++ )

echo $n." ";

$n = $n + 1;

echo "<br/>";

?>

Q11. Find and print all odd numbers between any two numbers.

Ans - <?php
$num1 = 10;

$num2 = 20;

for ($i = $num1; $i <= $num2; $i++) {

if ($i % 2 != 0) {

echo $i . "\n";

?>
Q12. Explicit typecast to int.

Ans - <?php
$var = "12345";
Output 12
$intVar = (int)$var;
The integer value is: 12345
echo "The integer value is: " . $intVar;

?> Output 13
Q13. Find the number of 1s in the binary form of the given number.
The number of 1’s in the binary form
Ans - <?php of 15 is: 4
$num = 15;
Output 14
$binary = decbin($num);

$count = substr_count($binary, '1'); The number of uppercase le ers in


the string is: 2
echo "The number of 1s in the binary form of " . $num . " is: " . $count;

?>

Q14. Find the number of uppercase le ers in a string.

Ans - <?php
$str = "Hello, World!";

$count = 0;

for ($i = 0; $i < strlen($str); $i++) {

if ($str[$i] >= 'A' && $str[$i] <= 'Z') {

$count++;

echo "The number of uppercase le ers in the string is: " . $count;

?>

You might also like