This question already has answers here:
Test if number is odd or even
(20 answers)
Closed 4 years ago.
I am trying to create this algorithm with PHP. I want to be able to echo out the result of this operation:
1 - 2 + 3 - 4 + 5 - 6 +...100
I want to get the result of this till I get to 100.
This how I have already started the code, however I am stuck and don't know how to proceed:
<?php
$somme = 0;
$I = 1;
while($I <= 100){
}
?>
How do I go on from this?
All answers are appreciated
It's a case of knowing, within the loop, when to add and when to subtract. A simple tracker will help us here.
$somme = 0;
$i = 1;
$op = 'sub';
while($i <= 100){
$somme = $op == 'sub' ? $somme - $i : $somme + $i;
$op = $op == 'sub' ? 'add' : 'sub';
$i++;
}
As noted in the comments, you could also decide the operation based on whether $i is even or odd.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How would you go about answering a question like this? I know I would have to use a loop, but my current answer won't allow me to go over an input of about 5 which is incredibly inefficient. I'm finding nested loops a little daunting.
Given the triangle of consecutive odd numbers:
1
3 5
7 9 11
...
Calculate the row sums of this triangle from the row index (starting at index 1)
rowSumOddNumbers(1); // 1
rowSumOddNumbers(2); // 3 + 5 = 8
Disclaimer - this is not a test questions. Just one of the practice ones I'm struggling to get my head around.
Since posting this I have found a working answer (three hours later...)
Working code:
function rowSumOddNum($n) {
$start = ($n *($n - 1)) + 1;
$sum = 0;
$step = $start +($n*2);
for($a=$start;$a<$step;$a++){
if($a % 2 !== 0){
$sum = $sum + $a;
}
} echo $sum;
}
This was the original code I was working with that kept crashing out saying it didn't have enough memory. Very inefficient I believe.
function rowSumOddNumbers($n) {
$len = $n + ($n*1);
$array = [];
for ($i = 1; $i <= $len; $i+2) {
if ($i % 2 == 1) {
$array[] = $i;
}
}
$count = 0;
$answer = 0;
for ($row = 1; $row < $n; $row++) {
$count = $count + $row;
}
$length = $count + $n;
for ($a = $count; $a <$length; $a++) {
$answer = $answer + $array[$a];
}
echo $answer;
}
I liked the challenge, so here it is.
This should do the job no matter how big the triangle is.
<?php
$triangle = array(1,5,11,9,7,3);
$u = 1;
$results = array();
$sumrow =0;
for($i=0; $i<=count($triangle); $i++){
if($i == count($triangle)){
$sumrow += $triangle[0]; // for the last row add the first number of array
}else{
$sumrow += $triangle[$i];
}
if(is_int($u/3)){ // for every third number store results and use the same number again
$i--;
$u = 1;
array_push($results, $sumrow);
$sumrow = 0;
}else{
$u++;
}
}
echo json_encode($results);
?>
This question already has answers here:
Can you 'exit' a loop in PHP?
(6 answers)
Closed 4 years ago.
How to prevent for loop from exceeding condition?
Loop below returns 25. I want it to return 20.
for ($i = 5; $sum < 23;)
{
echo $sum += $i;
}
This loop is just an example. There will be variables with any value in place of 5 and 23
You have to write your loop differently.
A for loop is designed to run until the conditions evaluates false. In your case, the loop at sum = 20 will still run, becuase sum < 23 evaluates to true. So if you want 20, simply write $sum < 20.
Or if I give it a second thought, you may want to do it like that:
<?PHP
$sum = 0;
for($i = 5; ($sum+$i) < 23;)
{
$sum += $i;
}
echo $sum;
?>
This question already has answers here:
The 3 different equals
(5 answers)
Closed 5 years ago.
I have a strange issue in my site conception...
I try to create a select with 3 input, one for day, one for month and one for year.
The year input work clean, but, if I try that for day:
for($i = 1; $i <= date('t'); $i++) {
if ($i < 10) {
$i = '0'. $i;
}
if ($i = date('d')) {
echo '<option selected>'. $i .'</option>';
} else {
echo '<option>'. $i .'</option>';
}
}
This bug totally my navigator (Firefox up to date version) and I don't understand why...
Change:-
if ($i = date('d')) {//it's assignment not comparison
To:-
if ($i == date('d')) { //or if ($i === date('d')) { now it's comparison
Output:-https://eval.in/860475 OR https://eval.in/860476
Reference:- The three different equals
This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 5 years ago.
I'm trying to make a for loop with numbers given by user ($start and $limit), but I want to be able to write '0001' as $start and '1000' as $limit and print each one of numbers. The problem is, only the first number is printed as '000..' and after increment, those zeros disappear. This is my code:
$start = 0001;
$limit = 1000;
for ($i=$start; $i <= $limit; $i++) {
echo $i.'<br>';
}
outputs:
001
2
3
...
1000
Is there any way to make it as:
0001
0002
...
1000
Try
$start = 1;
$limit = 1000;
for ($i=$start; $i <= $limit; $i++) {
printf("%04d<br>",$i);
}
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
php display number with ordinal suffix
I'm attempting to add ordinal contractions i.e.(st/nd/rd/th) to an increment.
Somehow I need to get the last digit of $i to test it against my if statements...
Here is my code so far:
$i = 1;
while($i < 101 ){
if($i == 1){$o_c = "st";}else{$o_c = "th";}
if($i == 2){$o_c = "nd";}
if($i == 3){$o_c = "rd";}
echo $i.$o_c."<br/>";
$i++;
}
You can use the modulus (%) operator to get the remainder when dividing by 10.
$i = 1;
while($i < 101 ){
$remainder = $i % 10;
if($remainder == 1){$o_c = "st";}else{$o_c = "th";}
if($remainder == 2){$o_c = "nd";}
if($remainder == 3){$o_c = "rd";}
echo $i.$o_c."<br/>";
$i++;
}
What about using the modulus operator: $i % 10?
Display numbers with ordinal suffix in PHP
(that thread has other solutions. I liked that one)