Home > front end >  How to code this one using PHP Looping Structure?
How to code this one using PHP Looping Structure?

Time:10-18

How can i code the corresponding divisible values from a given number from the user that only have one input-box to be used?

For example the user input 20 in the input-box

Output should display like this

The divisible value of 20 are: 1 2 4 5 10 20

CodePudding user response:

This is a simple problem. You should try first for such types of issues.

Here is an example answer, upgrade it more like increasing $i and dynamic your input value and others.

$divisible_val = '';
for($i = 1; $i <= 20; $i  ){
    if((20 % $i) == 0){
        $divisible_val .= $i.' ';
    }
}

echo 'The divisible value of 20 are: '.$divisible_val;

CodePudding user response:

Take HTML like:

<input type="text" onkeyup="divisible(this.value)">
<br>
<div id="showDivisibel"></div>

and add javascript function like:

function divisible(val)
    {
        var divisible_val = '';
       for(i=1;i<=val;i  )
       {
        if((val % i) == 0){
          divisible_val  = i  ' ';
        } 
        
       }   
      $("#showDivisibel").text("The divisible value of " val " are:"  divisible_val);
    }
  • Related