How to identify duplicate numbers - php

So i have this problem where i generate random numbers from 1 - 10 then displaying the numbers then identifying what is repeating. BTW intNcases is the number of numbers and should not exceed 20. This is actually our assignment and i'm really having a hard time with it please help. This is my code so far.
Sample Output
Random numbers of case is: 7
Random numbers are: 4, 2, 1, 1, 4,3,2
Numbers: 4, 2, 1, 3
Repeating numbers are: 4, 2, 1
<html>
<body>
<?php
$intNcases = 5;
$hold = array(0,0,0);
$temp = array(0,0,0);
$rep = array(0,0,0);
$num = array(0,0,0);
$count = 1;
if($intNcases>20)
{
echo 'Error N cases is greater than 20';
}
else
{
echo 'The number of case/s is: '. $intNcases;
echo '<br><br>'. 'Input'.'<br>';
for($x=0;$x<$intNcases;$x++)
{
$N = rand(1,10);
echo $N. '<br>';
$hold[$x] = $N;
$temp[$x] = $N;
}
echo 'OUTPUT<br>';
for($d=0;$d<$intNcases;$d++)
{
for($j=1;$j<$intNcases;$j++)
{
if($hold[$d] == $temp[$j])
{
$rep[$j-1] = $hold[$j-1];
$hold[$j-1] = 0;
}
else
{
$num[$j-1] = $hold[$j-1];
}
}
echo '#'.$count.' - '.$num[$d]. '<br>';
$count++;
}
echo 'Repeating numbers are: ';
for($k=0;$k<sizeof($rep);$k++)
{
echo $rep[$k]. ' ';
}
}
?>
</body>
</html>

Maybe you can do this more easier with array_unique or array_shuffle or other array functions

You could try this.
$intcases = rand(1,20);
$numbers = array();
echo 'Random number of cases: '. $intcases . '</br>';
echo 'Random numbers are: ';
for($x=0;$x<$intcases;$x++)
{
$number = rand(1,10);
echo $number. ' ';
if(array_key_exists($number, $numbers))
{
$numbers[$number] = $numbers[$number] + 1;
}else
{
$numbers[$number] = 1;
}
}
echo '</br>';
echo 'Numbers are: ';
foreach($numbers as $number => $x)
{
echo $number . ' ';
}
echo '</br>';
echo 'Repeating numbers: ';
foreach($numbers as $key => $value)
{
if($value > 1)
{
echo $key . ' ';
}
}
echo '</br>';

Related

loop with symbol minus and number php

i have this code .
echo "<br>";
$start = 1;
$angka = $_POST[angka];
$a = $angka;
for($i=$start; $i<=$angka; $i++) {
for($j=$start;$j<=$angka;$j=$j+2){
echo $i;
if($j < $angka) echo $a;
}
$a--;
echo '<br>';
}
Reference: Print Looping for dynamic row php
This is not my expecting result .
First i want the result like this .
-2-4-
1-3-5
-2-4-
1-3-5
-2-4-
The rule is Number of rows and columns follow the number of numbers declared.If the declaration of the number 5, then the results will be displayed are 5 rows and 5 columns like the example above.
i think this code is working
<?php
$_POST['angka'] = 5;
$angka = $_POST['angka'];
for($i=1; $i<=$angka; $i++) {
for($j=1;$j<=$angka;$j++){
if($i%2 == 1) {
if($j%2 == 0) {
echo $j;
} else {
echo '-';
}
} else {
if($j%2 == 0) {
echo '-';
} else {
echo $j;
}
}
}
echo '<br>';
}
Result:
-2-4-
1-3-5
-2-4-
1-3-5
-2-4-

how to determine the sequence number of odd /even numbers using php

how to determine the sequence number of odd /even numbers using php ?
example ,i want output like here
odd numbers (1,3,5,7,9)
output
1 = 1
3 = 2
5 = 3
7 = 4
9 = 5
even numbers (2,4,6,8,10)
output
2=1
4=2
6=3
8=4
10=5
how code to make this function in php?
edit/update
if input 1 then output = 1 , if input 3 then output = 2, if input 21 then output= 11, etc,,
Try out this
php code
<?php
$result = '';
if(isset($_POST['value'])){
//assign POST variable to $num
$num = $_POST['value'];
$count = 0;
//for even numbers
if($num % 2 == 0){
$count = $num/2;
$result = "The Number ".$num." is Even on ".$count;
}else {
//if you know about sequences and series, you can understand it :P
$count = (($num-1) / 2)+1;
$result = "The Number ".$num." is Odd on ".$count;
}
}
?>
HTML COde
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<input type="text" name="value"/>
<input type="submit" value="Check"/><br/>
<?php echo $result;?>
</form>
It is working correctly :P
<?php
$evenLimit = 10;
$oddLimit = 9;
$count = 1;
for ($x = 1; $x <= $oddLimit; $x = $x + 2) {
echo $x . "=" . $count . "<br>";
$count++;
}
echo "<br>";
$count = 1;
for ($x = 2; $x <= $evenLimit; $x = $x + 2) {
echo $x . "=" . $count . "<br>";
$count++;
}
?>

