Jump to content

Subtraction question


soupi

Recommended Posts

Hi i need to Echo out this statement,

 

Starting weight is: 180 Starting body fat is: 15%
After losing 1 pounds of fat Weight is now 179 body fat is now: 14.53%
After losing 2 pounds of fat Weight is now 178 body fat is now: 14.04%
After losing 3 pounds of fat Weight is now 177 body fat is now: 13.56%
After losing 4 pounds of fat Weight is now 176 body fat is now: 13.07%
After losing 5 pounds of fat Weight is now 175 body fat is now: 12.57%
After losing 6 pounds of fat Weight is now 174 body fat is now: 12.07%

 

So this is what I have, im not sure how to subtract 1 pound to output 179 and so forth, any help and advice would be appreciated.

 

Thank you.

 

 

<?php

$start_weight = 180;
$start_body_fat = 27;
$start_body_fat_percent = $start_body_fat/$start_weight * 100;
$pounds_lost = 15;
$first_number= 1;

echo "Starting weight is: ".$start_weight." Starting body fat is: ".$start_body_fat_percent."%";
echo "<br>";  
echo "After losing 1 pounds of fat weight is now: "$start_weight"- ".$first_number." Starting body fat is: ".$start_body_fat_percent."%";


?>

Link to comment
https://forums.phpfreaks.com/topic/282685-subtraction-question/
Share on other sites

You'll want to use a for loop. The code for the loop will be like

for($i = 1; $i <= $pounds_lost; $i++)
{
    // work out new body weight, take $i from $body_weight
    // recalculate body fat percentage, take $i from $start_body_fat and divide by new body weight and times by 100
    // output how many pounds lost ($i), new weight and body fat percentage
}

so would it be like this?

 

<?php

$start_weight = 180;
$start_body_fat = 27;
$start_body_fat_percent = $start_body_fat/$start_weight * 100;
$pounds_lost = 15;


echo "Starting weight is: ".$start_weight." Starting body fat is: ".$start_body_fat_percent."%";
for ($i =1; <= $pounds_lost; $i++);

{
echo "After losing 1 pounds of fat weight is now: $number1" ; echo " & Starting body fat is: ".$start_body_fat_percent1."%";
    
    
    
}
?>
 

Not quite, the loop needs to recalculate the weight and body fat on each iteration.

for($i = 1; $i <= $pounds_lost; $i++)
{
    //work out new body weight, take $i from $body_weight
    $new_weight = calculate new weight here;

    //recalculate body fat percentage, take $i from $start_body_fat and divide by new body weight and times by 100
    $new_body_fat_percent = calculate new body fat percent here;

    // output how many pounds lost ($i), new weight and body fat percentage
    echo "After losing $i pounds of fat weight is now: $new_weight, body fat is: $new_body_fat_percent%";
}

On each iteration of the loop you're giving new values to $new_weight and $new_body_fat_percent. The output from the loop will then look like

 

After losing 1 pounds of fat Weight is now 179 body fat is now: 14.53%

After losing 2 pounds of fat Weight is now 178 body fat is now: 14.04%
After losing 3 pounds of fat Weight is now 177 body fat is now: 13.56%
After losing 4 pounds of fat Weight is now 176 body fat is now: 13.07%
After losing 5 pounds of fat Weight is now 175 body fat is now: 12.57%
After losing 6 pounds of fat Weight is now 174 body fat is now: 12.07%

Do you know what for loop do? have you read http://php.net/for yet? Learn how the for loop works then try again.

 

I have told you what you need to do to calculate the new weight and body fat. Loook at the lines that start with // in the code I posted.

 

<?php

 

$start_weight = 180;

$start_body_fat = 27;

$start_body_fat_percent = $start_body_fat/$start_weight * 100;

$pounds_lost = 15;

 

 

echo "Starting weight is: ".$start_weight." Starting body fat is: ".$start_body_fat_percent."%";

for($i = 1; $i <= $pounds_lost; $i++)

{

    //work out new body weight, take $i from $body_weight*/

    $new_weight = $i-$pounds_lost

 

    //recalculate body fat percentage, take $i from $start_body_fat and divide by new body weight and times by 100

    $new_body_fat_percent = $i-$start_body_fat/$new_weight *100;

 

    // output how many pounds lost ($i), new weight and body fat percentage

    echo "After losing $i pounds of fat weight is now: $new_weight, body fat is: $new_body_fat_percent%";

}

    

?>

 

 

I have a sytax on the red line, I kno what for loop does, but this is my first example. Im sorry

Im receiving a syntax error on this line

    echo "After losing $i pounds of fat weight is now: $new_weight, body fat is: $new_body_fat_percent%";

,

can you atleast show me how to print the first statement

After losing 1 pounds of fat Weight is now 179 body fat is now: 14.53%
After losing 2 pounds of fat Weight is now 178 body fat is now: 14.04%

Fixed code.

<?php

$start_weight = 180;
$start_body_fat = 27;
$start_body_fat_percent = $start_body_fat/$start_weight * 100;
$pounds_lost = 15;


echo "Starting weight is: ".$start_weight." Starting body fat is: ".$start_body_fat_percent."%<br />";
for($i = 1; $i <= $pounds_lost; $i++)
{
    //work out new body weight, take $i from $body_weight*/
    $new_weight = $start_weight - $i;

    //recalculate body fat percentage, take $i from $start_body_fat and divide by new body weight and times by 100
    $new_body_fat_percent = ($start_body_fat - $i)/$new_weight *100;

    // clean up $new_body_fat_percent;
    $new_body_fat_percent = number_format($new_body_fat_percent, 2, '.', ',');

    // output how many pounds lost ($i), new weight and body fat percentage
    echo "After losing $i pounds of fat weight is now: $new_weight, body fat is: $new_body_fat_percent%<br />";
}
    
?>

GOT IT

<?php
$start_weight = 180;
$start_body_fat = 27;
$start_body_fat_percent = $start_body_fat/$start_weight * 100;
$pounds_lost = 15;


echo "Starting weight is: ".$start_weight." Starting body fat is: ".$start_body_fat_percent."%";


for($i = 1; $i <= $pounds_lost; $i++)
{
    //work out new body weight, take $i from $body_weight
    $new_weight = $start_weight - $i;

    //new body fat percentage, take $i from $start_body_fat and divide by new body weight
    $new_body_fat_percent = (($start_body_fat - $i) / $new_weight) * 100;

    // new weight and body fat percentage
    echo "<br/>After losing $i pounds of fat weight is now: $new_weight, body fat is: $new_body_fat_percent%";
}
?>
 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.