I am unsure why I am not getting an echo if it is a prime number
$numPrime = 2;
function Number($numPrime)
{
for($numPrime = 0; $numPrime < 100; $numPrime++)
{
if($numPrime == TRUE)
{
echo("TRUE");
}else{
echo("FALSE");
}
}
}
You want something like this
for($i = 2; $i < $num; $i++) {
if($num % $i == 0) {
echo "false";
}
}
echo "true";
Related
I have to resolve this exercice :
Display all the numbers in descending order between 0 and 100, then display hurray each prime number between 100 and zero within the same loop that displays the decreasing eveners.
I did this code :
for($n=100; $n>0; $n-=1 ){
$tableaunombrepaires[]= $n;
//loop for dividers
for($j=2; $j<=$n/2; $j++) {
$diviseurs[]= $j;
}
// here we devide
foreach($diviseurs as $diviseur){
$restes[]=$n%$diviseur;
}
if(!(in_array(0,$restes))){
echo "$n hourrah! \n";
}
else{
echo "$n \n";
}
}
but it doesn't work
help please
function isPrime ($n) {
for ($x = 2; $x < $n; $x++) {
if($n % $x == 0) {
return false;
}
}
return true;
}
for($n = 100; $n > 0; $n--) {
if (isPrime($n)) {
echo $n . " hourrah! \n";
} else {
echo $n . "\n";
}
}
This question already has answers here:
How to get whole and decimal part of a number?
(20 answers)
Closed 1 year ago.
I want to get the breakdown of the charges but im stock on the decimal point, it should be the output i fill.out below. please see and check my code if there is lacking. it really could help me much. Ill show the output of my code.
$charge = 1600.50;
$base = 750;
$difference = $charge - $base;
$installment = $charge / 750;
$remainder = ($charge % 750);
$counter = 1;
if (is_float($charge)) {
if($remainder == 0) {
$count = 0;
for ($i=1; $i <= round($installment); $i++) {
$count = $count + 1;
echo $base."\n";
}
if(is_float($charge)){
$exploadedAmount = explode('.', $charge);
echo "0.".$exploadedAmount[1];
}
} else {
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}else{
echo $remainder."\n";
}
$counter = $counter + 1;
}
if(is_float($charge)){
$exploadedAmount = explode('.', $charge);
echo "0.".$exploadedAmount[1];
}
}
} else {
if($remainder == 0){
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
echo $base."\n";
}
} else {
if($difference < 0){
echo $remainder."\n";
} else {
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}else{
echo $remainder."\n";
}
$counter = $counter + 1;
}
}
}
}
This is the output of my code.
750
750
100
0.50
But the correct output would be this.
750
750
100.50
I hope there is anyone could help me to solve this problem. it took weeks but im not able to solve this.
I don't know if i faced your problem correctly, but according to your desired output the following will do the trick.
else{
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}else{
// echo $remainder."\n";
echo $remainder + $charge-floor($charge); // get fractial part of your $charge and add it to your remainder
}
$counter = $counter + 1;
}
/* unnecessary if-statement, is already checked by surrounding if-statement
if(is_float($charge)){
*/
// echo 'TRUE';
// $exploadedAmount = explode('.', $charge);
// echo "0.".$exploadedAmount[1];
}
}
I really don't understand what are you trying to do but isn't it better to write the code in partly functions?
anyways by editing this part of your code I manage to output the result you want:
$charge = 1600.50;
$base = 750;
$difference = $charge - $base;
$installment = $charge / 750;
$remainder = ($charge % 750);
$counter = 1;
if (is_float($charge)) {
// echo "TRUE";
if($remainder == 0){
$count = 0;
for ($i=1; $i <= round($installment); $i++) {
$count = $count + 1;
echo $base."\n";
}
if(is_float($charge)){
$exploadedAmount = explode('.', $charge);
echo "0.".$exploadedAmount[1];
}
}
else{
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}
$counter = $counter + 1;
}
if(is_float($charge)){
// if you want to concatinate the number to previuse one you must do it here
// the extra 0.5 is echo here
$exploadedAmount = explode('.', $charge);
echo "$remainder.".$exploadedAmount[1];
}
}
}
else{
if($remainder == 0){
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
echo $base."\n";
}
}
else{
if($difference < 0){
echo $remainder."\n";
}else{
$count = 0;
for ($i=1; $i <= ceil($installment); $i++) {
$count = $count + 1;
if($counter != ceil($installment)){
echo $base."\n";
}else{
echo $remainder."\n";
}
$counter = $counter + 1;
}
}
}
}
The output will be :
750 750 100.5
echo ".".$exploadedAmount[1]."0" ;
At line number 63 , if u change that number and 0 adding at the beginning and end means your can get that accurate output and also it is applicable for other phone numbers also.
I'm supposed to use nested for loops to create this shape: https://imgur.com/a/prh6zwj
This is what I currently have:
<?php
for ($x = 1; $x <= 10; $x++){
for ($y = 1; $y <= 6; $y++){
echo "Y";
}
}
?>
I have no clue what to do.
Thanks in advance!
<?php
$position = 1;
for ($x = 1; $x <= 11; $x++){
for ($y = 1; $y <= 6; $y++){
if ($y == $position) {
echo "Y";
} else {
echo "0";
}
}
if ($x < 6) {
$position++;
} else {
$position--;
}
echo "\n";
}
<?php
$length = 6; // change this to change height width
$pos = 0;
for ($x = 1; $x <= (($length*2)-1); $x++){
if($x <= $length)
{$pos = $pos+1; }
else
{$pos = $pos-1; }
for ($y = 1; $y <= $length; $y++){
if($y == $pos)
echo "Y";
else
echo "O";
}
echo "\n";
}
There are many possible ways to achieve this when I started programming I never cared about the code quality and just focused on the output. I have added two examples to help you understand it better!
<?php
//We have 6 columns & 11 rows so we need two loops one size of 11 and second size of 6
$counter = 1;
for ($i = 1; $i <= 11; $i++){
for ($j = 1; $j <= 6; $j++){
if ($j == $counter) {
echo "Y";
} else {
echo "O";
}
}
if ($i < 6) {
$counter++;
} else {
$counter--;
}
echo "<br/>";
}
echo "**************************** METHOD TWO ****************************";
//Following is not efficient But its also printing the same results
for ($i = 0 ; $i < 66 ; $i++){
if($i == 65)
{
echo "O";
break;
}
if($i % 6 == 0){
echo "<br/>";
}
if($i <= 36)
{
if ($i % 7 == 0){
echo "Y";
}else{
echo "O";
}
}else{
if ($i % 5 == 0){
echo "Y";
}else{
echo "O";
}
}
}
?>
$k=2; // for calculating position from backside
for($i=1;$i<=11;$i++) //for row
{
for($j=1;$j<=6;$j++) //column
{
if($j==$i && $i<=6) //logic for printing "Y" till the end of row
echo "Y";
else if($i>6 && $j==($i-$k)) //logic for printing "Y" in reversal order from the end of row
{
echo "Y";
$k+=2;
}
else
echo "O"; // filling rest places with "O"
}
echo"\n"; // jumping to new Row;
}
Hope you can understand it easily.
I flip a coin until I get three heads in a row!
<?php
$headcount = 0;
$flipcount = 0;
while ($headcount < 3) {
$flip = rand(0, 1);
$flipcount++;
if($flip) {
$headcount++;
echo "H<br>";
}
elseif ($headcount > 3) {
$count_head = $headcount;
for($i = 0; $i < $headcount; $i++) {
$count_head = $i;
}}
else {
$headcount = 0;
echo "T<br>";
}
}
echo "<p>It took {$flipcount} flips!</p>";
echo "<p>It took {$count_head} flips!</p>";
?>
Use two variables that you increment when the roll is heads. One is the count of sequential heads, which gets set back to 0 when you roll tails, the other is the count of all heads, which doesn't get reset.
<?php
$headcount = 0;
$allheads = 0;
$flipcount = 0;
while ($headcount < 3) {
$flip = rand(0, 1);
$flipcount++;
if($flip) {
$headcount++;
$allheads++;
echo "H<br>";
}
else {
$headcount = 0;
echo "T<br>";
}
}
echo "<p>It took {$flipcount} flips!</p>";
echo "<p>There were {$allheads} heads!</p>";
?>
DEMO
I have the following code, to output all prime numbers from array. I would like to get the sum of the output in ex: 2+3+5 = 10, Any hint how to get that ?
$n = array(1,2,3,4,5,6);
function prime($n){
for($i=0;$i<= count($n);$i++){
$counter = 0;
for($j=1;$j<=$i;$j++){
if($i % $j==0){
$counter++;
}
}
if($counter == 2){
print $i."<br/>";
}
}
}
print prime($n);
Then this should work for you:
(Here i used $sum which i initialized before the foreach loop and then used the += operator to add the sum together)
<?php
$n = array(1,2,3,4,5,6);
function prime($n){
$sum = 0;
foreach($n as $k => $v) {
$counter = 0;
for($j = 1; $j <= $v; $j++) {
if($v % $j == 0)
$counter++;
}
if($counter == 2) {
echo $v."<br/>";
$sum += $v;
}
}
echo "Sum: " . $sum;
}
prime($n);
?>
Output:
2
3
5
Sum: 10
As #IMSoP commented above, one option is to compile the list of primes into a new array:
$m = [];
// looping code...
// If prime:
array_push( $m, $primeNumber );
Then, when you're done, you can do your printing mechanism:
print implode( "<br />", $m );
And then you can do your summing mechanism:
print "<p>Sum: " . array_sum( $m ) . "</p>";
The added benefit here is you can split out each piece of functionality into it's own function or method (which you should do to have a good design).
try this
<?php
define('N', 200);
function isPrime($num)
{
if ($num == 2 || $num == 3) { return 1; }
if (!($num%2) || $num<1) { return 0; }
for ($n = 3; $n <= $num/2; $n += 2) {
if (!($num%$n)) {
return 0;
}
}
return 1;
}
for ($i = 2; $i <= N; $i++) {
if (isPrime($i)) {
$sum += $i;
}
}
echo $sum;
You can try something like this:
function isPrime($n){
if($n == 1) return false;
if($n == 2) return true;
for($x = 2; $x <= sqrt($n); $x++){
if($n % $x == 0) return false;
}
return true;
}
$sum = 0;
$n = array(1,2,3,4,5,6);
foreach($n as $val){
if(isPrime($val)) {
echo $val . "<br />";
$sum += $val;
}
}
echo "Sum: " . $sum;
<?php
$num = 100;
for($j=2;$j<$num;$j++)
{
for($k=2;$k<$j;$k++)
{
if($j%$k==0)
{
break;
}
}
if($k==$j)
{
$prime_no[]=$j;
}
}
echo "<pre>";
print_r($prime_no);
echo "</pre>";
for($j=0;$j<count($prime_no);$j++)
{
$myprimeAdd = $prime_no[$j] + $prime_no[$j+1];
if(in_array($myprimeAdd,$prime_no))
{
echo "Resultant Prime No:-", $myprimeAdd;
echo nl2br("\n");
break;
}
}
?>