Checking if a number is a Fibonacci number through a loop

so the task I'm trying to complete here is comparing a user input number against the fibonacci sequence and if it is a fibonacci number the program will echo true, if not it will echo false.
Can someone tell me where I'm going wrong?
this is my first file:
<?php
function print_fibonacci($n){
$first = 0;
$second = 1;
echo "Fibonacci Series: \n";
echo $first.' '.$second.' ';
for($i=2;$i<$n;$i++){
$third = $first + $second;
echo $third.' ';
$first = $second;
$second = $third;
}
}
/* Function call to print Fibonacci series upto 6 numbers. */
print_fibonacci(16);
?>
<form method="POST" action="fibonacci3.php"><br>
<label>Input a number to check if it is fibonacci or not.</label><br>
<input name="fib" type="text" placeholder="#" /><br>
<input type="submit" value="OK!" />
</form>
This outputs the fibonacci sequence until the 16th fibonacci number, and is followed by a form which allows a user to enter a number.
The next file is fibonacci3.php as referenced in the form.
<?php
include "fibonacci2.php";
$n = $_POST["fib"];
function fibonacci($n) {
//0, 1, 1, 2, 3, 5, 8, 13, 21
/*this is an error condition
returning -1 is arbitrary - we could
return anything we want for this
error condition:
*/
if((int)$n <0){
return -1;
echo "False";
}
if ((int)$n == 0){
return 0;
echo "0";
}
if((int)$n == 1 || $n == 2){
return 1;
}
$int1 = 1;
$int2 = 1;
$fib = 0;
for($i=1; $i<=$n-2; $i++ )
{
$fib = $int1 + $int2;
//swap the values out:
$int2 = $int1;
$int1 = $fib;
}
if ($fib = $int1 + $int2 && $n == $fib){
echo "True!";
} else {
echo "False!";
}
return $fib;
}
fibonacci((int)$n);
?>
I thought this might be correct but it's not outputting anything when the user inputs a number.
$n = 'Your number';
$dRes1 = sqrt((5*pow($n, 2))-4);
$nRes1 = (int)$dRes1;
$dDecPoint1 = $dRes1 - $nRes1;
$dRes2 = sqrt((5*pow($n, 2))+4);
$nRes2 = (int)$dRes2;
$dDecPoint2 = $dRes2 - $nRes2;
if( !$dDecPoint1 || !$dDecPoint2 )
{
echo 'True';
}
else {
echo 'False';
}

How to get total value of odd, even numbers and echo invalid inputs

