Below is a QBASIC program to find if a number entered by the user is even or odd.
' Program to input a number and check if it is even or odd
CLS
INPUT "Enter a number";a
IF a MOD 2 = 0 THEN
PRINT a;" is an even number"
ELSE
PRINT a;" is an odd number"
END IF
END
Explanation:
First, let’s understand even and odd numbers. Even numbers are those that are divisible by 2 (2,4,6,8,10…) and the ones that are not divisible by 2 are odd numbers(1,3,5,7,9,…). Now, to determine whether a number is even or odd, we need to find if the number entered by the user is divisible by 2 or not.
For this purpose, we are using a MOD operator provided by QBASIC. MOD is a modulus operator used to find the remainder of any number before the operator divided by the number after the operator. Now, let’s go to the program. You can notice a MOD 2; here a is divided by 2 and gives back a remainder. The remainder is either 0 or 1 as the number is divided by 2.
Also, if statement is used in the above program to check if the remainder is 0 then display the number is even else display the number is odd.