38.8k views
4 votes
Write a program in JavaScript to display all the Odd number from 1 to 1.​

1 Answer

2 votes

Answer:

for (let i = 1; i <= 10; i++) {

if (i % 2 !== 0) {

console.log(i);

}

}

Step-by-step explanation:

This program uses a for loop to iterate over the numbers from 1 to 10. The if statement checks if the current number is odd by using the modulo operator % to check if the remainder of dividing by 2 is not equal to 0. If the number is odd, it is printed to the console using the console.log function.

You can adjust the program to display odd numbers up to a different limit by changing the upper limit of the for loop. For example, to display odd numbers up to 20, you could change the loop to:

for (let i = 1; i <= 20; i++) {

if (i % 2 !== 0) {

console.log(i);

}

}

User Patalmypal
by
8.2k points