print the numbers from 1 to 100, but only print the even numbers. use a while loop in typescript

let num: number = 1;
while (num <= 100) {
  if (num % 2 === 0) { // Check if the number is even
    console.log(num); // Print the even number
  num++; // Increment the number
182 chars
8 lines

In this code snippet, we initialize num to 1 and use a while loop to print even numbers from 1 to 100. The loop continues as long as num is less than or equal to 100.

Inside the loop, we check if the number is even by using the modulo operator (%). If the remainder of num / 2 is 0, it means that the number is even. We print the even number using console.log().

Finally, we increment num to move on to the next number.

gistlibby LogSnag