0% found this document useful (0 votes)
1 views

php pr2

The document contains several PHP and Java code snippets demonstrating basic programming concepts such as logical operators, conditional statements, and switch cases. It includes examples for checking if a number is positive or negative, converting numbers to words, finding the largest of three numbers, and calculating an electricity bill based on unit consumption. Each code snippet is followed by its respective output.

Uploaded by

thopteayush604
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)
1 views

php pr2

The document contains several PHP and Java code snippets demonstrating basic programming concepts such as logical operators, conditional statements, and switch cases. It includes examples for checking if a number is positive or negative, converting numbers to words, finding the largest of three numbers, and calculating an electricity bill based on unit consumption. Each code snippet is followed by its respective output.

Uploaded by

thopteayush604
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/ 4

1.

Make the use of Logical Operators


<?php
$a = true; $b =
false;

echo ($a && $b) ? "Both are true\n" : "At least one is false\n";

echo ($a || $b) ? "At least one is true\n" : "Both are false\n";

echo (!$a) ? "a is false\n" : "a is true\n";

echo ($a xor $b) ? "Only one is true\n" : "Both are either true or false\n"; ?>

Output:
At least one is false At least
one is true a is true Only
one is true

2. Check number is Positive or Negative


<?php

$number = -5;

if ($number > 0)
{
echo "$number is Positive";
}
elseif ($number < 0)
{
echo "$number is Negative";
}
else
{
echo "The number is Zero";
}
?>

Output:
-5 is Negative

3. Convert 0-9 Number to Words using switch statement


<?php $number
= 3;
switch ($number)
{
case 0: echo "Zero"; break; case 1:
echo "One"; break; case 2: echo "Two";
break; case 3: echo "Three"; break;
case 4: echo "Four"; break; case 5:
echo "Five"; break; case 6: echo "Six";
break; case 7: echo "Seven"; break;
case 8: echo "Eight"; break; case 9:
echo "Nine"; break; default: echo
"Invalid number"; }
?>

Output:
Three

4. Find Largest Number from three number using if else statement.


<?php
$a = 25;
$b = 40; $c =
30;

if ($a >= $b && $a >= $c)


{
echo "$a is the largest";
}
elseif ($b >= $a && $b >= $c)
{
echo "$b is the largest"
}
else
{
echo "$c is the largest";
}
?>

Output:
40 is the largest
5. Write a java program to input electricity unit charges and calculate total Electricity Bill
according to the given condition:
for first 50 unit Rs.0.50/unit
for next 100 unit Rs.0.75/unit
for next 100 unit Rs.1.20/unit
for unit above 2.50 Rs.1.50/unit
An additional subcharge of 20% is added to the bill
import java.util.Scanner;

public class ElectricityBill

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.print("Enter electricity units consumed: ");

int units = sc.nextInt();

double bill = 0;

if (units <= 50)

bill = units * 0.50;

else if (units <= 150)

bill = 50 * 0.50 + (units - 50) * 0.75;

else if (units <= 250)


{

bill = 50 * 0.50 + 100 * 0.75 + (units - 150) * 1.20;

else

bill = 50 * 0.50 + 100 * 0.75 + 100 * 1.20 + (units - 250) * 1.50;

bill += bill * 0.20;


System.out.println("Total Electricity Bill: Rs. " + bill); sc.close();
}
}

Output:
Enter electricity units consumed: 300
Total Electricity Bill: Rs. 420.0

You might also like