IF Statement and FOR Statement in QBasic Examples

To find positive negative or zero.

CLS

INPUT “Enter any number” ; a

IF a > 0 THEN

PRINT “The number is positive”

ELSEIF a < 0 THEN

PRINT “The number is negative”

ELSE

PRINT “The number is zero”

END IF

END


To find even or odd

CLS

INPUT “Enter any number ” ; N

IF N MOD 2 =0 THEN

PRINT “The number is EVEN “;

ELSE

PRINT “The number is ODD “;

END IF

END

First 10 Even Number

CLS
n = 0
FOR i = 1 TO 10
PRINT n;
n = n + 2
NEXT i
END

First 10 Odd Number

CLS
n = 1
FOR i = 1 TO 10
PRINT n;
n = n + 2
NEXT i
END

SUM of First 10 odd numbers

CLS
s = 0
n = 1
FOR i = 1 TO 10
s = s + n
n = n + 2
NEXT i
COLOR 5
PRINT “The Sum of first 10 odd number is “; s
END

 

SUM of First 10 Even Numbers

CLS
s = 0
n = 2
FOR i = 1 TO 10
s = s + n
n = n + 2
NEXT i
COLOR 5
PRINT “The Sum of first 10 Even number is “; s
END

SUM of First 10 numbers

CLS
s = 0
FOR i = 1 TO 10
s = s + i
NEXT i
COLOR 5
PRINT “The Sum of first 10 number is “; s
END


To find Square and Square root

CLS

INPUT “Enter any number ” ; N

SQ=N ^ 2

SQROOT= N ^(1/2)

PRINT “The square of a number is ” ; SQ

PRINT “The square root of a number is ” ; SQROOT

END


 Convert Fahrenheit to Celsius

CLS

INPUT “Enter temperature ” ; F

C = (f-32)*(5/9)

PRINT “Temperature in Celcius is ” ; C

END


Convert Nepalese Rupees into Indian Rupees.

CLS

INPUT “Enter the currency ” ; NC

IC = NC / 1.6

PRINT ” Indian currency value ” ; IC

END


Convert Indian Rupees into Nepalese Rupees

CLS

INPUT “Enter the currency ” ; IC

NC = IC * 1.6

PRINT “Nepalese currency ” ; NC

END

 


Display Greatest among three numbers

CLS
INPUT “enter the first number “; a
INPUT “enter the second number “; b
INPUT “enter the third number “; c
IF a > b AND a > c THEN g = a
IF b > a AND b > c THEN g = b
IF c > a AND c > b THEN g = c
COLOR 6
PRINT “Greatest number is “; g
END

Display smallest among three numbers

CLS

INPUT “ENTER ANY THREE NUMBERS “; A, B, C

IF A < B AND A < C THEN

PRINT A; “IS SMALLEST ”

ELSEIF B < A AND B < C THEN

PRINT B; “IS SMALLEST ”

ELSE

PRINT C; “IS SMALLEST ”

END IF

END


WAP that makes a bill of selling goods. Also add 10% VAT in total bill with discount rate given below:

1-100    =  10%

101-500 = 20%

501-1000=30%

1001-5000=40%

5001 above=50%

CLS
INPUT “enter the amount “; a
IF a > 0 AND a <= 100 THEN
d = p * 0.10
ELSEIF a >= 101 AND a <= 500 THEN
d = a * 0.20
ELSEIF a >= 501 AND a <= 1000 THEN
d = a * 0.30
ELSEIF a >= 1001 AND a <= 5000 THEN
d = a * 0.40
ELSEIF a >= 5001 THEN
d = a * 0.50
END IF
v = (a − d) * 10 / 100
t = a − d + v
PRINT “1-100 “, , “10% ”
PRINT “101-500 “, , “20% ”
PRINT “501-1000 “, , “30% ”
PRINT “1001-5000 “, , “40% ”
PRINT “5001 and above “, “50% ”
PRINT “************************************************** ”
PRINT “You have to Pay only Rs.: “; t
END

 

WAP to create commission with information given below:
Unit Rate / Unit
upto 10000 2%
more than 10000 and less than 20000 3%
more than 20000 and less than 30000 4%
beyond 30000 5%

