![]() |
We have to find all even numbers within a given range. To solve this question we are given the range(start, end) in which we have to find the answer. There are several ways to print all the even numbers in a range in an array using JavaScript which are as follows: Table of Content Using for Loop in JavaScriptIn this method, a “for” loop is used to iterate through a given range of numbers. It identifies even numbers within this range by checking if the current number’s remainder, when divided by 2, is zero. If so, it prints the even number. Example: To print all the even number within a specific range in JavaScript using for loop. Javascript
Output
4 6 8 10 12 14 Time Complexity: O(n) Space Complexity: O(1) Using While Loop in JavaScriptIn this method, we uses a “while” loop to iteratively identify and print even numbers within a specified range. The loop continues until the end of the range is reached. Example: To find all the even number in a range in JavaScript using while loop. Javascript
Output
4 6 8 10 12 14 Time Complexity: O(n) Space Complexity: O(1) Using forEach Loop in JavaScriptIn this method, it combines a “for” loop with a “forEach” loop to identify even numbers in a given range. It populates an array with even numbers and then prints each element using “forEach”. Example: To print all the even numbers in a range in JavaScript using forEach loop. Javascript
Output
4 6 8 10 12 14 Time Complexity: O(n) Space Complexity: O(n) |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |