QBASIC – Even or Odd using SUB Procedure

Table of Contents

Overview

Below is a QBASIC program to find whether a number entered by the user is even or odd using SUB Procedure i.e., SUB…END SUB.

Using FUNCTION…END FUNCTION: Even or Odd using FUNCTION Procedure

Without Parameters

REM Program to input a number and check if it is even or odd
DECLARE SUB EVENORODD
CLS
CALL EVENORODD
END
SUB EVENORODD
  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 SUB

With Parameters:

REM Program to input a number and check if it is even or odd
DECLARE SUB EVENORODD(a)
CLS
INPUT "Enter a number";a
CALL EVENORODD(a)
END
SUB EVENORODD(a)
  IF a MOD 2 = 0 THEN
    PRINT a;" is an even number"
  ELSE 
    PRINT a;" is an odd number"
  END IF
END SUB

Output – Even or Odd using SUB Procedure

Outputs 6 is an even number if input is 6

Explanation

Notice there is no any dollar ($) sign at the end of the function name. There is no need of a dollar sign in SUB Procedure as the SUB procedure doesn’t return anything. Instead, we can do the processing and display the output in the SUB procedure. The main module will use CALL keyword to call the SUB Procedure into the main module.

Leave a Comment

Your email address will not be published. Required fields are marked *

Exit mobile version