Here is the code that I have so far for one of my assignments.
<!DOCTYPE html>
<html>
<head><title>Numbers</title></head>
<body>
<form action="index.php" method="get">
<b>Numbers</b>
<br>
<textarea rows="12" cols="25" name="result" value="result"></textarea>
<br>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
<?php
$result=$_GET["result"];
if (empty($_GET['result']))
{
echo '<p><font size="3" color="red">Field is Empty*</font></p>';
}
elseif (isset($_GET['result']))
{
$result=(explode("\n", $result));
}
echo "<br />";
echo "Total lines passed: " . count ($result);
echo "<br />";
echo "Total value of numbers: " .array_sum($result);
?>
I need a code to echo out the total values of the even and odd number inputs that are submitted into the textbox. Ex. 1, 2, 3, 4, 5 are submitted and the total value for even comes out as 6 because of 2 and 4 being even numbers and the total value for odd comes out as 9 because of 1, 3, and 5 being odd. If the user also inputs an invalid integer, let's say a, b, c. I will be able to echo out a list of all the invalid values submitted.
1
2
3
4
5
a
b
c
Total of even numbers: 6
Total of odd numbers: 9
Invalid Numbers: a b c
$result = array_map('trim', $result); // to trim the whitespace in your input
$even = $odd = $invalid = array();
foreach ($result as $num) {
if (is_numeric($num)) {
if ($num % 2 == 0) {
$even[] = $num;
} else {
$odd[] = $num;
}
} else {
$invalid[] = $num;
}
}
echo "Total of even numbers: " . array_sum($even) . "<br />";
echo "Total of odd numbers: " . array_sum($odd) . "<br />";
echo "Invalid numbers: " . implode(' ', $invalid) . "<br />";
Demo!
$total = array("even" => 0, "odd" => 0, "invalid" => 0);
foreach ($result as $r){
if(intval($r) == "" && $r != 0){
$total['invalid']++;
}else{
if ($r % 2 == 0){
$total['even'] += $r;
}else{
$total['odd'] += $r;
}
}
}
print_r($total);
Try this:
$evenSum=0;$oddSum=0;
$alphaArr=array();
foreach($result as $key){
if(!is_nan($key)){
if($key%2==0){
$evenSum+=$key;
}
else{
$oddSum+=$key;
}
}else{
$alphaArr[]=$key;
Try that:
$even=array();
$odd=array();
$invalid=array();
for($i=0;$i<count($result);$i++)
{
if(!is_numeric($result[$i])) { $invalid[]=$result[$i]; continue; }
if($result[$i] % 2 == 0) $even[]=$result[$i];
else $odd[]=$result[$i];
}
echo "Sum of even numbers: " . array_sum($even);
echo "Number of even numbers: " . count($even);
echo "Sum of odd numbers: " . array_sum($odd);
echo "Invalid numbers: " . implode(",",$invalid);

If value is greater/lesser than xyz

I have a value as a number. For instance, 502.
I want to write a php if statement that will display some text if the value is lesser or greater than certain numbers, or between a range.
E.g.
number is 502, text will say: "Between 500-600"
number is 56, text will say: "Between 0-60"
etc.
So far I have this:
<?php $count=0;?>
<?php $board = getUserBoard($userDetails['userId']);?>
<?php if(is_array($board)):?>
<?php $boardCount = count($board);?>
<?php foreach($board as $key=>$value):?>
<?php
$boardPin = getEachBoardPins($value->id);
$count = $count + count($boardPin);
?>
<?php endforeach?>
<?php endif?>
And that gives me a number:
<?php echo $count;?>
I have tried writing...
<?php if(($count)): => 500 ?>
Over 500
<?php endif ?>
But I keep running into errors.
I'd like to create a list if possible with elseif statements denoting various number ranges.
E.g.
0-50, 51-250, 251-500 etc.
Can anyone help me?
Thanks.
The sanest, neatest and most widely used syntax for if conditions in PHP is:
if($value >=500 && $value <=600 )
{
echo "value is between 500 and 600";
}
if ($count >= 0 && $count < 100) {
echo 'between 0 et 99';
} elseif ($count < 199) {
echo 'between 100 and 199';
} elseif { ...
}elseif ($count < 599) {
echo 'between 500 and 599';
} else {
echo 'greater or equal than 600';
}
I wrote something like this a few years back (might be a better way to do it):
function create_range($p_num, $p_group = 1000) {
$i = 0;
while($p_num >= $i) {
$i += $p_group;
}
$i -= $p_group;
return $i . '-' . ($i + $p_group - 1);
}
print 'The number is between ' . create_range(502, 100) . '.';
It'll say 500-599, but you can adjust it to your needs.
I'm not sure what you need, but here is what I understand you ask:
function getRange($n, $limit = array(50, 250, 500)) { // Will create the ranges 0-50, 51-250, 251-500 and 500-infinity
$previousLimit = 0;
foreach ($limits as $limit) {
if ($n < $limit) {
return 'Between ' . ($previousLimit + 1) . ' and ' . $limit; //Return whatever you need.
}
$previousLimit = $limit;
}
return 'Greater than ' . $previousLimit; // Return whatever you need.
}
echo getRange(56); // Prints "Between 51 and 250"
echo getRange(501); // Prints "Greater than 500"
echo getRange(12, array(5, 10, 15, 20)); // Prints "Between 11 and 15"
function getRange($number){
$length=strlen($number);
$length--;
$r1=round($number,-$length);
if ($r1>$number){
$r2=$r1-pow(10,$length);
return ''.$number.' value is between '.$r2.'-'.$r1;
}
else {
$r2=$r1+pow(10,$length);
return ''.$number.' value is between '.$r1.'-'.$r2;
}
}
Try this.

Categories