i have code :
<?php
$number =9;
$number2 = $number / 2;
for ($b=0; $b<=$number2; $b++){
for ($i=$number; $i>=1; $i--){
echo $i;
}
echo "<br/>";
for ($a=1; $a<=$number; $a++)
{
echo $a;
}
echo "<br/>";
}
?>
I want the result like this .
987654321
123456789
987654321
123456789
987654321
123456789
987654321
123456789
987654321
why if i put odd number like 9 , why always the result 10 loop ?
You have two loops in a loop, therefore you'll always get an even number of lines.
You don't actually need that structure, see:
$number=9;
$k=0;
$sum=1;
for($i=1; $i<=$number; $i++) {
for($j=1; $j<=$number; $j++) {
$k+=$sum;
echo $k;
}
echo '<br>';
//Sum once again so in the next iteration $k is 0 or 10
$k+=$sum;
//Invert the sign of $sum so in the next iteration it substracts, and then adds, and so on
$sum*=-1;
}
Also note that going from 0 to $number inclusive (lower or equal comparation) you'll get one iteration more (from 0 to 9 there are 10 whole numbers).
Solve with this code , thx Gabriel.
$number=9;
$k=0;
$sum=1;
for($i=1; $i<=$number; $i++) {
for($j=1; $j<=$number; $j++) {
$k+=$sum;
echo $k;
}
echo '<br>';
//Sum once again so in the next iteration $k is 0 or 10
$k+=$sum;
//Invert the sign of $sum so in the next iteration it substracts, and then adds, and so on
$sum*=-1;
}
Related
I'm writing program to list odd/even series from n elements (1,2,...n) using if-statement.
For example,
n = 1
Odd Series
1
3
5
7
9
Even Series
0
2
4
6
8
If any possible to print odd/even series without if-statement.
You can just use range with a step of 2, starting with either 0 or 1 as required:
echo "Odd Series\n";
foreach (range(1, 9, 2) as $v) echo "$v ";
echo "Even Series\n";
foreach (range(0, 9, 2) as $v) echo "$v ";
Output:
Odd Series
1 3 5 7 9
Even Series
0 2 4 6 8
Demo on 3v4l.org
Yes. This is possible. We can list odd or even series without if-condition.
We use Increment operator insteed of if-condition.
Sample code is,
<?php
echo "Odd Series";
echo "<pre>";
for ($i=0; $i < 10; $i++) {
echo ++$i;
}
echo "Even Series";
echo "<pre>";
for ($i=0; $i < 10; $i++) {
echo $i++;
}
?>
Sample output is here,
Odd Series
1
3
5
7
9
Even Series
0
2
4
6
8
We could try using a ternary expression in lieu of an if statement:
// even series
for ($i = 0; $i < 10; $i++) {
echo $i % 2 == 0 ? $i : "\n";
}
Another possibility is to just iterate the for loop by steps of 2:
for ($i = 0; $i < 10; $i=$i+2) {
echo $i . "\n";
}
Using arrays, you can create the range with range(), and use array_filter() to pluck the odd or even values using bit-operators.
$n = 8;
$series = range(1, $n);
$odd = array_filter($series, function($value) { return $value & 1; });
$even = array_filter($series, function($value) { return !($value & 1); });
var_dump($odd, $even);
Then its just a matter of looping the arrays $odd and $even.
echo "Odd values: \n";
foreach ($odd as $v) {
echo $v."\n";
}
echo "Even values: \n";
foreach ($even as $v) {
echo $v."\n";
}
Live demo at https://3v4l.org/J9Dio
What I want is the first loop iterating from 1 to 4 and the second loop from 5 to 6.
Here is my code:
<?php
for ($i = 1 ; $i <= 4 ; $i++)
{
echo $i . "<br>";
}
?>
<hr>
<?php
for ($i = 1 ; $i <= 2 ; $i++)
{
echo $i . "<br>";
}
?>
The loops you've given are:
1st loop: from 1 to 4
2nd loop: from 1 to 2
First loop is ok, but seconds needs to be modified. Use $i<=6 and don't initialize $i variable.
This will give you:
1st loop: from 1 to 4
2nd loop: from (value that 1st loop have ended)+1 to 6, so (4+1) to 6, 5 to 6
<?php
$i = 0; // be sure 'i' is visible in both loops
for ($i=1; $i<=4; $i++) // form 1 to 4
{
echo $i . "<br>";
}
?>
<hr>
<?php
$i++; // start from 5, not 4
for (; $i<=6; $i++) // from the previous value to 6
{
echo $i . "<br>";
}
?>
Your problem
The second for loop resets your $i variable to 1:
for ($i = 1 ; $i <= 2 ; $i++)
Solution
You can use a while loop instead of your second for loop:
<?php
for ($i = 1; $i <= 4; $i++)
{
echo $i . "<br>";
}
?>
<hr>
<?php
while ($i <= 6) // `<= 6` instead of `<= 2`, since we keep $1's value
{
echo $i . "<br>";
$i++;
}
?>
Rather than using two loops for this, why not just output the <hr> tag at the appropriate point within the same one? If you carry on with adding extra loops, first of all you'll run into confusing problems like this about (re-)initialising variables, and you'll also quickly end up with a lot of unnecessary duplicated code.
You can use the PHP modulo operator (%) to output the <hr> tag after every fourth element, which will both reduce the complexity and be a lot more extensible if you later add more elements:
for ($i=1; $i<=6; $i++) {
echo $i . "<br>";
if ($i % 4 === 0) {
echo "<hr>";
}
}
See https://eval.in/976102
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 )
What i want to print is
1
3 5
7 9 11
With my current code , that is ...
<?php
function Odd($limit='20'){
$c = 1;
while($c <= $limit){
if ($c % 2!=0){
echo $c ;
echo "<br/>";
}
$c++ ;
}
}
Print Odd();
?>
i am getting
1
3
5
7
9
11
Can someone please guide me the right way ?
Aaah ... ok.^^ Now i got it.
Its pretty easy: You need another variable which counts up and one which limits the breakposition. Looks like this:
<?php
function Odd($limit='40'){
$c = 1;
$count = 0;
$break = 1;
while($c <= $limit){
if ($c % 2!=0){
echo $c . " ";
$count++;
if($count === $break) {
echo "<br/>";
$break++;
$count = 0;
}
}
$c++ ;
}
}
Print Odd();
?>
Output till 40:
1
3 5
7 9 11
13 15 17 19
21 23 25 27 29
31 33 35 37 39
Edit: Code for your new request:
<?php
function Odd($limit='40'){
$c = 1;
$count = 0;
$break = 1;
while($c <= $limit){
echo $c . " ";
$count++;
if($count === $break) {
echo "<br/>";
$break++;
$count = 0;
}
$c++ ;
}
}
Print Odd();
?>
So if I understand correctly you want to output something like that:
1
3 5
7 9 11
13 15 17 19
Here is my solution:
function Odd($limit='20'){
$c = 1;$some_array = array();
while($c <= $limit){
if ($c % 2!=0){
$some_array[]=$c;
}
$c++ ;
}
return $some_array;
}
$array = Odd();
$nr =0;
$j=1;
foreach ($array as $key => $value) {
echo $value.' ';$nr++;
if($nr==$j){
echo '<br />';
$nr=0;
$j++;
}
}
Hope this helps!
From your question it Seems you are really new to programming so before writing any program first of all observe the question properly:
For example for the question above it is clear that is an triangle of odd numbers.
now the number of odd numbers on each row is equal to the row
i.e 1st row contains 1 number ,2nd contains 2 and it continues...
Now what we do is take an variable to count the no of rows say $row and the other will be $limit .
<?php
function odd($limit){
$row=1;
$current_number=1;
while($current_number<=$limit){
for($i=1;$i<=$row;$i++){
echo $current_number." ";
$current_number=$current_number+2;//incrementing numbers by 2 if you want to increment by 1 i.e print all numbers replace 2 by 1
}
$row++;
echo "<br/>";//for new line
}
}
To run above function you need to call it and pass the value of $limit.To do it just type anywhere outside of this function.
odd(20);
Watch this running here:
I am trying to make a triangular-shaped set of lines of decreasing numbers like this :
5
45
345
2345
12345
I tried this :
for($i=1;$i<=5;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo $j;
}
echo "<br>";
}
But it is printing the low number first and appending increasing numbers like this :
1
12
123
1234
12345
The inner loop needs to count down instead of up.
You can either subtract the outer loop's variable from the limit to get the starting point and count down:
for ($i = 0; $i < 5; $i++)
{
for ($j = 5 - $i; $j > 0; $j--)
{
echo $j;
}
echo "<br>";
}
or change the outer loop to count down from the limit as well.
for ($i = 5; $i >= 1; $i--)
{
for ($j = $i; $j >= 1; $j--)
{
echo $j;
}
echo "<br>";
}
This is pretty straightforward:
$max = 5;
echo "<pre>";
for($line=0; $line<$max; $line++) {
$min_this_line = $max-$line;
for($num = $min_this_line; $num <= $max; $num++) {
echo $num;
}
echo "\n";
}
echo "</pre>";
Output:
5
45
345
2345
12345
I think I would declare the $peak value, then use a for() loop to decrement the counter down to 1, and use implode() and range() to build the respective strings inside the loop.
This isn't going to outperform two for() loops, but for relatively small $peak values, no one is going to notice any performance hit.
Code: (Demo)
$peak = 5;
for ($i = $peak; $i; --$i) {
echo implode(range($i, $peak)) , "\n";
}
or with two loops: (Demo)
Decrement the outer loop and increment the inner loop.
$peak = 5;
for ($i = $peak; $i; --$i) {
for ($n = $i; $n <= $peak; ++$n) {
echo $n;
}
echo "\n";
}
Both output:
5
45
345
2345
12345