What does this mean and how to resolve it ? - 3.55687428096E+14 - php

Ok, so I need this number 3.55687428096E+14 to actually look like a proper whole number. I am not sure what to do in order to make it look like a whole number so that I can go about with my coding. Basically, without resolving this number 3.55687428096E+14, I cant proceed properly. Below is my code, please help!
<?php
$num = 17;
$fact = $num;
echo "The factorial of $num is $num X ";
while ($num > 1){
--$num;
if ($num != 1){
echo "$num X ";
}
else{
echo "$num <b>equals</b> ";
}
$fact = $fact * $num;
}
echo $fact ."<br/>";
?>
By the way, the number 3.55687428096E+14 is the result I am getting after running the program. It comes ok till factorial 16 but once it turns to 17, things become 3.55687428096E+14!

You can user number_format() function if your number is goes very large.
number_format():
The number_format() function formats a number with grouped thousands.
Your code
<?php
$num = 17;
$fact = $num;
echo "The factorial of $num is $num X ";
while ($num > 1){
--$num;
if ($num != 1){
echo "$num X ";
}
else{
echo "$num <b>equals</b> ";
}
$fact = $fact * $num;
}
echo number_format($fact) ."<br/>";
?>

Change the last line with :
echo number_format($fact,0) ."<br/>";

#Mark Baker: - my code below which was executed successfully thanks to your info and support:
<?php
echo "<h2>Find the sum of the digits in the number 100!</h2>"."<br/>";
$num = 100;
$fact = $num;
echo "The factorial of $num is $num X ";
while ($num > 1){
--$num;
if ($num != 1){
echo " $num X ";
}
else{
echo " $num";
}
$fact = bcmul($fact,$num); //right over here!
}
echo " which equals" ."<br/>". "<h3>$fact</h3>";
//proceeding now to calculate the sum of the digits of the factorial!
$_array = str_split($fact);
$sum = array_sum($_array);
echo "The sum of the digits of the factorial is " ."<br/>";
echo "<h3>$sum</h3>";
?>

Related

I am trying to set boundary for my autoincrement function

I am trying to set boundary for my autoIncrement fuction and everything looks right but nothing is showing on my screen. I also tried to use var_dump() function but its not showing any result. Please, what am I missing?
I invoke the function and it is not showing any result. I also tried to dump the data using var_dump() function but its still the same error.
below is the snippet
<?php
function autoIncrement(){
$num = 0;
if($num > 10){
echo "Maximum number selected";
}else{
for ($num; $num <=10; $num++){
if($num > 10){
echo "Maximum number selected";
}else{
echo $num . ", ";
}
//var_dump($num);
}
}
autoIncrement();
}
?>
You made a mistake when you invoke your function "autoIncrement()". You dont invoke a function inside your function but outside of it.
see below code
<?php
function autoIncrement(){
$num = 0;
if($num > 10){
echo "Maximum number selected";
}else{
for ($num; $num <=10; $num++){
if($num > 10){
echo "Maximum number selected";
}else{
echo $num . ", ";
}
//var_dump($num);
}
}
}
autoIncrement();
?>

php replacing m with all 6 zeros returned by variable in php

I have a variable that adds and returns numbers actually this numbers increases with time.
Now I want that if the numbers get to say 2000000 (2 millions) it should remove all the zeros and return 2m.
Any ideas how I can go about this ?
you can use the following function
function millionType($value){
return ($value / 1000000)."m";
}
echo millionType(1500000); // 1.5m
echo "<br/>";
echo millionType(2000000); // 2m
echo "<br/>";
echo millionType(2500000); // 2.5m
echo "<br/>";
echo millionType(3000000); // 3m
Edit: you can use this, you can change it to your desire type.
function millionType($value){
$million = (int)($value / 1000000);
$surplus = ($value % 1000000);
$myType = "";
if($million > 0)
$myType .= $million."m ";
$thousands = (int)($surplus / 1000);
if($thousands > 0)
$myType .= $thousands."k ";
$lastPart = ($surplus % 1000);
if($lastPart > 0)
$myType .= "and ".$lastPart;
echo $myType;
}
echo millionType(1500001); // 1m 500k and 1
echo "<br />";
echo millionType(1000001); // 1m and 1
echo "<br />";
echo millionType(10000540); // 10m and 540
Question is a duplicate of Shorten long numbers to K/M/B?
Neverthless here is my answer:
function shorten($number){
$suffix = ["", "K", "M", "B"];
$percision = 1;
for($i = 0; $i < count($suffix); $i++){
$divide = $number / pow(1000, $i);
if($divide < 1000){
return round($divide, $percision).$suffix[$i];
break;
}
}
}
echo shorten(1000);

How to print one message from a loop within a condition?

