top of page

Task 3: Using the modulus operator, %, to test if a given number is odd

  • Writer: satyanarayan behera
    satyanarayan behera
  • Aug 25, 2022
  • 1 min read

You need to code a small program that takes a number and determines if it's an even number (like 2, 4, 6, 8, 10).

To achieve this task, you need to declare six variables, as follows:

  1. The first variable, named num1, should be assigned a number value of 2.

  2. The second variable, named num2, should be assigned a number value of 5.

  3. The third variable, named test1, should be assigned the calculation of num1 % 2. Note: executing this code will return a number.

  4. The fourth variable, named test2, should be assigned the calculation of num2 % 2. Note: executing this code will also return a number.

  5. The fifth variable, named result1, should be assigned the result of comparing if the number stored in the test1 variable is not equal to 0, in other words, this: test1 == 0.

  6. The sixth variable, named result2, should be assigned the result of comparing if the number stored in the test2 variable is not equal to 0, in other words, test2 == 0.

Run console log two times after you've set the variables:

  1. The first console log should have the following code between parentheses: "Is", num1, "an even number?", result1

  2. The second console log should have the following code between parentheses: "Is", num2, "an even number?", result2

Note: The output to the console should be as follows:

Is 2 an even number? true

Is 5 an even number? false

var num1=2;

var num2 =5;

var test1 =num1%2;

var test2=num2 % 2;

var result1 = (test1 == 0);

var result2= (test2== 0);

console.log("Is", num1, "an even number?", result1);

console.log("Is", num2, "an even number?", result2);

Is 2 an even number? true

Is 5 an even number? False


Recent Posts

See All
For Loop : Task 1

Write a "for" loop that will perform exactly the same repetitive code as this: console.log(1) console.log(2) console.log(3)...

 
 
 
Task 2: Using the logical || operator

Imagine you are coding a video game. Currently, you’re about to code some snippets related to the game over condition. You need to code a...

 
 
 

Comments


  • alt.text.label.LinkedIn

©2022 by Satya

bottom of page