Jump to content

multiplication in a loop


adamgonge

Recommended Posts

im working on a problem that involves a loop that goes to 64 with each time the number is doubling itself so it would be 1,2,4,8,16.. I dont know how to do it .

I currently have this code which is wrong it just ends up taking 64*2 but i dont know how else to set this up. 

 

 

 
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Multiplication test</p>'; ?>
<?php
 
$total = 0;
$even = 0;
 
for ( $x = 1; $x <= 64; $x++ ) {
 
    $sum = 1+$x*2;
 
  }
 
echo "The total sum: ".$sum."<br />";
 
?>
 

Link to comment
https://forums.phpfreaks.com/topic/285954-multiplication-in-a-loop/
Share on other sites

i changed the equation but now it just ends up with 64squared whereas i think i need the sum of all the squares.

 

new

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Multiplication test</p>'; ?>
<?php
 
$total = 0;
$even = 0;
 
for ( $x = 0; $x < 64; $x++ ) {
 
    $sum =($x+1*2)*$x;
 
  }
 
echo "The total sum: ".$sum."<br />";
 
?>
 
 
</body>
</html>

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.