Skip to content

Instantly share code, notes, and snippets.

@mehedicsit
Forked from queky18/Day 0: Hello, World.
Created October 30, 2019 09:35
Show Gist options
  • Save mehedicsit/03c3a7c6ac0e23380d015a3bc6bd9fd1 to your computer and use it in GitHub Desktop.
Save mehedicsit/03c3a7c6ac0e23380d015a3bc6bd9fd1 to your computer and use it in GitHub Desktop.
Hackerrank - 30 days of code in PHP
<?php
$_fp = fopen("php://stdin", "r");
$inputString = fgets($_fp); // get a line of input from stdin and save it to our variable
// Your first line of output goes here
print("Hello, World.\n");
// Write the second line of output
print($inputString);
fclose($_fp);
?>
$i2 = fgets($handle);
$d2 = fgets ($handle);
$s2 = fgets ($handle);
printf("%d\n", $i + $i2);
printf("%.1f\n", $d + $d2);
printf($s.$s2."\n");
<?php
$_fp = fopen("php://stdin", "r");
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
$mealCost = fgets($_fp);
$tipPercent = fgets($_fp) / 100 * $mealCost;
$taxPercent = fgets($_fp) / 100 * $mealCost;
$totalCost = round($mealCost + $tipPercent + $taxPercent);
echo "The total meal cost is ". $totalCost. " dollars.";
?>
<?php
$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$N);
if(($N % 2 !==0) || (($N % 2 == 0) && ($N < 21) && (5 < $N))) {
print "Weird";
} elseif ((($N % 2 == 0) && (1<$N) && ($N < 6)) || (($N > 20)&& ($N % 2 ==0))) {
print "Not Weird";
}
?>
<?php
class Person{
public $age;
public function __construct($initialAge){
// Add some more code to run some checks on initialAge
if($initialAge < 0){
$this -> age = 0;
print("Age is not valid, setting age to 0.\n");
} else {
$this-> age = $initialAge;
}
}
public function amIOld(){
// Do some computations in here and print out the correct statement to the console
if($this->age < 13){
echo "You are young.\n";
}elseif($this->age < 18){
echo "You are a teenager.\n";
} else {
echo "You are old.\n";
}
}
public function yearPasses(){
// Increment the age of the person in here
$this->age++;
}
}
<?php
$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$N);
fgets($handle);
for($i = 1; $i < 11; $i++){
$result = $N * $i;
echo "$N x $i = " . $result . "\n";
}
?>
<?php
$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
$arr_temp = fgets($handle);
$arr = explode(" ",$arr_temp);
array_walk($arr,'intval');
$array = array_reverse($arr, true);
foreach($array as $key=>$value){
print_r($value." ");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment