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 N odd 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

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

Construct a Pseudocode to take three numbers and print largest and smallest among them.

What will be the output of the following code snippet?var a = 1; var b = 0;  while (a <= 3) {  a++;  b += a * 2;  print(b); }*4 10 181 2 3None of Above1 4 7

create a Python script with a recursive function to display the sum of the first n integers, where n is entered by the user.

write a program to print integer no, from 1 to n where n is the input from the user using do while loop in c language use getch

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.