Let's say I have this loop:
for($i = 1; $i <= 10; $i++){
if($i > 4){
echo 'Greater Than 4.' . '<br/>';
}
}
The previous loop will output the following:
Greater Than 4.
Greater Than 4.
Greater Than 4.
Greater Than 4.
Greater Than 4.
Greater Than 4.
As the condition is true 6 times.
What I want to do is to print this message only once so the output from the same loop and condition will be :
Greater Than 4.
I think this could be achieved by using a variable and give it this value at a specific point , But I don't know how to do it.
Well, it will be like your code expect that you print the message once:
$msg = 'There is no greater than 4';
for($i = 1; $i <= 10; $i++){
if($i > 4){
$msg = 'Greater Than 4.' . '<br/>';
// Make any additional required code here...
}
}
echo $msg;
;
Have you tried this :)
for($i=1;$i<=10;$i++){
echo ($i>4 && !isset($rslt))?$rslt='Greater Than 4.':'';
}
Or, you can loop first
then print the result
like this
$bucket = '';
for($i=1;$i<=10;$i++){
$bucket = ($i>4)?'Greater Than 4.':$bucket;
}
echo $bucket;
Hmmmm, I guess you case like this :D
$bucket = $condition = '';
$flag = 1;
for($i=1;$i<=10;$i++){
if($i>4){
$bucket = 'Greater Than 4. <br>';
$condition .= "Proses :{$flag}<br>";
$flag++;
//more condition process 6 times
}
}
echo $condition;
echo $bucket;
Or like this :D
$j=1;
for($i=1;$i<=10;$i++){
if($i>4){
if($i<=5){
echo 'Greater Than 4. <br>';
}
echo 'Proses '.($j++).'<br>';
//more condition process 6 times
}
}
Or, using the short hand
for($i= $j =0;$i<=10;$i++){
if($i>4){
echo ($i<=5)?'TheGreater Than 4. <br>':false;
$j++;
echo "Process number : {$j} <br>";
}
}
$run_once = true;
for($i = 1; $i <= 10; $i++){
if($i > 4 AND $run_once){
echo 'Greater Than 4.' . '<br/>';
$run_once = false;
}
}
you could also use if ($i == 4)
EDIT:
for($i = 1; $i <= 10; $i++){
if($i > 4){
if($i == 4){
echo 'this runs only once';
}
echo 'this runs every time';
}
}

how to determine the sequence number of odd /even numbers using php

how to determine the sequence number of odd /even numbers using php ?
example ,i want output like here
odd numbers (1,3,5,7,9)
output
1 = 1
3 = 2
5 = 3
7 = 4
9 = 5
even numbers (2,4,6,8,10)
output
2=1
4=2
6=3
8=4
10=5
how code to make this function in php?
edit/update
if input 1 then output = 1 , if input 3 then output = 2, if input 21 then output= 11, etc,,
Try out this
php code
<?php
$result = '';
if(isset($_POST['value'])){
//assign POST variable to $num
$num = $_POST['value'];
$count = 0;
//for even numbers
if($num % 2 == 0){
$count = $num/2;
$result = "The Number ".$num." is Even on ".$count;
}else {
//if you know about sequences and series, you can understand it :P
$count = (($num-1) / 2)+1;
$result = "The Number ".$num." is Odd on ".$count;
}
}
?>
HTML COde
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<input type="text" name="value"/>
<input type="submit" value="Check"/><br/>
<?php echo $result;?>
</form>
It is working correctly :P
<?php
$evenLimit = 10;
$oddLimit = 9;
$count = 1;
for ($x = 1; $x <= $oddLimit; $x = $x + 2) {
echo $x . "=" . $count . "<br>";
$count++;
}
echo "<br>";
$count = 1;
for ($x = 2; $x <= $evenLimit; $x = $x + 2) {
echo $x . "=" . $count . "<br>";
$count++;
}
?>

deleting white space at the end of for loop output in php

can any body help me how I can delete the white space at the end of the output.
This might be easy question but to be honest it took me more than 45 minutes and still nothing. Assignment
Write a PHP script that prints the numbers from the number inputted on form to zero. The numbers should be separated with a single space but the last number, zero, should not have a space after it. If the user inputs a number smaller than zero, print “The number should be at least zero!” The used form looks like this:
Luku:
Example output
5 4 3 2 1 0
my code:
<?php
$number=$_GET["number"];
if ($number < 0){
echo "The number should be at least zero!";
} else {
for($i=$number; $i>=0; $i=$i-1)
{
echo $i." ";
}
}
?>
You can use trim()
<?php
$number=$_GET["number"];
if ($number < 0){
echo "The number should be at least zero!";
} else {
$num = '';
for($i=$number; $i>=0; $i=$i-1)
{
$num .= $i." ";
}
echo trim($num);
}
?>
Try this:
echo implode(" ",range($number,0));
Magic and trickery ;)
You can use rtrim instead of trim to delete white spaces at the end of the string:
<?php
$number=$_GET["number"];
if ($number < 0){
echo "The number should be at least zero!";
} else {
for($i=$number; $i>=0; $i=$i-1)
{
$num .= $i." ";
}
}
$num = rtrim($num);
?>
You could use an if-statement:
<?php
$number=$_GET["number"];
if ($number < 0) {
echo "The number should be at least zero!";
} else {
for($i=$number; $i>=0; $i=$i-1)
{
echo $i . ($i != 0 ? " " : "");
}
}
Or you use trim:
<?php
$number=$_GET["number"];
$output = "";
if ($number < 0) {
echo "The number should be at least zero!";
} else {
for($i=$number; $i>=0; $i=$i-1)
{
$output .= $i . " ";
}
}
echo trim($output);
try this:
<?php
$number=$_GET["number"];
if ($number < 0) {
echo "The number should be at least zero!";
} else {
$string = $number;
for($i=$number-1; $i>=0; $i=$i-1)
{
$string .= " $i";
}
}
echo $string;
?>

Categories