Knowee
Questions
Features
Study Tools

Create an HTML and JavaScript program which accepts N as input and print first Nodd numbers

Question

Create an HTML and JavaScript program which accepts N as input and print first Nodd numbers

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple HTML and JavaScript program that accepts a number N as input and prints the first N odd numbers.

HTML:

<!DOCTYPE html>
<html>
<head>
    <title>Print N Odd Numbers</title>
</head>
<body>
    <input type="number" id="num" placeholder="Enter a number">
    <button onclick="printOddNumbers()">Print Odd Numbers</button>
    <p id="output"></p>
</body>
</html>

JavaScript:

function printOddNumbers() {
    var n = document.getElementById('num').value;
    var output = '';
    for (var i = 1, count = 0; count < n; i += 2, count++) {
        output += i + ' ';
    }
    document.getElementById('output').innerHTML = output;
}

In this program, when you click the "Print Odd Numbers" button, the printOddNumbers function is called. This function gets the value of the input field, which is the number N. It then uses a for loop to generate the first N odd numbers. The loop starts at 1 (the first odd number), and increments by 2 each time (to get the next odd number). The loop continues until it has generated N odd numbers. The odd numbers are added to the output string, which is then displayed in the paragraph with id "output".

This problem has been solved

Similar Questions

Write an HTML and JavaScript program which accepts N as input and print N is oddor even number

Write a program that takes a number 𝑁N as the input, and prints it to the output.Input FormatThe only line of input contains a single integer.Output FormatOutput the answer in a single line.Constraints0≤𝑁≤1050≤N≤10 5 Sample 1:InputOutput123123

You are given a simple HTML page with a form and a submit button. The form takes comma-separated numbers as input from the user.The user can enter a list of comma-separated numbers in the input field with id #numbers.When the user presses the "Submit" button with id #submitBtn, your task is to create a JavaScript function using Map to square each number from the input and display the result in the paragraph tag with id #result.If the input is "34,67,78,65" then the output is "The squares are: 1156, 4489, 6084, 4225"

Write code that allow the user to input a number. Use for loop to print the number and 4 numbers after this number the user. For example, if the input is 5, then the program should print:56789

Write code that allow the user to input a number. Use for loop to print the number and 4 numbers after this number the user. For example, if the input is 5, then the program should print:

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.