QBasic Program to Check if Number is Even or Odd

Write a program in QBasic to check if the input number is even or odd.

Program using IF..ELSE

CLS
INPUT "Enter a Number: ", n
IF n MOD 2 = 0 THEN
  PRINT "Input Number is Even"
ELSE
  PRINT "Input Number is Odd"
END IF    
END

Program using only IF

CLS
INPUT "Enter a Number: ", n
IF n MOD 2 = 0 THEN
  PRINT "Input Number is Even"
END IF
IF n MOD 2 = 1 THEN
  PRINT "Input Number is Odd"
END IF   
END

Program using Select Case

CLS
INPUT "Enter a Number: ", n

SELECT CASE (n MOD 2)

  CASE 1
    PRINT "Input Number is Odd"

  CASE 0
    PRINT "Input Number is EVEN"

END SELECT

END

Program using subroutine

DECLARE SUB odd_even
CLS
odd_even
END

SUB odd_even
INPUT "Enter a Number: ", n
IF n MOD 2 = 0 THEN
  PRINT "Input Number is Even"
ELSE
  PRINT "Input Number is Odd"
END IF  
END SUB

Program using subroutine – Passing input as an argument

DECLARE SUB odd_even(n)

CLS
INPUT "Enter a Number: ", n
odd_even(n)
END

SUB odd_even(n)
IF n MOD 2 = 0 THEN
  PRINT "Input Number is Even"
ELSE
  PRINT "Input Number is Odd"
END IF  
END SUB

Program using Function

DECLARE FUNCTION odd_even()

CLS
odd_even()
END

FUNCTION odd_even()
INPUT "Enter a Number: ", n
IF n MOD 2 = 0 THEN
  PRINT "Input Number is Even"
ELSE
  PRINT "Input Number is Odd"
END IF
END FUNCTION

Program using Function – Passing input as an argument

DECLARE FUNCTION odd_even(n AS INTEGER)

CLS
INPUT "Enter a Number: ", n
r = odd_even(n)
END

FUNCTION odd_even(n AS INTEGER)
IF n MOD 2 = 0 THEN
  PRINT "Input Number is Even"
ELSE
  PRINT "Input Number is Odd"
END IF
END FUNCTION

Program using Function – Passing input as an argument and returning True/False

DECLARE FUNCTION odd(n AS INTEGER)

CLS
INPUT "Enter a Number: ", n
r = odd(n)
IF r = 0 THEN
  PRINT "Input Number is Even"
ELSE
  PRINT "Input Number is Odd"
END IF
END

FUNCTION odd(n AS INTEGER)
odd = n MOD 2
END FUNCTION

0 is considered as false and 1 is considered as true.

Read

Leave a Comment