CLS
yeta:
PRINT “****************************************** ”
INPUT “enter the amount “; a
IF a >0 AND a< 10000 THEN
c = a * 0.02
ELSEIF a > 10000 AND a < 20000 THEN
c = a * 0.03
ELSEIF a > 20000 AND a < 30000 THEN
c = a * 0.04
ELSEIF a > 30000 THEN
c = a * .05
END IF
v = (a − c) * 0.10
t = a − c + v
PRINT “upto 10000”, , , “2%”
PRINT “more than less than 20000”, , “3%”
PRINT “more than 20000 and less than 30000”, “4%”
PRINT “beyond 30000”, , , “5%”
PRINT “*********************************************************”
PRINT “You have to Pay only Rs.:”; t
GOTO yeta
END

 

WAP to accept bill with following information:
For 200 calls Rs. 250 (Compulsory)
For next 100 calls Rs. 2 / call
Above 100 calls Rs. 1.50 / call

CLS
INPUT “enter calls “; c
IF c <= 200 THEN
r = 250
ELSEIF c <= 300 AND c > 200 THEN
r = 250 + (c − 200) * 2
ELSE
r = 250 + (100 * 2) + (c − 300) * 1.50
END IF
PRINT “Total charge=”; r
END

 

 

WAP to display fare. Accept passenger’s name, destination, number of tickets and display total. Jhapa Rs. 5000, Biratnagar= Rs. 6000 and Pokhara Rs. 7000.

CLS
PRINT ” Destination and Its Fare ”
INPUT “enter passenger’s name “; n$
INPUT “enter destination “; d$
c$ = UCASE$(d$)
c$ = LCASE$(d$)
IF c$ = “jhapa ” THEN
fare = 5000
ELSEIF c$ = “biratnagar ” THEN
fare = 6000
ELSEIF c$ = “pokhara “THEN
fare = 7000
END IF
INPUT “ticket= “; t
total = fare * t
IF c$ = “jhapa ” OR c$ = “biratnagar ” OR c$ = “pokhara ” THEN GOTO start ELSE GOTO last
start:
PRINT “Passenger’s Name= “; n$
PRINT “destination= “; d$
PRINT “total fare= “; total
END
last:
COLOR 20

PRINT “enter valid destination ”
END

 

 

WAP to find Factorial of input number

CLS
Fact = 1
INPUT ” Enter a number “; n
FOR j = 1 TO n
Fact = Fact * j
NEXT j
PRINT “Factorial is “; Fact
END

 

WAP to display the factors and sum of factors for an input number.

CLS
INPUT ” Enter a number “; n
FOR j = 1 TO n
IF n MOD j = 0 THEN
PRINT j
Sum = Sum + j
END IF
NEXT j
PRINT “Sum of factors “; Sum
END

 

WAP to display the middle number among any three numbers.

CLS
INPUT ” Enter  any three different numbers “; a, b, c
IF (a > b AND a < c) OR (a < b AND a > c) THEN
PRINT “the middle number is “; a
ELSEIF (b > c AND b < a) OR (b < c AND b > a) THEN
PRINT “the middle number is “; b
ELSE
PRINT “the middle number is “; c
END IF
END

W.A.P to SORT 5 numbers

CLS
INPUT “any 5 number “; a, b, c, d, e
IF a > b THEN SWAP a, b
IF a > c THEN SWAP a, c
IF a > d THEN SWAP a, d
IF a > e THEN SWAP a, e
IF b > c THEN SWAP b, c
IF b > d THEN SWAP b, d
IF b > e THEN SWAP b, e
IF c > d THEN SWAP c, d
IF c > e THEN SWAP c, e
IF d > e THEN SWAP d, e
PRINT a, b, c, d, e
END

 

W.A.P. to find weather its triangle or not

CLS
INPUT “enter sides “; a, b, c
IF (a + b) > c AND (b + c) > a AND (c + a) > b THEN
PRINT “its triangle ”
ELSE
PRINT “NO ”
END IF
END

 

WAP to display Prime or Composite

