Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Monday, February 20, 2023

Find JavaScript odd or even

 Find JavaScript odd or even


Find JavaScript odd or even


 

Write a program to find JavaScript odd or even by using if..else.

To determine whether a number in JavaScript odd or even, you can use the modulus operator (%).

The modulus operator returns the remainder when one number is divided by another. If a number is divisible by 2 (i.e., it's an even number), the remainder will be 0. If it's not divisible by 2 (i.e., it's an odd number), the remainder will be 1.

 



 Explanation:




i) We use the prompt() function to ask the user to input a number. The prompt() function displays a dialog box with an input field for the user to enter a value.

ii) We store the input number in a variable called number.

iii) We use the modulo operator (%) to check if the number is odd or even. The modulo operator returns the remainder of a division operation. When we divide an even number by 2, the remainder is always 0. When we divide an odd number by 2, the remainder is always 1. Therefore, if the remainder of number % 2 is 0, the number is even. Otherwise, the number is odd.

iv) We use an if-else statement to check whether the number is odd or even. If the remainder of number % 2 is 0, we display a message in an alert box saying that the number is even. If the remainder of number % 2 is 1, we display a message in an alert box saying that the number is odd.

v) We use the alert() function to display the result in an alert box. The alert() function displays a dialog box with a message and an OK button. The message in this case is whether the input number is odd or even, which is stored in a string concatenated with the input number using the + operator.

 

Write a program to find JavaScript odd or even by using switch statement.

 



 

Write a program to find JavaScript odd or even by using function.

 



 Explanation:




i) We define a function called checkOddOrEven() that takes one parameter: number. The function uses an if-else statement to check whether the number is odd or even using the modulo operator (%), and returns a message in the form of a string.

ii) We use the prompt() function to ask the user to input a number and store it in a variable called inputNumber using the let keyword.

iii) We call the checkOddOrEven() function and pass the inputNumber variable as an argument.

iv) The checkOddOrEven() function checks whether the number argument is odd or even using an if-else statement and returns a message in the form of a string.

v) We store the result of the checkOddOrEven() function in a variable called result.

vi)We use the alert() function to display the result in an alert box. The alert() function displays a dialog box with a message and an OK button. The message in this case is the result of the checkOddOrEven() function, which is stored in the result variable.


Output:






No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages