Fundamental Programming

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Nama : Dawwas Kamal Fasyabillah

Programming Fundamental

1. Write a program to convert number of days to years, weeks and days from the given number.
Example.
Data : 373 days
Output :373 days = 1 year(s), 1 Week(s) and 1 day(s)

$days = 373;
$days / 365 = $years;
$weeks = ($days % 365) / 7;
$days1 = $days (($years * 365 + ($weeks * 7);

Echo year/s : . $years ;


Echo week/s: $weeks;
Echo day/s: . $days1;

2. Write a program to calculate diameter, circumference and area of the circle from the given
radius. Example.
Data : radius = 10,
Output : Diameter : 20 Circumference : 62.79
Area : 314

$radius = 10

$diameter = 2 * $radius
Echo $diameter
$Circumference = 2 * (22/7) * ($radius * $radius)
Echo $circumference
$area = 22/7 * $radius * 2

3. If percentage >= 90% : Grade A


If percentage >= 80% : Grade B
If percentage >= 70% : Grade C
If percentage >= 60% : Grade D
If percentage >= 40% : Grade E
If percentage <40% : Grade F

Example.
Data :
Physics = 95
Chemistry = 90
Biology = 87
Math = 93
Computer = 90

$data = (array( 95, 90, 87,93, 90))

$sum = array_sum ($data)


$percentage = $sum/count($data)

Echo percentage : $percentage


If ( $data >= 90) { echo Grade A}
Elseif ($data> = 80) {echo Grade B}
Elseif ($data>= 70) {echo Grade C}
Elseif ($data>= 60) {echo Grade D}
Elseif ($data>= 40) {echo Grade E}
Elseif ($data< 40) {echo Grade F}

4.
Write a program to check if the sales is profit or loss from two variable, c
ost price and sales price
of the product.
Example 1.
Data
:
Sales Price = 1000
Cost Price = 800
Output : Profit = 200
Example 2.
Data
:
Sales Price = 950
Cost Price = 1000
Output : Loss = 50

$cost = 800
$sales = 1000
$output = 0

If ( $cost > $sales) { echo loss . $cost - $sales = $output}


Else {echo profit . $sales - $cost = $output}

5.
6.
7. Write a to check whether the number is prime number or not.
Example.
Data : 17
Output
:
17 is prime number

$num = 17

$Check = True;
for ($i = 2; $i < $num; $i++)
{
if ($num % $i == 0)
{
$bCheck = False;
break;
}
if ($bCheck)
{ echo 'Prime';}
else
{echo 'NOT prime'; }

8.

9. Write a function to check whether the given number is even or odd using. Example.

Data : 10 Output : Even

$num = 10

if ($num % 2 == 0)

{
Echo is even number
}
elseif ($num % 2 != 0)
{echo is odd number; }

10. Write a recursive function to calculate factorial from the given number. Example.

Data : 5 Output :

Factorial of 5 = 120
$number = 5

function factorial($number) {

if ($number < 2) {

return 1;

} else {

return ($number * factorial($number-1));

11. Write a recursive function to calculate sum of digits of any number.

Example.

Data : 22345

Output : Sum of digits = 16

Function SumDigits ($var)

if ($var == 0;) {

return 0

if ($var !=0;) {

return (number%10) + (number/10)

12. $input = 5;

for ($i=$input; $i >= 1; $i--)

for ($j=$input; $j > $i; $j--)


{

echo "_"; }

for ($k=1; $k <= (2 * $i - 1) ; $k++)

echo "*";

echo "<br>";}

?>

You might also like