(ii) Conditional Programs
Program-11 Write QBASIC Program to find the greater number || smaller among two different numbers.
(11-A) Write a QBASIC program to find the greater number among any two different numbers.
Program-12 Write QBASIC Program to that asks three number and display the greatest || smallest one.
(12-A) Write a QBASIC program to find the greatest number
among any three different numbers.
Answer:
CLS
INPUT "Enter three numbers ";A,B,C
IF A>B AND A>C THEN
PRINT A; " is greatest number. "
ELSEIF B>A AND B>C then
PRINT B; " is greatest number. "
ELSE
PRINT C;" is greatest number. "
END IF
END
(12-B) Write a QBASIC program to find the smallest number
among any three different numbers.
Answer:
CLS
INPUT "Enter three numbers ";A,B,C
IF A<B AND A<C THEN
PRINT A; " is smallest number. "
ELSEIF B<A AND B<C then
PRINT B; " is smallest number. "
ELSE
PRINT C;" is smallest number. "
END IF
END
Program-13 Write QBASIC Program to that asks three different numbers and display the middle number.
CLS
INPUT "Enter three numbers ";A,B,C
IF (A>B AND A<C) OR (A<B AND A>C)THEN
PRINT A; " is middle number. "
ELSEIF (B>A AND B<C) OR (B<A AND B>C) THEN
PRINT B; " is middle number. "
ELSE
PRINT C;" is middle number. "
END IF
END
Program-14 Write QBASIC Program to test given number is divisible by 7 or not.
CLS
INPUT "Enter a number ";N
R = N MOD 7
IF R=0 THEN
PRINT N; " is divisible by 7"
ELSE
PRINT N; " is not divisible by 7"
END IF
END
Program-15 Write QBASIC Program to test given number is odd or even.
CLS
INPUT "Enter a number ";N
R = N MOD 2
IF R=0 THEN
PRINT N; " is even number "
ELSE
PRINT N;" is odd number "
END IF
END
Program-16 Write QBASIC program to INPUT mark of computer science and check whether student is pass or fail where pass mark is 40.
CLS
INPUT "Enter mark of computer ";C
IF C>=40 THEN
PRINT "Pass "
ELSE
PRINT "Fail"
END IF
END
Program-17 Write QBASIC program to test whether the entered number is positive, negative or zero.
CLS
INPUT "Enter a number ";N
IF N>0 THEN
PRINT "Positive "
ELSEIF N<0 THEN
PRINT "Negative"
ELSE
PRINT "Zero"
END IF
END
Program-18 Write QBASIC program to ask CP and SP and test for profit or loss.
CLS
INPUT "Enter CP and SP";CP,SP
IF SP>CP THEN
P=SP-CP
PRINT "Profit=";P
ELSE
L=CP-SP
PRINT "Loss=";L
End IF
END
Program-19 Write QBASIC program to check whether the given year is leap year or not.
CLS
INPUT "ENTER A YEAR",Y
IF Y MOD 4=0 AND Y MOD 100<>0 OR Y MOD 400=0 THEN
PRINT "LEAP YEAR"
ELSE
PRINT "NOT LEAP YEAR"
END IF
END
Program-20 Write QBASIC menu driven program that inputs two numbers and perform the following operations according to user's choice:
1. Sum 2. Difference 3. Product 4. Division
CLS
PRINT "Main-Menu"
PRINT "1. Sum"
PRINT "2. Difference"
PRINT "3. Product"
PRINT "4. Division"
INPUT "Enter two numbers "; a,b
INPUT "Enter your choice"; CH
SELECT CASE CH
CASE 1:
s = A + B
PRINT "Sum=";s
CASE 2:
d = A - B
PRINT "Difference=";d
CASE 3:
p = A * B
PRINT "Product=";p
CASE 4:
div = A / B
PRINT "Division=";div
CASE ELSE
PRINT "Wrong Choice"
End SELECT
END
(iii) Looping Programs
Program-21 Write QBASIC program to display first ten natural numbers.
CLS
FOR I = 1 TO 10
PRINT I
NEXT I
END
Program-22 Write QBASIC program to display 10,9,8,...1.
CLS
FOR I = 10 TO 1 STEP-1
PRINT I
NEXT I
END
Program-23 Write QBASIC program to display first twenty even numbers.
CLS
A=2
FOR I = 1 TO 20
PRINT A
A=A+2
NEXT I
END
Program-24 Write QBASIC program to PRINT the factorial of given number.
[The product of a given positive integer multiplied by all lesser positive integers: The quantity five factorial (5!) = 5 x 4 x 3 x 2 x 1 = 120.]
INPUT "Enter a number"; N
F=1
FOR I = N TO 1 STEP -1
F = F * I
NEXT I
PRINT "Factorial of " ; N; " = "; F
END
Program-25 Write QBASIC to PRINT multiplication of 5 || 'N' number.
(25 A) Write QBASIC Program to PRINT multiplication table of 5 .
CLS
FOR I = 1 TO 10 STEP 1
ANS = 5 * I
PRINT ANS
NEXT I
END
(25 B) Write QBASIC Program to PRINT multiplication table of "N" .
CLS
INPUT "Enter a number"; N
FOR I = 1 TO 10 STEP 1
ANS = N * I
PRINT ANS
NEXT I
END
Program-26 Write QBASIC program to PRINT sum of first ten natural numbers.
CLS
Sum = 0
For I = 1 to 10
Sum = Sum + I
Next I
PRINT "Sum of first ten natural numbers = "; Sum
END
Program-27 Write QBASIC program to PRINT factors of given number.
CLS
INPUT "Enter a number ";N
For I = 1 to N
IF N MOD I = 0 Then PRINT I
Next I
END
Program-28 Write QBASIC program to PRINT sum of digits of entered number. [e.g. 123=1+2+3=6]
CLS
INPUT "Enter a number ";N
Sum = 0
While N<>0
R = N MOD 10
Sum = Sum + R
N = N \ 10
Wend
PRINT "Sum of digits of entered number =";Sum
End
Program-29 Write QBASIC program to PRINT the product of digits of entered number. [e.g. 123=1*2*3=6]
CLS
INPUT "Enter a number ";N
pro = 1
While N<>0
R = N MOD 10
pro = pro * R
N = N \ 10
Wend
PRINT "Product of digits of entered number =";pro
END
Program-30 Write QBASIC program to reverse given number. [e.g. 123=321]
CLS
INPUT "Enter a number ";N
While N<>0
R = N MOD 10
rev = rev * 10 + R
N = N \ 10
Wend
PRINT "Reverse of given number =";rev
END
Program-31 Write QBASIC program to reverse a given string. [e.g. PLK=KLP]
CLS
INPUT "Enter any string ";W$
FOR I= LEN(W$) TO 1 STEP-1
C$ = C$ + MID$(W$, I, 1)
NEXT I
PRINT "Reverse of given string= ";C$
END
Program-32 Write QBASIC program to display 100,95,90,.... upto 10th terms.
CLS
A = 100
FOR I= 1 TO 10
PRINT A
A = A - 5
NEXT I
END
Program-33 Write QBASIC program to display 1,4,9,.... upto 10th terms. Hint: (1*1=1), (2*2=4), (3*3=9),(4*4=16),...
CLS
FOR I= 1 TO 10
PRINT I^2
NEXT I
END
Program-34 Write QBASIC program to display 1,8,27,64,..... upto 10th terms.
Hint: (1*1*1=1), (2*2*2=8), (3*3*3=27),(4*4*4=64),...
CLS
FOR I= 1 TO 10
PRINT I^3
NEXT I
END
Program-35 Write QBASIC program to display 1,2,4,7,11,.... upto 10th terms.
Hint: 1, (1+1=2), (2+2=4),(4+3=7),...
CLS
A = 1
FOR I= 1 TO 10
PRINT A
A = A + I
NEXT I
END
Program-36 Write QBASIC program to display 1,5,9,13,.... upto 10th terms.
Hint: 1, (1+4=5), (5+4=9),(9+4=13),...
CLS
A = 1
FOR I= 1 TO 10
PRINT A
A = A + 4
NEXT I
END
Program-37 Write QBASIC program to display 2,5,9,14,.... upto 10th terms.
Hint: 2, (2+3=5), (5+4=9),(9+5=14),...
CLS
A = 2
B = 3
FOR I= 1 TO 10
PRINT A
A = A + B
B=B+1
NEXT I
END
Program-38 Write QBASIC program to display 2,8,15,23,32,.... upto 10th terms. Hint: 2, (2+6=8), (8+7=15),(15+8=23),(23+9=32),...
CLS
A = 2
B = 6
FOR I= 1 TO 10
PRINT A
A = A + B
B = B + 1
NEXT I
END
Program-39 Write QBASIC program to display 2,8,18,32,.... upto 10th terms. Hint: (1*1*2=2), (2*2*2=8), (3*3*2=18),(4*4*2=32),...
CLS
FOR I= 1 TO 10
PRINT I^2*2
NEXT I
END
Program-40 Write QBASIC program to display 0,1,1,2,3,5,.... upto 10th terms.[Fibonacci series]
An integer in the infinite sequence 0,1, 1, 2, 3, 5, 8, 13, … of which the first two terms are 0 and 1 and each succeeding term is the sum of the two immediately preceding is called Fibonacci Series.
CLS
A = 0
B = 1
FOR I = 1 TO 10
PRINT A
C = A + B
A = B
B = C
NEXT I
END
(iv) Looping Programs with Condition
Program-41 Write QBASIC program to display 7,22,11,34,.... upto 10th terms. [Hailstone series]
Start with any integer value greater than 0, say x. If x is even, then the next value in the series is x/2; if x is odd, then the next value in the series is 3x + 1. This type of series is called Hailstone series.
CLS
N = 7
FOR I = 1 TO 10
PRINT N
IF N MOD 2 = 0 THEN
N = N / 2
ELSE
N = 3 * N + 1
END IF
NEXT I
END
Program-42 Write QBASIC program to test whether the given number is prime or composite number.
[A number that is divisible by itself and 1 is called prime number. (e.g. 2,3,5,7,11 etc)]
CLS
INPUT "Enter a number"; N
C = 0
FOR I = 1 TO N STEP 1
R = N MOD I
IF R=0 THEN C=C+1
NEXT I
IF C=2 THEN
PRINT N;" is prime number"
ELSE
PRINT N;"is composite number"
END IF
END
Program-43 Write QBASIC program to display prime numbers from 1 to 100.
CLS
FOR N = 1 TO 100
C = 0
FOR I = 1 TO N STEP 1
R = N MOD I
IF R=0 THEN C=C+1
NEXT I
IF C=2 THEN PRINT N;
NEXT N
END
Program-44 Write QBASIC program to test whether the given number is palindrome or not.
A word, phrase, or sequence that reads the same backwards as forwards is called Palindrome. Example : 121 , 101 etc
CLS
INPUT "Enter a number"; N
A = N
WHILE N<>0
R = N MOD 10
REV = REV * 10 + R
N = N \ 10
WEND
IF A=REV THEN
PRINT A;" is Palindrome Number"
ELSE
PRINT A;"is not Palindrome Number"
END IF
END
Program-45 Write QBASIC program to check whether the entered number is Armstrong number or not.
An Armstrong number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number it self. Hence 153 is Armstrong Number because 13+53+33=1+125+27=153,370,371,407.
CLS
INPUT "Enter a number"; N
A = N
WHILE N<>0
R = N MOD 10
SUM = SUM + R * R * R
N = N \ 10
WEND
IF A=SUM THEN
PRINT A;"is Armstrong Number"
ELSE
PRINT A;"is not Armstrong Number"
END IF
END
Program-46 Write QBASIC program to display Armstrong number from 1 to 1000.[Hint: 1,153,370,371,407 are Armstrong Number].
CLS
FOR I = 1 TO 1000
SUM = 0
N = I
WHILE N<>0
R = N MOD 10
SUM = SUM + R * R * R
N = N \ 10
WEND
IF I=SUM THEN PRINT I
NEXT I
END
Program-47 Write QBASIC program to count the total number of vowels in a given string.
CLS
INPUT "Enter any string ";W$
W$ = UCASE$(W$)
FOR I= 1 TO LEN(W$)
C$ = MID$(W$, I, 1)
SELECT CASE C$
CASE "A","E","I","O","U"
C = C + 1
END SELECT
NEXT I
PRINT "Number of vowels=";C
END
Program-48 Write QBASIC program to count the total number of consonants in a given string.
CLS
INPUT "Enter any string ";W$
W$ = UCASE$(W$)
FOR I= 1 TO LEN(W$)
C$ = MID$(W$, I, 1)
SELECT CASE C$
CASE "A","E","I","O","U"
CASE ELSE
C = C + 1
END SELECT
NEXT I
PRINT "Number of consonants=";C
END
Program-49 Write QBASIC program to check whether the given string is palindrome or not.
CLS
INPUT "Enter any string ";W$
FOR I= LEN(W$) TO 1 STEP-1
C$ = C$ + MID$(W$, I, 1)
NEXT I
IF W$=C$ THEN
PRINT "Palindrome String"
ELSE
PRINT "Not Palindrome String"
END IF
END
Program-50 Write QBASIC program to count total number of word in a given sentence.
CLS
INPUT "Enter any sentence ";S$
sp = 1
FOR I= 1 TO LEN(S$)
C$ = MID$(S$, I, 1)
IF C$=" " THEN sp=sp+1
NEXT I
PRINT "Total words=";sp
END
Program-51 Write QBASIC program to calculate HCF of two numbers.
CLS
INPUT "Enter two numbers "; A,B
FOR I = 1 TO A
IF A MOD I = 0 AND B MOD I = 0 THEN
H = I
END IF
NEXT I
PRINT "HCF =";H
END
Program-52 Write QBASIC program to calculate LCM of two numbers.
CLS
INPUT "Enter two numbers ";A,B
FOR I = 1 TO A
IF A MOD I = 0 AND B MOD I = 0 THEN
H = I
END IF
NEXT I
LCM = (A * B) / H
PRINT "LCM ="; LCM
END
Nested Loop Patterns
Program-53Write QBASIC program to display:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Answer
CLS
FOR I = 1 TO 5
FOR J = 1 TO I
PRINT J;
NEXT J
NEXT I
END
Program-54 Write QBASIC program to display:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Answer:
CLS
FOR I = 1 TO 5
FOR J = 1 TO I
PRINT I;
NEXT J
NEXT I
END
Program-55 Write QBASIC program to display:
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
Answer:
CLS
FOR I = 5 TO 1 STEP-1
FOR J = 5 TO I STEP-1
PRINT J;
NEXT J
NEXT I
END
Program-56 Write QBASIC program to display:
1 1 1 1 1
2 2 2 2
3 3 3
2 2
1
Answer:
CLS
FOR I = 1 TO 5
FOR J = 5 TO I STEP-1
PRINT I;
NEXT J
NEXT I
END
Program-57 Write QBASIC program to display:
1
1 3
1 3 5
1 3 5 7
Answer:
CLS
FOR I = 1 TO 7 STEP +2
FOR J = 1 TO I STEP +2
PRINT J;
NEXT J
NEXT I
END
Program-58 Write QBASIC program to display:
1
3 3
5 5 5
7 7 7 7
Answer:
CLS
FOR I = 1 TO 7 STEP +2
FOR J = 1 TO I STEP +2
PRINT I;
NEXT J
NEXT I
END
Program-59 Write QBASIC program to display:
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
Answer:
CLS
K=0
FOR I = 1 TO 5
FOR J = 1 TO 3
PRINT J+K;
NEXT J
K=K+1
NEXT I
END
Program-60 Write QBASIC program to display : 1 2 3 6 11 20 37 .... Up to 10th terms.
Answer:
CLS
A = 1
B = 2
C = 3
FOR I = 1 TO 10
D = A + B + C
PRINT A
A = B
B = C
C = D
NEXT I
END
Program-61 Write QBASIC program to display : 5,16,8,4,2,1,4,2,1,4.
Answer:
CLS
N = 5
FOR I = 1 TO 10
PRINT N
IF N MOD 2 = 0 THEN
N = N / 2
ELSE
N = 3 * N + 1
END IF
NEXT I
END
Program-61 Write QBASIC program to display : 66666,6666,666,66,6.
Answer:
CLS
A=66666
FOR I = 1 TO 5
PRINT A ;
A=A\10
NEXT I
END
Program-62 Write QBASIC program to display : 2,8,18,32,.... up to 10th terms
Answer:
CLS
FOR I = 1 TO 10
PRINT I^2*2 ;
NEXT I
END
Program-63 Write QBASIC program to display : 7,22,11,34,17,52,26,13,40,20.
Answer:
CLS
N = 7
FOR I = 1 TO 10
PRINT N
IF N MOD 2 = 0 THEN
N = N / 2
ELSE
N = 3 * N + 1
END IF
NEXT I
END