Finding the averege in a loop counting to 100 - php

I need to find the averege after using a loop counting ever 3rd to a 100. The loop part is easy enough, but I need to sum every value then divide the sum on the total of values.
for ($x = 3; $x < 100; $x+=3) {
echo $x.", ";
}
This is the loop I need to use. How to I sum the values this produces and how do I find how many values this loop produces?

I believe the intention here is to learn about loops, otherwise this stuff can be done without looping too.
For learning purpose, you can simply introduce two variables count and sum and compute them inside the loop. For count, you just increment it on each iteration. For sum, you add the current value of x into sum. After the loop you print both variables.
$count = 0;
$sum = 0;
for ($x = 3; $x < 100; $x+=3) {
echo $x.", ";
$count++;
$sum+=$x;
}
echo $sum;
echo $count;

add your elements into an array and then use array_sum to sum the array elements , then divide the sum by the count of your array
$arr = [];
for ($x = 3; $x < 100; $x+=3) {
// echo $x.", \n";
$arr[] = $x;
}
print_r(array_sum($arr) / count($arr));
// Output : 51

$i=0;
$tempx=0;
for ($x = 3; $x < 100; $x+=3) {
//total sum
$tempx = $tempx + $x;
//count of how many times the loop ran in this case 33 times
$i++;
}
//first $i was 0 so we add 1
$i=$i + 1;
//getting the average
$average=$tempx / $i;
echo $average;
//output

For the last answer i think we should not do:
//first $i was 0 so we add 1 $i=$i + 1;
Regards

Related

for loop that increases the value of a variable inside the loop based on a variable outside of it

I have a for loop that loops based on the variable $total. I want the variable $skip to increment by 6 (based on $val1) every time it loops. It should display like 6 12 18 24 cause it loops 4 times since we started with 0.
$val1 = 6;
$total = 20 / $val1;
for ($x = 0; $x <= $total; $x++) {
$skip = 0; //Increase every loop based on $val1
echo "$skip" ;
}
Do I need to add another for loop inside the loop to increment the $skip or is there a function for that?
Just add the value of $val1 to $skip will be ok. No need to have another loop.
<?php
$var1 = 6;
$total = 4;
$skip=0;
for ($x = 0; $x < $total; $x++) {
//Increase every loop based on $val1 below
$skip = $skip+ $var1;
echo $skip. " " ;
}
?>

PHP Add numbers 1-10 and only even numbers 1-10. Display sums separately with one loop

I'm trying to use ONE PHP for loop to echo the sum of the numbers 1-10 as well as echo the sum of only the even numbers. I seem to have a problem as these iterations won't be "parallel"
Code:
<?php
$sum = 0; $evensum = 0;
for($x = 1, $y=2; $x<=10, $y<=6; $x++, $y += 2) {
$sum = $sum + $x; $evensum = $evensum + $y;
}
echo "total sum= ". $sum, ", even sum=" . $evensum;
?>
total sum should reflect 55 (1+2+3+4+5+6+7+8+9+10) and even sum should reflect 30 (2+4+6+8+10)
Just Use
<?php
$sum = 0; $evensum = 0;
for($x = 1; $x<=10; $x++) {
// sum all the number
$sum = $sum + $x;
// check the number is even
if( $x % 2 === 0 ) {
// sum only the even numbers
$evensum = $evensum + $x;
}
}
// output
echo "total sum= ". $sum, ", even sum=" . $evensum;
?>
Another approach is using range() and array_functions
$arr=range(1,10);
echo $sum=array_sum($arr);
function even($var)
{
return(!($var & 1));
}
echo $even=array_sum(array_filter($arr, "even"));
Sum all ranges
$all_sum=array_sum(range(1,10));
Sum even number (0,2,4,6,8,10)
$even_sum = array_sum(range(0,10,2));

How to random numbers in php between two block and make the results unique?

