after a few exercises of php i found two exercises that i got stuck on, though i have give it many tries.
the 1st one :
Write a simple program to print the digit in unit place of a number entered by the user.
input : Enter Number:3457
output 7
<?php
$answer=0;
echo "Enter Number:";
$number = trim(fgets(STDIN));
//{Write down your logic here
//}
echo $answer;
exit;
?>
the 2nd one :
Write a program to see whether a number is an even multiple of 3.
input : Enter the number:6
output : yes
input : the number:9
output : no
<?php
echo "Enter the Number:";
$number = trim(fgets(STDIN));
//{
//write down your logic here
//}
exit;
?>
Thank you :)
Write a simple program to print the digit in unit place of a number entered by the user.
Assuming the input is type string, you can approach this with character arrays. For example:
//We subtract 1 from strlen() because indexes start at 0
echo (string) $number[strlen($number)-1];
Write a program to see whether a number is an even multiple of 3.
We can use the modulo operator for this, and see if we divide by 3 there is a remainder.
echo ($number % 3 == 0) ? "Yes" : "No";
Although i still think there is a better way to solve this exercise. I was able to pass it using the following solution
<?php
$answer=0;
echo "Enter Number:";
// Converts the number into a double to cover all input cases
$number = (double)trim(fgets(STDIN));
//{Write down your logic here
// Checks to see if a negetive number was entered
if ($number <0){
// Gets the Position of the decimal Point and reduce it by 1 to get
// the position of the Units place
$position = strpos($number, ".") - 1;
// returns the units string and makes it negetive
$answer = substr ($number,$position,1 ) * -1;
}
// follows the same as above, leaves string positive
else {
$position = strpos($number, ".") - 1;
$answer = substr ($number,$position,1 );
}
//}
echo $answer;
exit;
?>
I used this one:
<?php
$answer=0;
echo "Enter Number:";
$number = trim(fgets(STDIN));
$x= $number;
$i= $x;
$m= 0;
if ($x > 0 && $x > 10) {
do {
$x = $x-10;
$m= $m +1;
} while ($x > 10);
$m= $m*10;
$answer=$i-$m; }
elseif ($x < 0){
$i=$x*(-1);
$x=$x*(-1);
do {
$x = $x-10;
$m= $m +1;
} while ($x > 10);
$m= $m*10;
$b=$i-$m;
$answer=$b*(-1);
}
else
$answer=round($number);
//}
echo $answer;
exit;
?>
here is solution for 1st one:
<?php
$answer=0;
echo "Enter Number:";
$numbers = trim(fgets(STDIN));
$number = intval($numbers);
if(is_float($number)){
list($int, $dec) = explode('.', $number);
if($number < 0){
$answer = "-".substr($int, -1);
}
else{
$answer = substr($int, -1);
}
}
else{
if($number < 0){
$answer = "-".substr($number, -1);
}
else{
$answer = substr($number, -1);
}
}
echo $answer;
exit;
?>
solution for 2nd one
<?php
echo "Enter the Number:";
$number = trim(fgets(STDIN));
//{
//write down your logic here
$even = $number % 2; //to check number is even or not
if($even == 0){
$mod = $number % 3; //to check whether it is devisible by 3 or not
if($mod == 0){
echo "yes";
}
else{
echo "no";
}
}
else{
echo "no";
}
//}
exit;
?>
Solution 1
<?php
$answer=0;
echo "Enter Number:";
$number = trim(fgets(STDIN));
$number = intval($number);
$answer = substr($number, (-1));
if($number < 0)
{
$answer = $answer * -1;
}
echo $answer;
exit;
?>
Solution 2
<?php
echo "Enter the Number:";
$number = trim(fgets(STDIN));
$number2 = $number / 3;
if ($number2 % 2 == 0)
{
echo "yes";
}
else
{
echo "no";
}
exit;
?>