CLS
INPUT “Enter a number “; n
FOR j = 1 TO n
IF n MOD j = 0 THEN c = c + 1
NEXT j
IF c = 2 THEN
PRINT “It is prime number”
ELSE
PRINT “It is composite number”
END IF
END

 

WAP to display Composite number from 50 to 100

CLS
FOR n = 50 TO 100
C = 0
FOR j = 1 TO n
IF n MOD j = 0 THEN C = C + 1
NEXT j
IF C <> 2 THEN PRINT n
NEXT n
END

 

WAP to display prime numbers from 100 to 200.

CLS
FOR n=100 TO 200
C=0
FOR j=1 TO n
       IF n MOD j=0 THEN c=c+1 
NEXT j
IF c=2 THEN PRINT n
NEXT n
END


SELECT CASE TO ADD, SUBTRACT, MULTIPLY AND DIVIDE

CLS
start:
PRINT “*************************************************”

INPUT “enter the first number”; a
INPUT “enter the second number”; b
PRINT “YOUR CHOICES”
PRINT “1 for ADDITION”
PRINT “2 for SUBTRACTION”
PRINT “3 for multiplication”
PRINT “4 for division”
INPUT “Enter your choice”; choice
SELECT CASE choice
CASE 1
PRINT “THE SUM OF TWO NUMBERS”; a + b
CASE 2
PRINT ” the SUBTRACTION OF TWO NUMBERS” ; a − b
CASE 3
PRINT “THE MULTIPLICATION OF TWO NUMBERS”; a * b
CASE 4
PRINT “THE DIVISION OF TWO NUMBERS”; a / b
CASE ELSE
PRINT “INVALID CHOICE:”
END SELECT
INPUT “Do you want to do more calculation?”; ch$
IF ch$ = “Y” OR ch$ = “y” THEN GOTO start else
PRINT “THANK YOU VERY MUCH”
END

 

CLS
start:
PRINT “*************************************************”
PRINT “YOUR CHOICES”
PRINT “1 for SUNDAY”
PRINT “2 for MONDAY”
PRINT “3 for TUESDAY”
PRINT “4 for WEDNESDAY”
PRINT “5 for THURSDAY”
PRINT “6 for FRIDAY”
PRINT “7 for SATURDAY”
INPUT “Enter your choice”; choice
SELECT CASE choice
CASE 1
PRINT “SUNDAY”;
CASE 2
PRINT “MONDAY”;
CASE 3
PRINT “TUESDAY”;
CASE 4
PRINT “WEDNESDAY”;
CASE 5
PRINT “THURSDAY”;
CASE 6
PRINT “FRIDAY”;
CASE 7
PRINT “SATURDAY”;
END SELECT
PRINT “*****************************************************”
INPUT “Do you want to display again?”; ch$
IF ch$ = “Y” OR ch$ = “y” THEN GOTO start else
PRINT “THANK YOU VERY MUCH”
END

 

ARMSTRONG OR NOT

CLS
INPUT “enter any number “; num
n = num
sum = 0
WHILE num <> 0
r = num MOD 10
num = num \ 10
sum = sum + (r ^ 3)
WEND
IF n = sum THEN
PRINT “It is armstrong ”
ELSE
PRINT “It is not armstrong ”
END IF
END

Reversed Number or not

CLS
INPUT “enter any number “; num

WHILE num <> 0
r = num MOD 10
s = s * 10 + r
num = INT(num / 10)
WEND
PRINT “the reversed number is “; s
END

Palindrome number or not

CLS
INPUT “enter any number “; num
n = num
WHILE num <> 0
r = num MOD 10
s = s * 10 + r
num = INT(num / 10)
WEND
IF s = n THEN
PRINT “It is palindrome number ”
ELSE
PRINT “It is not palindrome number ”
END IF
END

 

Multiplication of any given number

CLS
INPUT “enter any number “; n
FOR i = 1 TO 20
PRINT n; “x”; i; “=”; n * i
NEXT i
END

 

 

STRING PATTERNS (CLICK HERE)

NUMERIC PATTERNS (CLICK HERE)