Write an HTML and JavaScript program which accepts N as input and print first Nodd numbers
Question
Write an HTML and JavaScript program which accepts N as input and print first Nodd numbers
Solution
Sure, here is a simple HTML and JavaScript program that accepts N as input and prints the first N odd numbers.
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Print N Odd Numbers</title>
</head>
<body>
<h2>Enter a number:</h2>
<input type="number" id="num" placeholder="Enter a number">
<button onclick="printOddNumbers()">Submit</button>
<p id="output"></p>
</body>
</html>
JavaScript:
function printOddNumbers() {
var num = document.getElementById('num').value;
var output = '';
for (var i = 1, count = 0; count < num; i += 2, count++) {
output += i + ' ';
}
document.getElementById('output').innerHTML = output;
}
Here's how it works:
-
The HTML part of the code creates a simple webpage with an input field for the user to enter a number, a button to submit the number, and a paragraph to display the output.
-
The JavaScript part of the code defines a function
printOddNumbersthat is called when the button is clicked. -
Inside the function, we first get the value of the input field using
document.getElementById('num').value. -
We then initialize an empty string
outputto store the odd numbers, and a for loop to generate the odd numbers. The loop starts withi = 1(the first odd number), and incrementsiby 2 in each iteration to get the next odd number. The loop continues until we have generatednumodd numbers (count < num). -
In each iteration of the loop, we append the current odd number to
outputfollowed by a space. -
Finally, we display the odd numbers in the paragraph using
document.getElementById('output').innerHTML = output.
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"
Select the correct answerPredict the output and give your views for the given code snippet in C#include <stdio.h>void Function(int n, int j) { if (j >= n) return; if (n-j > 0 && n-j >= j) printf("%d %d ", j, n-j); Function(n, j+1);} int main() { int n = 10; Function(n, 3);}Options3 7 5 6 5 53 7 4 6 5 53 7 4 6 6 53 7 4 6 5 6
What sequence of numbers would be printed if the following function were executed with the value of N being 2?
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.