can you guys please help me for this? I want to loop numbers from 1 to 100..
if the number is multiple of 4 it will print 'NEW', if number is multiple of 7 it will print 'TEST', and if the number is multiple of both 4 and 7 it will print 'NEWTEST'.
I've output the multiple of 4 and multiple of 7 but in both 4 and 7 I cant print the 'NEWTEST'.
Here's my code.
Thank you guys
function primeno($n){
for($i = 1; $i < 100; $i++){
if ($i % 4 == 0){
echo 'easy<br>';
}else if($i % 7 == 0){
echo 'EMPLOYER<br>';
}
else if($i % 4 == 0 && $i % 7 == 0){
echo 'easyEMPLOYER<br>';
}else{
echo $i."<br>";
}
}
}
primeno(100);
Here's my output:
1
2
3
NEW
5
6
TEST
NEW
9
10
11
NEW
13
TEST
15
NEW
17
18
19
NEW
TEST
22
23
NEW
25
26
27
NEW ----> it should be NEWTEST
29
30
No. 28 should be output 'NEWTEST' but instead it output NEW
You just need to swap your conditions in your "if" and your last "else if" statements
<?php
function primeno($n) {
for ($i = 1; $i < 100; $i++) {
if ($i % 4 == 0 && $i % 7 == 0) {
echo 'easyEMPLOYER<br>';
}
else if ($i % 7 == 0) {
echo 'EMPLOYER<br>';
}
else if ($i % 4 == 0) {
echo 'easy<br>';
}
else {
echo $i . "<br>";
}
}
}
primeno(100);
?>
Related
I tried to do a while inside a while to print a multiplication table like,
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
But I got only 1, 2, 3, 4, 5.
Code:
$i = 1;
$x = 1;
while($i <= 5){
while($x <= 5){
echo $i * $x;
$x++;
}
echo "<br>";
$i++;
}
This is happening because you're not resetting $x when the inner loop completes its iteration. Try this instead:
$i = 1;
while($i <= 5) {
$x = 1;
while($x <= 5) {
echo $i * $x;
$x++;
}
echo "<br>";
$i++;
}
You need to reset $x, so:
$i = 1;
$x = 1;
while($i <= 5){
while($x <= 5){
echo $i * $x;
$x++;
}
$x = 1; // added this line
echo "<br>";
$i++;
}
Output:
12345
246810
3691215
48121620
510152025
You can then do what ever you want to format it.
More elabrate explanation:
First run:
It enters both outer and inner loops, showing the desired output for the first line. You end up with $i = 2 and $x = 6.
Second run:
Since $i is 2, it doesn't leave the outer loop, but $x is 6, so it doesn't enter the inner loop again.
Last* run:
It then keeps adding 1 to $i until it doesn't match the outer loop condition anymore and leaves you with that unwanted result.
Use this
This is because you have not initialized your $x after external while loop completes its one cycle. so after one cycle inner loops does not run
<?php
$i = 1;
while($i <= 5) {
$x = 1;
while($x <= 5) {
echo $i * $x;
$x++;
}
echo "<br>";
$i++;
}
DEMO ONLINE
php code:
$i = 1;
while($i <= 5){
$x = 1;
while($x <= 5){
echo $i * $x." ";
$x++;
}
echo "<br/>";
$i++;
}
result:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
I want this output:
1 1 1 1 1 1
2 2 2 3 3 3
4 4 5 5 6 6
7 8 9 10 11 12
I think I need to three nested For(), But I don't know how should I print the above result. Here is my code, How to complete it? (though I don't know, maybe my code is completely wrong)
for ($i=1; $i<=4; $i++) // row
{
for ($j=1; $j<=6; $j++) // column
{
for($z=1; $z<=12; $z++) // number
{
// what should be in here?
}
}
}
Edit: I want something like these examples: (Although these examples are very simple, what I want is a little more harder)
for ($i=1; $i<=4; $i++)
{
for ($j=1; $j<=6; $j++)
{
echo $i.' ';
}
echo '<br>';
}
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
Or this: echo $j;
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
Edit2:
Note: I need to a code that be able to print this either: (its logic is the same with first output)
4 4 4 4 4 4
5 5 5 6 6 6
7 7 8 8 9 9
10 11 12 13 14 15
You can try something like this.
var $c = 1;
for ($i=1; $i<=4; $i++)
{
var $noOfChanges = 6/$i;
for ($j=1; $j<=6; $j++)
{
echo $c.' ';
if($j%$noOfChanges==0){
$c = $c + 1;
}
}
echo '<br>';
}
Not tested.
You can intialize the var $c = 4; to get the next pattern.
Tested and working:
$length = 6;
$row = 0;
$number = 1;
$total = 0;
$n = $length;
while(true) {
$n = floor($length/($row+1));
for($i = 0; $i<$n; $i++) {
echo $number;
echo "\t";
}
$total+=$n;
if($total >= $length) {
$row++;
$total = 0;
echo "\n";
if($n == 1 ) break;
}
$number++;
}
Output:
3
3 4
3 4 5
3 4 5 6
3 4 5 6 7
3 4 5 6 7 8
function Triangle ($begin, $end) {
if ($begin < 0 || $end < 0) {
return;
}
if ($begin == $end) {
return $a;
}
else {
// non recursive
for ($i = 1; $i <= $end; $i++) {
for ($j = $begin; $j <= $i; $j++) {
echo $j . " ";
}
echo "<br>";
}
}
}
This is what I made so far.
Here's one way:
function triangle ($begin, $end, $row = 1) {
//stop when we've printed up to end
if($end - $begin + 1 < $row) return;
//let's start at the beginning :)
for($i = 0; $i < $row; $i++){
//the row number increments each time so we can keep adding.
echo ($begin + $i)." ";
}
echo "<br>";
//now recurse...
triangle($begin, $end, $row + 1);
}
Usage:
triangle(3,9);
Output:
3
3 4
3 4 5
3 4 5 6
3 4 5 6 7
3 4 5 6 7 8
3 4 5 6 7 8 9
This should work for you:
(Here I just added the variable step which defines how many steps you make from $begin to $end and if $begin + $step == $end the function is done. If not It starts from $begin and makes X steps and as long as it doesn't reach the end I call the function again with a step more)
<?php
function Triangle($begin, $end, $step = 0) {
for($count = $begin; $count <= ($begin+$step); $count++)
echo "$count ";
echo "<br />";
if(($begin + $step) == $end)
return;
else
Triangle($begin, $end, ++$step);
}
Triangle(3, 8);
?>
output:
3
3 4
3 4 5
3 4 5 6
3 4 5 6 7
3 4 5 6 7 8
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 have a prepared query with the following result:
12
12
12
14
14
14
14
14
11
11
2
2
2
I simply want to return the number and its frequency:
12 3
14 5
11 2
2 3
My code is as follows:
$count =0;
while($DNB -> fetch())
{
// echo "<br/>";
$nomatch = 0;
if($count ==0)
{
array_push($array, array($postalcode ,1));
// echo $array[0][1];
}
else
{
for($i = 0; $i < count($array);$i++)
{
if($array[$i][0] != $postalcode)
{
$nomatch ++;
if($nomatch == count($array) -1)
{
array_push($array, array($postalcode ,1));
//echo $array[$i][1];
}
}
else
{
$array[$i][1]++;
}
}
}
$count = $count +1;
}
I am not sure what I am doing wrong.
You should use "SELECT id, COUNT (*) FROM table GROUP BY id" as the initial sql query.