4
06/11/2024 5:29 pm
Topic starter
Write a JS function that reads an integer n and prints all odd numbers from 1 to n.
The input comes as a single number n. The number n will be an integer in the range [1 … 100 000].
Examples:
Input:
5
Output:
1
3
5
Input:
4
Output:
1
3
Input:
7
Output:
1
3
5
7
The output should hold the expected odd numbers, each at a separate line.
1 Answer
2
06/11/2024 5:30 pm
Here is the javascript solution to this task:
function oddNum(number) { for (let i = 1; i <= number; i++) { if (i % 2 != 0) { console.log(i); } } } oddNum(7); //2nd solution: function oddNumberCheck(input) { for (let i = 1; i <= input; i += 2) { console.log(i); } } oddNumberCheck(7);