QBASIC – Check if a number is divisible by 3 or 5 or both or not

Table of Contents

Overview

Below is the program to check whether a user input number is divisible by 3 or 5 or both or not by any.

REM Program to check if a number is divisible by 3 or 5 or both or not by 3
CLS
INPUT "Enter a number: "; n
IF n MOD 5 = 0 AND n MOD 3 = 0THEN
  PRINT n;" is divisible by 5 and 3 both"
ELSE IF n MOD 5 = 0 AND n MOD 3 <> 0 THEN
  PRINT n;" is divisible by 5 but not by 3"  
ELSE IF n MOD 3 = 0 AND n MOD 5 <> 0 THEN
  PRINT n;" is divisible by 3 but not by 5"  
ELSE 
  PRINT n;" is not divisible by 5 and 3 both"
END IF
END

Output

Outputs “15 is divisible by 5 and 3 both” when 15 is input

Explanation

To check if a number is divisible by any number, we will be using the modulo operator (MOD). we need to check if the number is divisible by 3 or 5 or not, so we need to execute modulo division of the number by 3 or 5 i.e., n MOD 3 or n MOD 5.

Also, we have used a logical operator in this program (AND). Let’s discuss AND and OR operators. AND operator is used to combining both conditions which returns true if both conditions satisfy and false if one of the conditions fails. Likewise, the OR operator is used to combining both conditions which returns true if any of the conditions satisfy, and false if both of the conditions fail.

In context to the program above, we are using AND operator to check if the number is divisible by both 5 and 3 or divisible by 5 and not 3 or divisibly by 3 and not 5.

More Programs

https://thinkshare.one/learn/programming/qbasic/

Leave a Comment

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

Exit mobile version