I need to create 2 blocks and each block contain number between 1-3. how can i achieve this with php?
the results should be:
1-1
1-2
1-3
2-1
2-2
2-3
3-1
3-2
3-3
many thanks for your help
Nest for loops and echo the loop values.
For($i=1;$i<=3;$i++){ //outer loop
For($j=1;$j<=3;$j++){ // inner loop
Echo $i ."-".$j ."\n"; // echo inner value and outer value
}
}
https://3v4l.org/2Kqbl
I am not sure what do you mean by random, but to get the desired out put you can use nested for loop.
$m=3;
$n=3;
for($i=1;$i<=$m;$i++){
for($j=1;$j<=$n;$j++){
echo $i .'-'. $j."\n";
}
}
Use nested for loop Example:
for($i = 1; $i <= 3 ; $i++){
for($j = 1; $j <= 3; $j++){
echo $i .'-'. $j."\n";
}
}
depends on what do you want to be the relation between that 2 "blocks" or what they are propose for... what you have posted there can be made like this:
$rand = rand(0,3);
for($x = 0; $x <= 9; $x++;){
echo $rand."-".$rand; echo"<br>";
}

Getting total value of an array with condition

I need to get the total value of this array of all the numbers above or equal to 0.
This is the array
$aReeks = array(23,245,1,2,12,-10,46,6,66,9999,-55,348,56,6,66,983);
This is the code i have so far, but it only shows the highest number of the array and doesnt count the values up and shows the total.
$totaal = 0;
for($y=0; $y < count($aReeks); $y++)
{
if($totaal < $aReeks[$y] && $aReeks[$y] > 0)
$totaal = $aReeks[$y];
}
I have to do it with a for loop.
Here's a quick way:
$total = array_sum(array_filter($aReeks, function($n) { return $n > 0; }));
Filter the array for values greater than 0
Sum that array
Oh I now see the "I have to do it with a for loop.", so this won't be accepted for your homework I imagine.
$aReeks = array(23,245,1,2,12,-10,46,6,66,9999,-55,348,56,6,66,983);
$total=0;
for($i =0 ; $i< count($aReeks) ; $i++)
{
if($aReeks[$i]>=0)
{
$total+= $aReeks[$i];
}
}
echo $total ;
?>
Output
11859
You are making 2 major mistakes one is in if condition which is $totaal < $aReeks[$y] you don't need this check at all. Secondly rather than summing up the value of each item to the total of all previous items... you are simply assigning the value to the $totaal variable inside the loop.
$aReeks = array(23,245,1,2,12,-10,46,6,66,9999,-55,348,56,6,66,983);
$totaal = 0;
for($y=0; $y < count($aReeks); $y++)
{
if($aReeks[$y] > 0)
$totaal = $totaal + $aReeks[$y];
}

php loop question

hello i'm beginner for programming I've got a homework. googled it but couldnt find anything...
i need to get the total value of numbers from 1 to 10. this need to be done in loop. but couldn't figure which loop should i use. if you can also give me an example code thats would be great.
This is a homework question, I'm not sure why people are just giving you an answer to copy-paste.
Achieving the sum of numbers 1..10 is pretty simple. You will need to initialise an empty int var before your loop, and for each iteration from 0 up to and including 10 you will add your int var to the current iteration.
For example:
sum = 0;
for num in range 1 to 10:
sum = sum + num;
<?php
$start = 0; // set the variable that will hold our total
for($i=1;$i<11;$i++){ // set a loop, read here: http://php.net/manual/en/control-structures.for.php for more info
$start += $i; // add $i to our start value
}
echo $start; // display our final value
I would use a for loop.
$total = 0;
for($i = 1; $i <= 10; $i++){
$total += $i;
}
Using the for loop:
<?php
$sum = 0;
for($i = 1; $i <= 10; $i++){
$sum += $i;
}
Using the foreach loop:
<?php
$sum = 0;
foreach(range(1,10) as $num){
$sum += $num;
}
echo $sum; // prints 55
And disregarding your assignment, here is an easier way:
echo array_sum(range(1,10));

Categories