Question

In HTML write a program that calculates and displays all factors of a number with a...

In HTML write a program that calculates and displays all factors of a number with a function. A factor is any number that divides into a number evenly.

For examples: Factors of 20 are 1,2,4,5,10,20

For the program:

  1. Prompt the user for a number or use an input
  2. Call a function to calculate all factors using loops and conditionals and modulus
  3. Display all factors to the page

Bonus +5

If the factor is Even : Print it in Green

If The Factor is Odd : Print it in Red

0 0
Add a comment Improve this question Transcribed image text
Answer #1

the HTML code with function is given below:-

<!DOCTYPE html>

<html lang="en">

  <head>

    <meta charset="UTF-8" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <title>Document</title>

  </head>

  <body>

    <p>Enter a number:</p>

    <input type="text" id="inp" />

    <input type="submit" onclick="factors()" value="Factors" />

    <!-- here I am taking a input and then on clicking the submit button it will run the factors function -->

    <p id="result"></p>

    <script>

      function factors() {

        // in factors function I have iterated till the number and

        document.getElementById("result").innerHTML = ""; // checked if any number is divible by it or not

        document.getElementById("result").innerHTML = "Factors are:"; // if divisible then printed it

        var num = document.getElementById("inp").value;

        var count = 0;  // this variable is to check wheather the factors are odd or even

        for (let index = 1; index <= num; index++) {

          if (num % index == 0) {

            count++;

            document.getElementById("result").innerHTML += index + " ";

          }

          if (count % 2 == 0) {  // of even then i am adding the style green to it

            document.getElementById("result").style.color = "green";

          } else {  // else style red to it

            document.getElementById("result").style.color = "red";

          }

        }

      }

    </script>

  </body>

</html>

FOR BETTER UNDERSTANDING PLEASE GO THROUGH THE SCREENSHOT:-

THE OUTPUT AND HTML WINDOW:-

Add a comment
Know the answer?
Add Answer to:
In HTML write a program that calculates and displays all factors of a number with a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ using For Loops; "Write a program that displays number facts for a number provided...

    In C++ using For Loops; "Write a program that displays number facts for a number provided by the end user between 1-100 in a table format that includes the number itself, whether the number is a prime number, whether the number is even or odd, whether the number is a perfect square and all of its factors. " It should also print this information.

  • 1. Write a program that takes a number as input and check whether the number is...

    1. Write a program that takes a number as input and check whether the number is positive, negative or zero. 2. Write a C++ program that prompts the user to enter a number and checks whether the entered number is even or odd. 3.  Using switch-case statement write a C++ program that prompts the user to enter 1, 2 or 3 and display "Red" if selection is 1, "Yellow" if selection is 2, or "Green" if selection is 3. 4. Write...

  • <!DOCTYPE html> <html> <body> <script> // // Write a function that calculates the amount of money...

    <!DOCTYPE html> <html> <body> <script> // // Write a function that calculates the amount of money a person would earn over // a period of years if his or her salary is one penny the first day, two pennies // the second day, and continues to double each day. The program should ask the // user for the number of years and call the function which will return the total // money earned in dollars and cents, not pennies. Assume...

  • Please solve it by Python 3 Write a program that asks the user for a positive...

    Please solve it by Python 3 Write a program that asks the user for a positive integer limit and prints a table to visualize all factors of each integer ranging from 1 to limit. A factor i of a number n is an integer which divides n evenly (i.e. without remainder). For example, 4 and 5 are factors of 20, but 6 is not. Each row represents an integer between 1 and 20. The first row represents the number 1,...

  • Write a program that reads from the keypad an integer and calculates the following: If the...

    Write a program that reads from the keypad an integer and calculates the following: If the number given by the user is even, then divide it by 2. If it is an odd number, multiply it by 3 and add 1.The program should repeat this procedure for the calculated new result (which will be printed on the screen) until it reaches number 1. The program will also need to print out how many steps it took overall.

  • programming language: C++ *Include Line Documenatations* Overview For this assignment, write a program that will simulate...

    programming language: C++ *Include Line Documenatations* Overview For this assignment, write a program that will simulate a game of Roulette. Roulette is a casino game of chance where a player may choose to place bets on either a single number, the colors red or black, or whether a number is even or odd. (Note: bets may also be placed on a range of numbers, but we will not cover that situation in this program.) A winning number and color is...

  • HTML only Design and implement a program that calculates the amount of money a person would...

    HTML only Design and implement a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should:ask the user for the number of daysdisplay a table showing what the salary was for each daythen show the total pay at the end of the period.The output should be displayed in the...

  • A factor of an integer N is an integer that divides N evenly. Note that 1...

    A factor of an integer N is an integer that divides N evenly. Note that 1 and N are always factors of N. For this task, You will need to write a MIPS program that accepts a positive integer from the standard input, counts and reports how many factors of this integer are even and how many are odd. See the sample run below for required format. Sample runs (user input in blue): Please enter a positive int: 20 User...

  • Write a C program to do the following 1) request user to enter 10 integer into...

    Write a C program to do the following 1) request user to enter 10 integer into an array 2) sort the array in ascending order 3) display the sorted array 4) count the number of odd and even number in the array and print out the result 5) add the value of the odd number and even number and calculate the average of the odd and even number. display the result 6) write function addNumber() to add all the number...

  • Write a function PoundsToKilograms that: Takes a number of pounds (an integer) as a parameter, Calculates...

    Write a function PoundsToKilograms that: Takes a number of pounds (an integer) as a parameter, Calculates and returns the corresponding value in kilograms. 1 kilogram is equal to 2.2 pounds. In the main section of the program: Prompt the user to input a weight in pounds, Call the function you wrote to transform the weight into kilograms, Output the weight in kilograms; the number must be shown with 2 decimals after the decimal point. Your program must define and call...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT