Write a JavaScript for loop that will iterate from 0 to 15. For each iteration, it will check if the current number is odd or even, and display a message to the screen. Show Sample Output: Calculate a Even Number: Even Numbers between 1 to 100: Calculate a Odd Number: Odd Numbers between 1 to 100: Sample Solution:- HTML Code:
JavaScript Code:
Sample Output: 0 is even 1 is odd 2 is even 3 is odd 4 is even 5 is odd 6 is even 7 is odd 8 is even 9 is odd 10 is even 11 is odd 12 is even 13 is odd 14 is even 15 is odd Flowchart: Live Demo: See the Pen javascript-conditional-statements-and-loops-exercise-5 by w3resource (@w3resource) on CodePen. In this section, we will create a Java program to display even numbers from 1 to 100. To learn the Java even number program, you must have the basic knowledge of Java for loop and if statement. We can use different ways to display even numbers:
Using Java for LoopIn the following example, we have declared a variable named number and initialized it with 100 (the limit to print the even number). We have used a for loop that executes up to 100 times and for each iteration of i the if statement checks the number is even or not. After printing each even number, the value if i is increased by 1. In order to check the number, we have divided the number by 2 if it does not leave any remainder, the number is even and the print statement prints that number. DisplayEvenNumbersExample1.java Output: List of even numbers from 1 to 100: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 Using nested-if StatementThe following program is slightly different from the above program because we have defined a method that contains the logic to check even number. Inside the method, we have used nested-if statement. I want to print all even numbers between 10 and 40. But what’s wrong with my code? code:
When I run in on console it’s just print 10 First, to find an Even number it is very simple, divide the number by 2 if the remainder is zero then it’s an Even number. Example if you give the start and end range from 10 to 20, the program has to print 10, 12, 14, 16, 18, 20. So let’s write a simple snippet now. JavaScript1 2 3 4 5 6 7 8 for(i=10; i<=20; i++){ // let's divide the value by 2 // if the remainder is zero then it's an even number
if(i % 2 == 0){ console.log(i); } } The above script should print the values in the console as expected. Let’s write a dynamic script with HTML to get the start and end range from the user and print the output on the browser. How to show even number in JavaScript?Example 1: Using if...else
In the above program, number % 2 == 0 checks whether the number is even. If the remainder is 0, the number is even. In this case, 27 % 2 equals to 1. Hence, the number is odd.
How to print all even numbers in JavaScript?const isOdd = (n) => (n & 1) === 1; const num = [1,2,3,4,5,6,7,8,9]; console. log( `Odd numbers are ${ num. filter( n => isOdd(n))}` ); console. log( `Even numbers are ${ num.. const.. Arrow functions.. Bitwise AND (&). Logical NOT (!). Array. prototype. filter(). How to sum 1 to 100 in JavaScript?Example 1: Sum of Natural Numbers Using for Loop. The value of sum is 0 initially.. Then, a for loop is used to iterate from i = 1 to 100 .. In each iteration, i is added to sum and the value of i is increased by 1.. When i becomes 101, the test condition is false and sum will be equal to 0 + 1 + 2 + ... + 100 .. How do you print even numbers from 1 to 100?Algorithm. Start.. Start a loop for i = 1 to 100. Check if i is divisible by 2. If true, print the number. If false, increment the number.. |