PHP stuck with the loop excercise - php
I am stuck with this php loop. Then n = 3, k = 5, s = 21.
Can anyone help me please ?
Rows 3. In the first row 5 chairs:
1 row: ⑁⑁⑁⑁⑁ (5 chairs)
2 row: ⑁⑁⑁⑁⑁⑁⑁ (7 chairs)
3 row: ⑁⑁⑁⑁⑁⑁⑁⑁⑁ (9 chairs)
Chairs in total: 21
<?php
for ($i=0; $i<=6; $i++) {
for ($j=0; $j<$i; $j++) {
if($i == 1) {
echo '';
}else{
echo '⑁';
}
} echo ' ';
}
?>
If I understand correctly what you want,
$n is number of row you needed,
$k number of chair at row 1.
Each row chair is added by two.
<?php
$n = 3;
$k = 5;
for($i = 0; $i < $n; $i++) {
if($i >= 1) {
echo PHP_EOL;
}
echo str_repeat('-', $k + ($i * 2));
}
Example: When n = 3 and k = 8, it must be obtained that order s = 30 chairs.
Create a PHP solution that specifies the N-row queue in the variables, K-how many chairs should be in the first row.
For example: N = 3; K = 5;
After loading the page (after performing the program actions with the variables available) the following should be displayed:
Rows 3. First row 5 chairs:
Queue 1: ⑁⑁⑁⑁⑁ (5 chairs)
Queue 2: ⑁⑁⑁⑁⑁⑁⑁ (7 chairs)
Queue 3: ⑁⑁⑁⑁⑁⑁⑁⑁⑁ (9 chairs)
Total required chairs: 21
I think you want to use 'if' syntax. Is it right?
If it's not correct, You should describe your work more detail
<?php
// ...
for($i = 0; $i< sizeof($rows); $i++ ){
if ($i % 2 == 0) {
print "It's even";
}
}
// ... If a row is an Array, use this one.
for($i = 0; $i< sizeof($rows); $i++ ){
for($j = 0; $j< sizeof($rows[$i]); $j++ ){
if ($j % 2 == 0) {
print "It's even index of a row";
//do Something.
}
}
}
?>
Related
Can a PHP for loop with number array start with NULL and also have 0?
I am working with some legacy PHP code so re-writing this isn't an option at this point but I have a dropdown for number of years and months of employment and currently they go from 0 - 11, 0 - 65. Can a PHP loop array numbers starting at NULL, which adds -Select- as the default forcing user to make a selection, but also have 0 as the starting number? I've tried: for ($i = NULL; $i <= 11; $i++) { echo $i; } But 0 is no loner an option This is what I have currently: for ($i = 0; $i <= 11; $i++) { echo $i; } I need it to display as: -Select- 0 1 2 3 4 etc
Echo the text before echoing the numbers using a loop. <?php echo '-Select-'; for($i = 0; $i <= 11; ++$i) { echo $i; }
NULL++ would not increase the value of NULL, which is essentially nothing. Why not start at -1, and if ($i == -1) then echo select? It would look like this: for ($i = -1; $i <= 11; $i++) { if ($i == -1) { echo '-Select-'; } else { echo $i; } }
PHP: for loop $i value turns zero if the number is greater
I want to turn the $i variable value to start counting from 1 if the given value is greater than 10: here is what i am trying to achieve <?php $givenValue = 15; //number of x value for ($i = 1; $i < $givenValue; $i++) { if ($givenValue > 10){ $i = 1; } echo $i."<br>"; } ?> This is how i want my result to look like output: 1 output: 2 output: 3 output: 4 output: 5 output: 6 output: 7 output: 8 output: 9 output: 10 output: 1 output: 2 output: 3 output: 4 output: 5 in for loop body Any help is welcome
You can use modulo calculation to get the result you want. I also changed your if from $givenvalue to $i as $givenvalue will "always" be 10+. $givenValue = 15; //number of x value for ($i = 1; $i <= $givenValue; $i++) { if ($i > 10){ Echo $i%10 . "\n"; }else{ echo $i . "\n"; } } https://3v4l.org/5afc5 Another option, if that is possible for you, is to start at zero and only use modulo calculation and add one to it to get the same result. This also means I need to stop the loop at <$givenvalue as your original code shows. $givenValue = 15; //number of x value for ($i = 0; $i < $givenValue; $i++) { Echo $i%10+1 . "\n"; } https://3v4l.org/r0sgA A method that uses less looping is to add 10 to the loop on each iteration and create the values using range(). Then add them to the array with array_merge, and output with implode. $givenValue = 47; //number of x value $breakpoint = 10; $arr=[]; For($i = $breakpoint; $i< $givenValue;){ // Add new values from 1-$breakpoint in array $arr = array_merge($arr, range(1,$breakpoint)); $i +=$breakpoint; } // Loop will exit before all values been collected // Add the rest of the values $arr = array_merge($arr, range(1,$givenValue-($i-10))); // Echo the values in array Echo implode("\n", $arr); https://3v4l.org/jGsO4
Your code can be written like this: <?php $givenValue = 15; //number of x value for ($i = 1; $i <= $givenValue; $i++) { if ($i > 10) { $i = 1; $givenValue-=10; } echo "output: $i\n"; } ?> http://sandbox.onlinephpfunctions.com/code/ed34d8dcd12a9a5a866b73338ad1209f55298519
You are resenting the counter, I would expect the behaviour you have. To do what you want add another counter to the mix $j=1; $givenValue = 15; //number of x value for ($i = 1; $i <= $givenValue; $i++) { if ($j > 10){ $j = 1; } echo $j."\n"; ++$j; } You also had several missing ; Output: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 If you want to end on 5 you have to do 16 as the $givenValue or change it to <= less than or equal See it here live See what I have now, the $i variable counts to the $givenValue then the $j variable counts along side it, but with a range of 1-10 ( resets to 1 after 10 )
forloop continue again after end of the iteration
I used function with same logic given below Given below code is sample of same logic i used, i need to continue the for loop after end of the iteration, i assign 0 to the variable $j at end so forloop need to continue, why its closed the process. for($i=$j;$i<7;$i++){ echo "<br/>".$i; if($i == 6){$j=0;continue;} } Actual Output 1 2 3 4 5 6 Expected output 1 2 3 4 5 6 1 2 3 4 5 6 .....etc My Original code sample is foreach($Qry_Rst as $key=>$Rst_Val){ for($j=$ItrDt;$j<7;$j++){ $ItrDate = date('Y-m-d', mktime(0, 0, 0, $month, $day + $j, $year)); if($ItrDate == $Rst_Val['sloat_day']){ $TimeTableAry[$loop_itr] = $Rst_Val; break } } }
The doc says The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop. So instead of $j just use $i to reset the loop. As this (demo) $j = 1; $current = 0; for ($i=$j; $i<4; $i++) { printf("i: %d, j: %d\n", $i, $j); if ($i==3 && $current < 5) { $i = -1; $j = mt_rand(0,3); $current++; continue; } } shows, you actually need to reset $i = -1; so it will be 0 after $i++ will be evaluated. But with this you'll have an if in every iteration of the loop although you only need it for one. Basically you don't need it for the iteration itself but only to start a next one, so there must be something else here. function doFor($data, $callback) { $dataLength = count($data); for ($i=0; $i<$dataLength; $i++) { call_user_func($callback, $data[$i]); } return $data; } Isolating the loop into its own function will allow for one line that will execute the wanted callback allowing your main code to be something like (demo) $data = array(array("foo","bar"),array("hello"),array("world","!")); function justDump($obj) { var_dump($obj); }; $i = 0; do { $data = doFor($data, 'justDump'); print "<br>"; $i++; } while($i<5);
You can also try the way you originally wanted (only slightly edited): //counter to avoid infinite loop $counter = 0; for($i=$i;$i<7;$i++){ echo "<br/>".$i; if($i == 6){ $i=0; $counter++; continue; } if($counter == 5){break;} } 1 2 3 4 5 6 1 2 3 ...
PHP Loop do action for each 10, 20, 30 etc [duplicate]
This question already has answers here: PHP: How do you determine every Nth iteration of a loop? (8 answers) Closed 3 years ago. In php i have loop e.g. for ($i = 0; $i <= 1000; $i++) { if ($i = 10 || $i == 20 || $i == 30 || $i == 40 || $i == 50 || $i == 60) { echo $i; } } imagine i need to echo $i every 10,20,30,40,50,60,..970,980,990 there should be way to not write 100 conditions in if statement. Is there some logical way to see if $i increased by 10 then do something like: if ($i == $i+10) { ... } P.S. if possible i dont want to introduce another variable to count i need to find solution with using only $i
Try: if ($i % 10 == 0) That will trigger whenever your index is exactly divisible by 10.
rewrite your for loop to: for ($i = 0; $i <= 1000; $i+=10) { And I don't know whether it worked for you with commas like this (as in your initial post): for ($i = 0, $i <= 1000, $i++) {
Skip extra looping: for ($i = 10; $i <= 1000; $i=$i+10) { echo $i; } Or if you still want to loop every single digit between: for ($i = 0; $i <= 1000; $i++) { if( $i % 10 === 0 ) { echo $i; } } Test Here
Put this inside your main loop. '%', or 'mod', gives you the remainder of $i / 10. If the remainder is '0', then you want to print. if(0 === ($i % 10)) { echo $i; }
how to find the sum of all the multiples of 3 or 5 below 1000 in php, issue?
i have an small issue with the way this problem is resolved. some would say: println((0 /: ((0 until 1000).filter(x => x % 3 == 0 || x % 5 == 0))) (_+_)) is the solution witch adds to 233168 my way was to do: $maxnumber = 1000; for ($i = 3; $i < $maxnumber; $i += 3) { $t += $i; echo $i.','; } echo '<br>'; for ($j = 5; $j < $maxnumber; $j += 5) { $d += $j; echo $j.','; } echo '<br>'; echo $t; echo '<br>'; echo $d; echo '<br>'; echo $t+$d; this will give me : 3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147,150,153,156,159,162,165,168,171,174,177,180,183,186,189,192,195,198,201,204,207,210,213,216,219,222,225,228,231,234,237,240,243,246,249,252,255,258,261,264,267,270,273,276,279,282,285,288,291,294,297,300,303,306,309,312,315,318,321,324,327,330,333,336,339,342,345,348,351,354,357,360,363,366,369,372,375,378,381,384,387,390,393,396,399,402,405,408,411,414,417,420,423,426,429,432,435,438,441,444,447,450,453,456,459,462,465,468,471,474,477,480,483,486,489,492,495,498,501,504,507,510,513,516,519,522,525,528,531,534,537,540,543,546,549,552,555,558,561,564,567,570,573,576,579,582,585,588,591,594,597,600,603,606,609,612,615,618,621,624,627,630,633,636,639,642,645,648,651,654,657,660,663,666,669,672,675,678,681,684,687,690,693,696,699,702,705,708,711,714,717,720,723,726,729,732,735,738,741,744,747,750,753,756,759,762,765,768,771,774,777,780,783,786,789,792,795,798,801,804,807,810,813,816,819,822,825,828,831,834,837,840,843,846,849,852,855,858,861,864,867,870,873,876,879,882,885,888,891,894,897,900,903,906,909,912,915,918,921,924,927,930,933,936,939,942,945,948,951,954,957,960,963,966,969,972,975,978,981,984,987,990,993,996,999 5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,125,130,135,140,145,150,155,160,165,170,175,180,185,190,195,200,205,210,215,220,225,230,235,240,245,250,255,260,265,270,275,280,285,290,295,300,305,310,315,320,325,330,335,340,345,350,355,360,365,370,375,380,385,390,395,400,405,410,415,420,425,430,435,440,445,450,455,460,465,470,475,480,485,490,495,500,505,510,515,520,525,530,535,540,545,550,555,560,565,570,575,580,585,590,595,600,605,610,615,620,625,630,635,640,645,650,655,660,665,670,675,680,685,690,695,700,705,710,715,720,725,730,735,740,745,750,755,760,765,770,775,780,785,790,795,800,805,810,815,820,825,830,835,840,845,850,855,860,865,870,875,880,885,890,895,900,905,910,915,920,925,930,935,940,945,950,955,960,965,970,975,980,985,990,995 $t - 166833 $d - 99500 and total: 266333 why am i wrong?
Some numbers are multiples of both 3 and 5. (Your algorithm adds these numbers to the total twice.)
Because 6 * 5 == 30 and 10 * 3 == 30, you're adding the some numbers up twice. $sum = 0; $i = 0; foreach(range(0, 999) as $i) { if($i % 3 == 0 || $i % 5 == 0) $sum += $i; }
Because you double-count numbers that are multiple of both 3 and 5, i.e. multiples of 15. You can account for this naively by subtracting all multiples of 15. for ($j = 15; $j < $maxnumber; $j += 15) { $e += $j; echo $j.','; } $total = $total - $d;
In your case, if it is 15, you will add the number twice. Try this: $t = 0; $d = 0; for ($i = 0; $i <= $maxnumber; $i++){ if ($i % 3 == 0) $t+= $i; else if ($i % 5 == 0) $d += $i; } echo $t.'<br>'.$d;
I think that in your code, if a number is a multiple of 3 and 5, it is added twice. Take 15 for example. It's in your list of multiples of 3 and in the list of multiples of 5. Is this the behaviour you want?
One of the best approach to this solution (to achieve optimum time complexity), run an Arithmetic Progression series and find the number of terms in all series by using AP formula: T=a+(n-1)d, then find sum by : S=n/2[2*a+(n-1)d] where : a=first term ,n=no. of term , d=common deference, T=nth term The code solution below has been implemented to suit the question above - so the values 3 and 5 are hard-coded. However, the function can modified such that values are passed in as variable parameters. function solution($number){ $val1 = 3; $val2 = 5; $common_term = $val1 * $val2; $sum_of_terms1 = calculateSumofMulitples($val1,$number); $sum_of_terms2 = calculateSumofMulitples($val2,$number); $sum_of_cterms = calculateSumofMulitples($common_term,$number); $final_result = $sum_of_terms1 + $sum_of_terms2 - $sum_of_cterms; return $final_result; } function calculateSumofMulitples($val, $number) { //first, we begin by running an aithmetic prograssion for $val up to $number say N to get the no of terms [using T=a +(n-1)d] $no_of_terms = (int) ($number / $val); if($number % $val == 0) $no_of_terms = (int) ( ($number-1)/$val ); //since we are computing for a no of terms below N and not up to/exactly/up to N. if N divides val with no remainder, use no of terms = (N-1)/val //second, we compute the sum of terms [using Sn = n/2[2a + (n-1)d] $sum_of_terms = ($no_of_terms * 0.5) * ( (2*$val) + ($no_of_terms - 1) * $val ); // sum of multiples return $sum_of_terms; }
You can run a single loop checking whether the number is multiple of 3 OR 5: for ($i = 0; $i < $maxnumber; $i++) { if($i%3 || $i%5){ $t += $i; echo $i.',';} }
I think the original code is not including numbers which are multiples of both 3 and 5 in the total: if the test for multiple of 3 matches, it takes that and goes on. If you total the multiples of 15 up to 1000, you get 33165, which is exactly the difference between your total, 266333, and the original total, 233168.
Here's my solution to the question: <?php $sum = 0; $arr = []; for($i = 1; $i < 1000; $i++){ if((int)$i % 3 === 0 || (int)$i % 5 === 0) { $sum += $i; array_push($arr,$i); } } echo $sum; echo '<br>'; print_r($arr);//Displays the values meeting the criteria as an array of values