(X) SEE Computer Lab Questions

1
Structure of END ... SUB END

DECLARE SUB PROCEDURE_NAME(PARAMETERS) INPUT STATEMENTS CALL PROCEDURE_NAME(PARAMETERS) END SUB PROCEDURE_NAME(PARAMETERS) FORMULA || PROCESS PRINT STATEMENTS END SUB

Simple Mathematical Programs using Sub

1. Find sum of two numbers using Sub ... End Sub.
Click for Video Tutorial -answer
DECLARE SUB SUM(A,B) INPUT "Enter two numbers ",A,B CALL SUM(A,B) END SUB SUM(A,B) S=A+B PRINT "Sum of two numbers is " , S END SUB
2. Find product of two numbers using Sub ... End Sub.
DECLARE SUB PRODUCT(A,B) INPUT "Enter any two numbers ",A,B CALL PRODUCT(A,B) END SUB PRODUCT(A,B) P = A * B PRINT "Product of two numbers is";P END SUB
3. Find simple interest using Sub ... End Sub.
DECLARE SUB INTEREST(P,T,R) INPUT "Enter principal , time and rate ",P,T,R CALL INTEREST (P,T,R) END SUB INTEREST(P,T,R) I = (P * T * R) / 100 PRINT "Simlple Interest is ";I END SUB
4. Find area || circumference of circle using Sub...End Sub.
DECLARE SUB AREA(R) INPUT "Enter radius ",R CALL AREA (R) END SUB AREA(R) A = 3.14 * R * R PRINT "Area of circle is ";A END SUB
DECLARE SUB CIRCUM(R) INPUT "Enter radius ",R CALL CIRCUM (R) END SUB CIRCUM(R) C = 2 * 3.14 * R PRINT " Circumference of circle is ";C END SUB
5. Find area and perimeter of a room using Sub...End Sub.
DECLARE SUB CAL(L,B) INPUT "Enter Length and Breadth ",L,B CALL CAL(L,B) END SUB CAL(L,B) A = L * B P = 2 * (L + B) PRINT " Area is ";A PRINT "Perimeter is ";P END SUB
6. Find the square root of given number using Sub...End Sub.
DECLARE SUB SROOT(N) INPUT " Enter any number ",N CALL SROOT(N) END SUB SROOT(N) SR = SQR(N) PRINT " Square root is ";SR END SUB
7. Write QBASIC Program to total surface area of a cube where TSA = 6L2 using Sub...End Sub.
DECLARE SUB TSA(L) INPUT " Enter Length ",L CALL TSA(L) END SUB TSA(L) A = 6 * L ^ 2 PRINT " TSA is ";A END SUB
8. Write QBASIC Program to convert degree Celsius and convert it into Fahrenheit where F=(9*C/5)+32 using Sub...End Sub.
DECLARE SUB TEMP(C) INPUT " Enter temp. in Celsius : ",C CALL TEMP(C) END SUB TEMP(C) F=(9*C/5)+32 PRINT "Temp. in Fahrenheit is " ; F END SUB
9. Write QBASIC Program to convert minutes into hours and minutes using Sub...End Sub.
DECLARE SUB CAL(MIN) INPUT " Enter time in minutes : ",MIN CALL CAL(MIN) END SUB CAL(MIN) HR = MIN \ 60 M = MIN MOD 60 PRINT HR ; "HOUR" ; M ; "MINUTE" END SUB
10. Write QBASIC Program to input number of days and convert it into years, months and days using Sub...End Sub.
DECLARE SUB CAL(D) INPUT " Enter days : ",D CALL CAL(D) END SUB CAL(D) YR = D \ 365 D1 = D MOD 365 MTH = D1 \ 30 DAY = D1 MOD 30 PRINT YR ; "YEARS" ; MTH ; "MONTHS" ; DAY ; "DAYS" END SUB

Conditional Statements Without Looping using Sub

11. Write QBASIC Program to that asks two number and display the smaller | greater one using Sub...End Sub.
DECLARE SUB CHECK(A,B) INPUT "Enter two numbers : ", A,B CALL CHECK(A,B) END SUB CHECK(A,B) IF A<B THEN PRINT A;" is smaller" ELSE PRINT B; " is smaller" END IF END SUB
DECLARE SUB CHECK(A,B) INPUT "Enter two numbers : ", A,B CALL CHECK(A,B) END SUB CHECK(A,B) IF A>B THEN PRINT A;" is greater" ELSE PRINT B; " is greater" END IF END SUB
12. Write QBASIC Program to that asks three number and display the greatest || smallest one using Sub...End Sub.
DECLARE SUB CHECK(A,B,C) INPUT "Enter three numbers : ", A,B,C CALL CHECK(A,B,C) END SUB CHECK(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 SUB
DECLARE SUB CHECK(A,B,C) INPUT "Enter three numbers : ", A,B,C CALL CHECK(A,B,C) END SUB CHECK(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 SUB
13. Write QBASIC Program to that asks three different numbers and display the middle number using Sub...End Sub.
DECLARE SUB CHECK(A,B,C) INPUT "Enter three numbers : ", A,B,C CALL CHECK(A,B,C) END SUB CHECK(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 SUB
14. Write QBASIC Program to test given number is divisible by 7 or not using Sub...End Sub.
DECLARE SUB CHECK(N) INPUT " Enter any number : ",N CALL CHECK(N) END SUB CHECK(N) R = N MOD 7 IF R = 0 THEN PRINT " Divisible by 7 " ELSE PRINT " Not divisible by 7 " END IF END SUB
15. Write QBASIC Program to test given number is odd or even using Sub...End Sub.
DECLARE SUB CHECK(N) INPUT " Enter any number : ",N CALL CHECK(N) END SUB CHECK(N) R = N MOD 2 IF R = 0 THEN PRINT " Even number " ELSE PRINT " Odd number " END IF END SUB
16. Write QBASIC Program to input mark of computer science and check whether student is pass or fail where pass mark is 40 using Sub...End Sub.
DECLARE SUB CHECK(C) INPUT " Enter computer mark : ",C CALL CHECK(C) END SUB CHECK(C) IF C >= 40 THEN PRINT " Pass " ELSE PRINT " Fail " END IF END SUB
17. Write QBASIC Program to test given number is positive, negative or zero using Sub...End Sub.
DECLARE SUB TEST (N) INPUT " Enter any number : ",N CALL TEST(N) END SUB TEST(N) IF N> 0 THEN PRINT " Positive " ELSEIF (N0) THEN PRINT " Negative " ELSE PRINT " Zero " END IF END SUB
18. Write QBASIC Program to ask CP and SP and test for profit or loss using Sub...End Sub.
DECLARE SUB TEST (SP,CP) INPUT " Enter selling and cost price: ",SP,CP CALL TEST (SP,CP) END SUB TEST(SP,CP) IF SP> CP THEN P = SP - CP PRINT " PROFIT : ";P ELSE L = CP - SP PRINT " LOSS : ";L END IF END SUB
19. Write QBASIC Program to display first ten natural numbers using Sub...End Sub.
DECLARE SUB SERIES() CALL SERIES END SUB SERIES FOR I = 1 TO 10 PRINT I NEXT I END SUB
20. Write QBASIC Program to display 10 9 8 ..... to 1 using Sub...End Sub.
DECLARE SUB SERIES() CALL SERIES END SUB SERIES FOR I = 10 TO 1 STEP-1 PRINT I NEXT I END SUB
21.Write QBASIC Program to display first twenty even numbers using Sub...End Sub.
DECLARE SUB SERIES() CALL SERIES END SUB SERIES E = 2 FOR I = 1 TO 20 PRINT E E = E + 2 NEXT I END SUB
22. Write QBASIC Program to print the factorial of given number using Sub...End Sub.
[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.]
DECLARE SUB FACTO(N) INPUT " Enter any number : ",N CALL FACTO(N) END SUB FACTO(N) F=1 FOR I = N TO 1 STEP -1 F = F * I NEXT I PRINT "Factorial of " ; N; " = "; F END SUB
23. Write QBASIC Program to print multiplication table of 5 || "N" using Sub...End Sub.
DECLARE SUB MTABLE() CALL MTABLE END SUB MTABLE FOR I = 1 TO 10 PRINT 5 * I NEXT I END SUB
DECLARE SUB MTABLE(N) INPUT "Enter any number : ", N CALL MTABLE(N) END SUB MTABLE(N) FOR I = 1 TO 10 PRINT N * I NEXT I END SUB
24. Write QBASIC Program to print sum of first ten natural numbers using Sub...End Sub.
DECLARE SUB SERI() CALL SERI END SUB SERI S = 0 FOR I = 1 TO 10 S = S + I NEXT I PRINT "Sum is";S END SUB
25. Write QBASIC Program to print factors of given number using Sub...End Sub.
DECLARE SUB FACTOR(N) INPUT " Enter any number : ";N CALL FACTOR(N) END SUB FACTOR(N) FOR I = 1 TO N IF N MOD I = 0 THEN PRINT I END IF NEXT I END SUB
26. Write QBASIC Program to print sum of digits of entered number using Sub...End Sub.[eg. 123=1+2+3=6]
DECLARE SUB SOD(N) INPUT " Enter any number : " , N CALL SOD(N) END SUB SOD(N) S=0 WHILE N<>0 R = N MOD 10 S = S + R N = N \ 10 WEND PRINT " Sum of Digits: ";S END SUB
27. Write QBASIC Program to print product of digits of entered number using Sub...End Sub. [eg. 132=1*3*2=6]
DECLARE SUB Prod(N) INPUT " Enter any number : " , N CALL Prod(N) END SUB Prod(N) P=1 WHILE N<>0 R = N MOD 10 P = P * R N = N \ 10 WEND PRINT " Product of Digits: "; P END SUB
28. Write QBASIC Program to reverse given number using Sub...End Sub.[eg. 123 = 321]
DECLARE SUB REV(N) INPUT " Enter any number : " , N CALL REV(N) END SUB REV(N) WHILE N<>0 R = N MOD 10 S = S * 10 + R N = N \ 10 WEND PRINT " Reverse of number : ";S END SUB
29. Write QBASIC Program to reverse a given string using Sub...End Sub.[eg. PLK = KLP]
DECLARE SUB REV(W$) INPUT " Enter any String : " , W$ CALL REV(W$) END SUB REV(W$) FOR I = LEN(W$) TO 1 STEP-1 R$ = R$ + MID$(W$, I, 1) NEXT I PRINT " Reverse of String : " ; R$ END SUB
30. Write QBASIC program to display 100,95,90...... upto 10th terms using Sub...End Sub.
DECLARE SUB SERIES() CALL SERIES END SUB SERIES A = 100 FOR I = 1 TO 10 PRINT A A = A - 5 NEXT I END SUB
31. Write QBASIC program to display 1,4,9,16 ...... upto 10th terms using Sub...End Sub.
DECLARE SUB SERIES() CALL SERIES END SUB SERIES FOR I = 1 TO 10 PRINT I^2 NEXT I END SUB
32. Write QBASIC program to display 1,27,64 ...... upto 10th terms using Sub...End Sub.
DECLARE SUB SERIES() CALL SERIES END SUB SERIES FOR I = 1 TO 10 PRINT I^3 NEXT I END SUB
33. Write QBASIC program to display 1,2,4,7,11 ...... upto 10th terms using Sub...End Sub.
DECLARE SUB SERIES() CALL SERIES END SUB SERIES A = 1 FOR I= 1 TO 10 PRINT A A = A + I NEXT I END SUB
34. Write QBASIC program to display 1,5,9,13...... upto 10th terms using Sub...End Sub.
DECLARE SUB SERIES() CALL SERIES END SUB SERIES A = 1 FOR I= 1 TO 10 PRINT A A = A + 4 NEXT I END SUB
35. Write QBASIC program to display 2,5,9,14...... upto 10th terms using Sub...End Sub.
DECLARE SUB SERIES() CALL SERIES END SUB SERIES A = 2 B = 3 FOR I= 1 TO 10 PRINT A A = A + B B = B + 1 NEXT I END SUB
36. Write QBASIC program to display 2,8,15,23,32 ...... upto 10th terms using Sub...End Sub.
DECLARE SUB SERIES() CALL SERIES END SUB SERIES A = 2 B = 6 FOR I= 1 TO 10 PRINT A A = A + B B = B + 1 NEXT I END SUB
37. Write QBASIC program to display 2,8,18,32 ...... upto 10th terms using Sub...End Sub.
DECLARE SUB SERIES() CALL SERIES END SUB SERIES FOR I = 1 TO 10 PRINT I^2*2 NEXT I END SUB
38. Write QBASIC program to display 0,1,1,2,3,5 ...... upto 10th terms using Sub...End Sub.[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.]
DECLARE SUB FIBO() CALL FIBO END SUB FIBO A = 0 B = 1 FOR I = 1 TO 10 PRINT A C = A + B A = B B = C NEXT I END SUB

(iv) Looping Programs with Condition

39. Write QBASIC program to display 7,22,11,34 ...... upto 10th terms using Sub...End Sub.[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.]
DECLARE SUB SERIES() CALL SERIES END SUB SERIES 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 SUB
40. Write QBASIC program to test whether the given number is prime or composite number using Sub...End Sub.
[A number that is divisible by itself and 1 is called prime number. (e.g. 2,3,5,7,11 etc)]
DECLARE SUB TEST(N) INPUT " Enter a number : ", N CALL TEST(N) END SUB TEST(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 SUB
41. Write QBASIC program to display prime numbers from 1 to 100 using Sub...End Sub.
DECLARE SUB SERIES() CALL SERIES END SUB SERIES 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 SUB
42. Write QBASIC program to test whether the given number is palindrome or not using Sub..End Sub.
[A word, phrase, or sequence of number that reads the same backwards as forwards is called Palindrome. Example : 121 , 101 etc]
DECLARE SUB PALIN(N) INPUT " Enter any number : ",N CALL PALIN(N) END SUB PALIN(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 SUB
43. Write QBASIC program to test whether the given number is Armstrong or not using Sub..End Sub.
[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.]
DECLARE SUB ARM(N) INPUT " Enter any number : ",N CALL ARM(N) END SUB ARM(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 SUB
44. Write QBASIC program to display Armstrong number from 1 to 1000 using Sub...End Sub.[Hint: 1,153,370,371,407 are Armstrong Number].
DECLARE SUB SERIES() CALL SERIES END SUB SERIES 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 SUB
45. Write QBASIC program to count the total number of vowels in a given string using Sub...End Sub.
DECLARE SUB VCOUNT(W$) INPUT "Enter any string ";W$ CALL VCOUNT(W$) END SUB VCOUNT(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 SUB
46. Write QBASIC program to count the total number of consonants in a given string using Sub...End Sub.
DECLARE SUB CCOUNT(W$) INPUT "Enter any string ";W$ CALL CCOUNT(W$) END SUB CCOUNT(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 SUB
47. Write QBASIC program to check whether the given string is palindrome or not using Sub...End Sub.
DECLARE SUB TEST(W$) INPUT " Enter any string : ";W$ CALL TEST(W$) END SUB TEST(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 SUB
48. Write QBASIC program to count total number of word in a given sentence using Sub...End Sub.
DECLARE SUB COUNT(S$) INPUT " Enter any sentence : ",S$ CALL COUNT(S$) END SUB COUNT(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 SUB
49. Write QBASIC program to calculate HCF of two numbers using Sub...End Sub.
DECLARE SUB HCF(A,B) INPUT "Enter two numbers "; A,B CALL HCF(A,B) END SUB HCF(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 SUB
50. Write QBASIC program to calculate LCM of two numbers using Sub...End Sub.
DECLARE SUB LCM(A,B) INPUT "Enter two numbers ";A,B CALL LCM(A,B) END SUB LCM(A,B) FOR I = 1 TO A IF A MOD I = 0 AND B MOD I = 0 THEN H = I END IF NEXT I L = (A * B) / H PRINT "LCM ="; L END SUB
51. Write QBASIC program to display following using Sub...End Sub.
P EPA NEPAL
Structure of Function Procedure
DECLARE FUNCTION function_name (parameter list) INPUT statements [ask all parameter] PRINT function_name (parameter list) END FUNCTION function_name (parameter list) Function_name = formula | process | calculations END FUNCTION
Example program ---- Write a program using Function ... End Function to find the sum of two numbers.
DECLARE FUNCTION SUM(A,B) INPUT " Enter any two numbers : ", A,B PRINT " Sum of two numbers is : " ; SUM(A,B) END FUNCTION SUM(A,B) SUM = A + B END FUNCTION
Q1. Write a program using Function ... End Function to find area of rectangle. [Hint:A=LxB].
DECLARE FUNCTION AREA(L,B) INPUT "Enter length and breadth: ";L,B PRINT "Area = "; AREA(L,B) END FUNCTION AREA(L,B) A = L * B AREA = A END FUNCTION
Q2.Write a program to calculate perimeter of rectangle using FUNCTION procedure. [Hint: P=2(L+B)]
DECLARE FUNCTION PERI(L,B) INPUT "Enter length and breadth: ";L,B PRINT "Perimeter = ";PERI(L,B) END FUNCTION PERI(L,B) P = 2 * (L + B) PERI = P END FUNCTION
Q3. Write a program using Function ... End Function to find simple interest.
DECLARE FUNCTION SI(P,T,R) INPUT " Enter Principal, time and rate : ", P,T,R PRINT " Simple Interest is : " ; SI(P,T,R) END FUNCTION SI(P,T,R) SI=(P*T*R)/100 END FUNCTION
Q4.Write a program to calculate the average of three numbers using FUNCTION procedure. [SEE-2075 R]
DECLARE FUNCTION AVG(A,B,C) INPUT " Enter three numbers: ",A,B,C PRINT " Average of three numbers is "; AVG(A,B,C) END FUNCTION AVG(A,B,C) AVG = (A + B + C) / 3 END FUNCTION
Q5. Write a program to calculate and return total surface area of a box using FUNCTION -- END FUNCTION. [Hint: A= 2(1b+1h+bh)] [SEE 075 Province-2]
DECLARE FUNCTION TSA(L,B,H) INPUT "Enter length, breath and height";L,B,H PRINT "Total surface area is" ; TSA(L,B,H) END FUNCTION TSA(L,B,H) TSA = 2 * (L * B + L * H + B * H) END FUNCTION
Q6.Write a program to calculate area of circle using function procedure? Hint: A=πr2]
DECLARE FUNCTION AREA(R) INPUT "Enter radius";R PRINT "Area of circle = "; AREA(R) END FUNCTION AREA(R) A = 3.14*R*R AREA = A END FUNCTION
Q7. Create a user defined function to area and circumference of a circle. Hint: A=πr2 and C=2πr
DECLARE FUNCTION AREA(R) DECLARE FUNCTION CIRCUM(R) INPUT "Enter radius";R PRINT "Area =";AREA(R) PRINT "Circumferenc =";CIRCUM(R) END FUNCTION AREA(R) A = 3.14 * R * R AREA = A END FUNCTION FUNCTION CIRCUM(R) C = 2 * 3.14 * R CIRCUM = C END FUNCTION
Q8. Create user defined function to accept degree in Celsius and convert it into Fahrenheit. [F=(9/5)*C+32]
DECLARE FUNCTION TEMP(C) INPUT "Enter temp. in celsius : ";C PRINT "Temp. in Fahrenheit =";TEMP(C) END FUNCTION TEMP(C) F = (9 / 5) * C + 32 TEMP = F END FUNCTION
Q9. Create user defined function to find square of a number.
DECLARE FUNCTION SQUARE(N) INPUT "Enter number : ";N PRINT "Square of given number =";SQUARE(N) END FUNCTION SQUARE(N) S = N * N SQUARE = S END FUNCTION
Q10. Create user defined function to check whether the supplied number is positive, negative or zero.
DECLARE FUNCTION TEST(N) INPUT "Enter any number : " , N A = TEST(N) IF A>0 THEN PRINT "Positive" ELSEIF N<0 THEN PRINT "Negative" ELSE PRINT "Zero" END IF END FUNCTION TEST(N) TEST = N END FUNCTION
Q11. Create user defined function to find sum of a cube of two numbers.
DECLARE FUNCTION CUBESUM(A,B) INPUT "Enter first number: ";A INPUT "Enter second number:";B PRINT "Sum of cube of two numbers =";CUBESUM(A,B) END FUNCTION CUBESUM(A,B) S = A * A * A + B * B * B CUBESUM = S END FUNCTION
Q12. Create user defined function to check whether the input number is divisible by 5 or not.
DECLARE FUNCTION TEST(N) INPUT "Enter any number : ";N ANS = TEST(N) IF ANS=0 THEN PRINT "divisible by 5" ELSE PRINT "Not divisible by 5" END IF END FUNCTION TEST(N) R = N MOD 5 TEST = R END FUNCTION
Q13. Create user defined function to check whether the number is “even” or “odd”. [SEE 2075 Supp.]
DECLARE FUNCTION TEST(N) INPUT "Enter any number : " , N A = TEST(N) IF A= 0 THEN PRINT "Even number" ELSE PRINT "Odd number" END IF END FUNCTION TEST(N) R = N MOD 2 TEST = R END FUNCTION
Q14. A function returns the square of a number using that function, print the square of all the numbers from 2 to 10.
DECLARE FUNCTION SQUARE(N) CLS FOR K = 2 TO 10 S = SQUARE(K) PRINT "Square of "; K ; "=" ; S NEXT K END FUNCTION SQUARE(N) SQ = N * N SQAURE=SQ END FUNCTION
Q15. A function returns the cube of a number using that function, print the cube of all the numbers from 10 to 20.
DECLARE FUNCTION CUBE(N) FOR K = 10 TO 20 C = CUBE(K) PRINT "Cube "; K ; "=" ; C NEXT K END FUNCTION CUBE(N) CU = N * N * N CUBE = CU END FUNCTION
Q16. Create user defined function to find factorial of the input number.
DECLARE FUNCTION FACT(N) INPUT "Enter any number : " , N PRINT "Factorial = " ; FACT(N) END FUNCTION FACT(N) F = 1 FOR I = N TO 1 STEP-1 F = F * I NEXT I FACT = F END FUNCTION
Q17. Create user defined function to check input number is prime or composite.
DECLARE FUNCTION TEST(N) INPUT "Enter any number : " , N A = TEST(N) IF A= 2 THEN PRINT "Prime number" ELSE PRINT "Composite number" END IF END FUNCTION TEST(N) C = 0 FOR I = 1 TO N IF N MOD I = 0 THEN C = C + 1 END IF NEXT I TEST = C END FUNCTION
Q18. A user defined function which returns the cube of a number, using that function, find the sum of the cube of the digits of input number.
DECLARE FUNCTION CUBE(N) INPUT "Enter number "; N PRINT "Cube of digits = "; CUBE(N) END FUNCTION CUBE(N) WHILE N<>0 R = N MOD 10 S = S + R * R * R N = N \ 10 WEND CUBE = S END FUNCTION
Q19. Create a user defined function to check whether the given number is Palindrome or not.
DECLARE FUNCTION PAL(N) INPUT "Enter number "; N IF N = PAL(N) THEN PRINT "Palindrome Nuumber" ELSE PRINT "Not Palindrome number" END IF END FUNCTION PAL(N) WHILE N<>0 R = N MOD 10 S = S * 10 + R N = N \ 10 WEND PAL = S END FUNCTION
Q20. Create user defined function to check whether the input number is Armstrong or not.
DECLARE FUNCTION ARM(N) INPUT "Enter number "; N IF N = ARM(N) THEN PRINT "Armstrong Nuumber" ELSE PRINT "Not Armstring number" END IF END FUNCTION ARM(N) WHILE N<>0 R = N MOD 10 S = S + R * R * R N = N \ 10 WEND ARM = S END FUNCTION
Q21. Create a user defined function to reverse the input string. [SEE 2074 Supp.]
DECLARE FUNCTION REV$(W$) INPUT "Enter string : "; W$ A$ = REV$(W$) PRINT "Reverse of given string is ";A$ END FUNCTION REV$(W$) FOR I = LEN(W$) TO 1 STEP-1 C$ = MID$(W$, I, 1) B$ = B$ + C$ NEXT I REV$ = B$ END FUNCTION
Q22. Write a program to input a string in the main module and then check whether the input string is a palindrome or not, using function procedure.
DECLARE FUNCTION TEST$(W$) INPUT "Enter string " ; W$ A$ = TEST$(W$) IF A$=W$ THEN PRINT "Palindrome string" ELSE PRINT " Not palindrome String" END IF END FUNCTION TEST$(W$) FOR I = LEN(W$) TO 1 STEP-1 C$ = C$ + MID$(W$, I, 1) NEXT I TEST$ = C$ END FUNCTION
Q23. Create user defined function to count number of consonants in input string.
DECLARE FUNCTION CCOUNT(W$) INPUT " Enter word : "; W$ A = CCOUNT(W$) PRINT "Total consonants = " ; A END FUNCTION CCOUNT(W$) C = 0 FOR I = 1 TO LEN(W$) W$ = UCASE$(W$) C$ = MID$(W$, I, 1) SELECT CASE C$ CASE "A","E","I","O","U" CASE ELSE C = C + 1 END SELECT NEXT I CCOUNT = C END FUNCTION
Q24. Write a program to input a word in the main module and count the total number of vowel characters present in the word using FUNCTION...END FUNCTION. [SEE 2073-R]
DECLARE FUNCTION VCOUNT(W$) INPUT " Enter word : "; W$ A = VCOUNT(W$) PRINT "Total vowels : " ; A END FUNCTION VCOUNT(W$) C = 0 FOR I = 1 TO LEN(W$) W$ = UCASE$(W$) C$ = MID$(W$, I, 1) SELECT CASE C$ CASE "A","E","I","O","U" C = C + 1 END SELECT NEXT I VCOUNT = C END FUNCTION
Q25. Write a program using FUNCTION... END FUNCTION to count the number of words in a sentence.[SEE 2074 R]
DECLARE FUNCTION WCOUNT(S$) INPUT "Enter sentence : " ; S$ A = WCOUNT(S$) PRINT "Total words in given sentence :";A END FUNCTION WCOUNT(S$) C = 1 FOR I = 1 TO LEN(S$) IF MID$(S$,I,1)= " " THEN C = C + 1 END IF NEXT I WCOUNT = C END FUNCTION
1. Write a program in QBASIC that asks length, breadth of a room and calculate its area and perimeter. Create a user defined function to calculate area and sub program to calculate perimeter. [Hint: [Area=LxB], [p=2(L+B]] SEE 2078 (2022)
D D I P C END
1. Write a program to store Roll No, Name, Class and Address of any five students.
OPEN "ABC.TXT" FOR OUTPUT AS #1 FOR I = 1 TO 5 INPUT "Enter roll, name,class and address : ",R,N$,C,A$ WRITE #1,R,N$,C,A$ NEXT I CLOSE #1 END
2. A data file "hospital.dat" contains information about some staffs. The records are staffname,post,shift,age. Write a program to insert 15 records to data file.
OPEN "hospital.dat" FOR APPEND AS #1 FOR I = 1 TO 15 Input "Enter staff name, post, shift and age : ",N$,P$,S$,AGE WRITE #1,N$,P$,S$,AGE NEXT I CLOSE #1 END
3. Write a program that asks the item's name, rate and quantity and stores into "sales.txt". The user can supply 10 records in each execution of the program.
OPEN "sales.txt" FOR OUTPUT AS #1 FOR I = 1 TO 10 INPUT "Enter item's name, rate and quantity ",name$,rate,quantity WRITE #1,name$,rate,quantity NEXT I CLOSE #1 END
4. Write a program to create a sequential data file named "Employee.dat" to store Name, Post, Address, and Salary for the number of employees. The program should terminate on the user's choice.
OPEN "employee.dat" FOR OUTPUT AS #1 DO INPUT "Enter employee name, post , address and salary ",name$,post$,adr$,sal WRITE #1,name$,post$,adr$,sal INPUT " Want to add more records"; choice$ LOOP WHILE UCASE$(choice$)="Y" CLOSE #1 END
5. Write a program to create a sequential data file "salary.dat" to store programmer's Name, salary and post according to the need of the user.
Open "salary.dat" for output as #1 Do Input "Enter employee name, post and salary ",name$,post$,salary WRITE #1,name$,post$,salary input " Want to add more records"; choice$ Loop while(ucase$(choice$))="Y" Close #1 End
6. Write a program to ask students' name, class and marks secured in three subjects. Store the data in a sequential data file "RESULT.TXT" along with the total marks. Make a provision to ask users to enter other records.
Open "result.txt" for output as #1 top: Input "Enter name, class and marks of three subjects ",n$,c,s1,s2,s3 total = S1 + S2 + S3 WRITE #1,n$,c,s1,s2,s3,total Input "Want to add another records ",choice$ If ucase$(choice$)="Y" then goto top Close #1 End
7. Write a program to input the Name, Address and Post of some employees and store them in a file named "emp.txt" along with a serial number for each record.
Open "emp.txt" for output as #1 top: sn = 1 Input "Enter name, address and post ",n$,a$,p$ write #1,sn,n$,a$,p$ sn = sn + 1 Input "Want to add another records ",choice$ If ucase$(choice$)="Y" then goto top Close #1 End
8.Write a program that asks students' name, roll and class and stores into "class.txt" only those records who are studying in class 10. Users can supply the records as per his/her need.
Open "class.txt" for output as #1 top: Input "Enter name,roll and class ",n$,roll,class If class=10 then write #1,n$,roll, class End If Input "Want to add another records ",choice$ If ucase$(choice$)="Y" then goto top Close #1 End
9.Write a program that asks student's names and marks in English, Math and Computer. Store only those records that are passed in all the subjects into the data file named "pass.dat". The user will be asked whether to input another record or not. [Note: pass mark for all the subjects is 40.]
Open "pass.txt" for output as #1 top: Input "Enter name, marks in Eng,Math and Computer ",n$,e,m,c If e>=40 and m>=40 and c>=40 then write #1,n$,e,m,c End If Input "Want to add another records ",choice$ If ucase$(choice$)="Y" then goto top Close #1 End
10. A sequential data file called "record.txt" contains NAME,AGE,CITY and TELEPHONE fields. Write a program to display all the contents of that data file.
Open "record.txt" for input as #1 Print "Name","Age","City","Telephone" while not eof(1) Input #1,n$,age,city$,tel$ Print n$,age,city$,tel$ Wend End
11. A sequential data file "EMP.TXT" contains name, post and salary fields of information about employees. Write a program to display all the information of the employee along with the tax amount also(tax is 15% of salary).
Open "emp.txt" for input as #1 Print "Name","Post","Salary","Tax" While not eof(1) Input #1,name$,post$,salary Tax = (SALARY * 15)/100 Print name$,post$,salary,tax Wend End
12. A data file "STAFF.dat" has stored records of few employees with EmpID, FirstName, LastName, Post and Salary. Write a program to display all the records of the employees whose salary is more than 40,000.
Open "staff.dat" for input as #1 Do while not eof(1) Input #1, empid, f$, l$, p$, salary If salary>40000 then print empid, f$, l$, p$, salary End if Loop Close #1 End
13. A sequential data file called "SEE.TXT" has stored data under the field heading Symbol No, Name, English, Nepali, Maths and Computer. Write a program to display all the information of those students whose marks in Computer is more than 80.
Open "SEE.TXT" for input as #1 Do while not eof(1) Input #1, symbolnum, NAME$, eng, nep, maths,comp If comp>80 then print symbolnum; NAME$; eng; nep; maths; comp End if Loop Close #1 End
14. A sequential data file called "Record.txt" has stored data under the file heading Roll No, Name, Gender, English, Nepali, Maths and Computer. Write a program to display all the information of those students whose gender are "F" and obtained marks in the computer is more than 90.
Open "Record.txt" for input as #1 print "Roll"; "Name"; "Gender"; "English"; "Maths"; "Computer" While not eof(1) Input #1, Roll, Name$, Gen$, Eng, Maths, Comp If UCASE$(Gen$)="F" and Comp>90 then Print Roll; Name$; Gen$; Eng; Maths; Comp End if Wend Close #1 End
15. A sequential data file "class.txt" has several records with the field's students' name, roll and class. Write a program that reads all the records and displays only those records whose roll number is less than 10.
Open "class.txt" for input as #1 Print "Roll", "Name", "Class" While not eof(1) Input #1, Roll, Name$, class If Roll < 10 then Print Roll, Name$, class End if Wend Close #1 End
16. A sequential data file "pass.dat" has several records having fields student's name and marks in English, Math and Computer. Write a program that reads all the records and displays only those records whose name starts with 'A' and also counts the total number of records stored in the file.
Open "pass.dat" for input as #1 While not eof(1) Input #1, Name$, eng,math,comp C = C + 1 p$ = ucase$(left$(NAME$, 1)) If p$="A" then Print Name$,eng,math,comp End if Wend Print "total number of record is" ; C Close #1 End
17. A doctor keeps name, age and sex of his patients in a sequential data file "PATIENT.DAT". Write a QBASIC program to display only male patient records also count the total number of patients in a file.
OPEN "PATIENT.DAT" FOR INPUT AS #1 PRINT "Name","Age","Sex" WHILE NOT EOF(1) INPUT #1, NAME$,AGE,SEX$ C = C + 1 IF UCASE$(SEX$)="MALE" THEN PRINT NAME$,AGE,SEX END IF WEND PRINT "Total number of patients ",C END
18. Write a program which reads records from the file "RESULT.DAT" having the fields name, and marks of three different subjects and display only those records whose percentage is greater than 60 and less than 80. Also count the total number of records present in that data file.
OPEN "RESULT.DAT" FOR INPUT AS #1 WHILE NOT EOF(1) INPUT #1,NAME$,SUB1,SUB2,SUB3 C = C + 1 PER = (SUB1 + SUB2 + SUB3) / 3 IF PER>60 AND PER<80 THEN PRINT NAME$,PER END IF WEND PRINT "TOTAL NUMBER OF RECORD IS " ; C Close #1 End
19. A sequential data file "staff.txt" has several records related to name, post , date of birth (mm-dd-yyyy) and salary. Write a program to display the records of those people who were born between the years 2000 to 2020 as well as count and display the total number of records.
Open "staff.txt" for input as #1 While not eof (1) Input #1, n$, p$, dob$, sal c = c + 1 a$ = Right$(DOB$, 4) d = val(a$) If d>=2000 and d<=2020 then Print n$,p$, dob$, sal End if Wend close #1 Print "total records= "; c End
20. A sequential data file "class.txt" has several records having fields student's name, class and roll number. Write a program to copy all the records from "class.txt" into another new file "stduent.txt".
Open "class.txt" for Input as #1 Open "student.txt" for Output as #2 While not eof(1) Input #1, name$, class, roll Write #2, name$, class, roll Wend Close #1,#2 Print "Data Copied Successfully!" End
21. A sequential data file "records.txt" has several records having fields Employee's Name, Post and Salary. Write a program to increase the salary of all employees by 10%.
Open "records.txt" for input as #1 Open "temp.txt" for output as #2 while not eof(1) Input #1, name$,post$,salary SALARY = SALARY + (SALARY * 10) / 100 Write #2, name$,post$,salary Wend Close kill "records.txt" name "temp.txt" as "records.txt" Print "Salary has been updated by 10%" End
22. Write a program to read the data from "INFO.TXT" that contains student name, class, roll no , DOB(MM-DD-YYYY) and address. Write/copy all the data whose DOB is current month to the data file "INFO.TXT".
Open "info.txt" for input as #1 Open "temp.txt" for output as #2 While not eof (1) Input #1, n$, c, r, dob$, A$ If left$(dob$,2)=left$(date$,2) then Write #2, n$,c,r,dob$,a$ End if Wend close Kill "info.txt" Name "temp.txt" as "info.txt" Print "Data copied successfully!" End
23. A sequential data file “RECORDS.TXT” contains name, address, phone number and DOB of few people. Write a program to count the total number of records stored in a data file and display the records of only those people whose name is five character long.
Open "RECORDS.TXT" for input as #1 c=0 While not eof(1) Input #1, name$ , address$ , phone$ , DOB$ c = c + 1 a = len(NAME$) If a=5 then print name$ , address$ , phone$ , DOB$ End if Wend Close #1 Print "Total records=";c End
24. Write a program to read product name, quantity, grade and price of 20 products from “product.dat”. Then count only those records where price is greater than 200 and grade="A".
Open "product.dat" for input as #1 c=0 while not eof(1) Input #1,name$,quantity,grade$,price If price>200 and grade$="A" then c = c + 1 End if Wend Close #1 Print "Total number of products having price greater than 200 and grade A =";c End
25. "score.dat" is a sequential data file which contains marks scored by students in a test . WAP to count and display all the students who has passed in all the subjects along with the calculation of their total marks. Assume field structure of data stored in the file is regno(registration number), English, Nepali, Science, Compulsory mathematics)
open "score.dat" FOR INPUT AS #1 WHILE NOT EOF(1) INPUT #1,REGNO$,ENG,NEP,SCI,MATHS IF ENG>=40 AND NEP>=40 AND SCI>=40 AND MATHS>=40 THEN PRINT REGNO$,ENG,NEP,SCI,MATHS C = C + 1 END IF WEND CLOSE #1 PRINT "No. of Passed Students : " ; C END
Bonus programs on file handling
(1) To create data file "STUDENT.DAT" to store Name, Roll No, Class and Section of students.
OPEN "STUDENT.DAT" FOR OUTPUT AS #1 DO INPUT "Enter student name, roll, class and section ", NAME$,ROLL,CLASS,SEC$ WRITE #1,NAME$,ROLL,CLASS,SEC$ INPUT " Want to add more records ", CHOICE$ LOOP WHILE UCASE$(CHOICE$)="Y" CLOSE #1 END
(2) To store Name, Address , Age and Salary of employees in data file "STAFF.DAT"
OPEN "STAFF.DAT" FOR OUTPUT AS #1 DO INPUT "Enter name, address , age and salary: ", NAME$,ADR$,AGE,SAL WRITE #1,NAME$,ADR$,AGE,SAL INPUT " Want to add more records ", CHOICE$ LOOP WHILE UCASE$(CHOICE$)="Y" CLOSE #1 END
(3) To add records in data file created in (1)
OPEN "STUDENT.DAT" FOR APPEND AS #1 DO INPUT "Enter student name, roll, class and section ", NAME$,ROLL,CLASS,SEC$ WRITE #1,NAME$,ROLL,CLASS,SEC$ INPUT " Want to add more records ", CHOICE$ LOOP WHILE UCASE$(CHOICE$)="Y" CLOSE #1 END
(4) To display all records of data file “STUDENT.DAT” created in (1)
OPEN "STUDENT.DAT" FOR INPUT AS #1 PRINT "Name","Roll No","Class","Section" WHILE NOT EOF(1) INPUT #1, NAME$,ROLL,CLASS,SEC$ PRINT NAME$,ROLL,CLASS,SEC$ WEND CLOSE #1 END
(5) To search record of studetent on the basis of roll
OPEN "STUDENT.DAT" FOR INPUT AS #1 INPUT "ENTER ROLL : ";R FLAG = 0 WHILE NOT EOF(1) INPUT #1,NAME$,ROLL,CLASS,SEC$ IF ROLL=R THEN PRINT NAME$,ROLL,CLASS,SEC$ GOTO AA ELSE FLAG = 1 END IF WEND IF FLAG=1 THEN PRINT "RECORD NOT FOUND" END IF AA: CLOSE #1 END
(6) To display those records whose salary is greater than 15000 from the data file “STAFF.DAT” created in (2)
OPEN "STAFF.DAT" FOR INPUT AS #1 PRINT "Name","Address","Age","Salary" WHILE NOT EOF(1) INPUT #1,NAME$,ADR$,AGE,SAL IF SAL>15000 THEN PRINT NAME$,ADR$,AGE,SAL END IF WEND CLOSE #1 END
(7) To delete the records whose age is greater than 60 from the data file "STAFF.DAT"
OPEN "STAFF.DAT" FOR INPUT AS #1 OPEN "TEMP.DAT" FOR OUTPUT AS #2 WHILE NOT EOF(1) INPUT #1,NAME$,ADR$,AGE,SAL IF AGE<60 THEN WRITE #2,NAME$,ADR$,AGE,SAL END IF WEND CLOSE #1,#2 KILL "STAFF.DAT" NAME "TEMP.DAT" AS "STAFF.DAT" PRINT "Employees having age greather than 60 deleted." END
(8) Write a menu based program to append, display and delete records from the data file.
DECLARE SUB MENU() DECLARE SUB ADDREC() DECLARE SUB DISREC() DECLARE SUB DELREC() CALL MENU INPUT CH SELECT CASE CH CASE 1 CALL ADDREC CASE 2 CALL DISREC CASE 3 CALL DELREC CASE ELSE PRINT "Bye Bye" END SELECT END SUB ADDREC OPEN "STUDENT.DAT" FOR APPEND AS #1 DO INPUT "Enter student name, roll, class and section ", NAME$,ROLL,CLASS,SEC$ WRITE #1,NAME$,ROLL,CLASS,SEC$ INPUT " Want to add more records ", CHOICE$ LOOP WHILE UCASE$(CHOICE$)="Y" CLOSE #1 END SUB SUB DISREC OPEN "STUDENT.DAT" FOR INPUT AS #1 PRINT "Name","Roll No","Class","Section" WHILE NOT EOF(1) INPUT #1, NAME$,ROLL,CLASS,SEC$ PRINT NAME$,ROLL,CLASS,SEC$ WEND CLOSE #1 END SUB SUB DELREC OPEN "STUDENT.DAT" FOR INPUT AS #1 OPEN "TEMP.DAT" FOR OUTPUT AS #2 INPUT "Enter name to delete ",DN$ FLAG = 0 WHILE NOT EOF(1) INPUT #1, NAME$,ROLL,CLASS,SEC$ IF UCASE$(DN$)<>UCASE$(NAME$) THEN WRITE #2,NAME$,ROLL,CLASS,SEC$ ELSE FLAG = 1 PRINT "Data deleted" END IF WEND IF FLAG=0 THEN PRINT "Record not found" END IF CLOSE #1,#2 KILL "STUDENT.DAT" NAME "TEMP.DAT" AS "STUDENT.DAT" END SUB SUB MENU PRINT "1. To add records " PRINT "2. To display record" PRINT "3. To delete record" PRINT "Enter your choice" END SUB
1. Write a program to create a sequential data file “salary.dat” to store programmer’s name, salary and post according to the need of the user.
OPEN "salary.dat" FOR OUTPUT AS #1 DO INPUT "Enter name,salary and post ",name$,salary,post$ WRITE #1,name$,salary,post$ INPUT "More records(Y-Yes/N-No) ";ch$ loop while ucase$(ch$)="Y" CLOSE #1 END
2. Write a program to create a sequential data file named "employ.dat" to store Name, Post, Address and Salary for the number of employees. The program should terminate on user's choice.
OPEN "employ.dat" FOR OUTPUT AS #1 DO INPUT "Enter name ,post , address and salary ";name$,post$,address$,salary WRITE #1,name$,salary,post$ INPUT "more records (Y-Yes/n-No) ";ch$ LOOP WHILE UCASE$(ch$)="Y" CLOSE #1 END
3. A sequential data file called "Record.txt" has stored data under the field heading Roll No, Name, Gender, English, Nepali, Maths and Computer. Write a program in QBASIC to display all the information of those students whose gender are “F” and obtained marks in computer is more than 90.
OPEN "RECORD.DAT" FOR INPUT AS #1 WHILE NOT EOF(1) INPUT #1,ROLL,NAME$,GEN$,ENG,NEP,MATHS,COMPUTER IF GEN$="F" AND COMPUTER>90 THEN PRINT ROLL,NAME$,GEN$,ENG,NEP,MATHS,COMPUTER END IF WEND CLOSE #1 END
4. A sequential data file called "Record.txt" has stored data under the field heading Roll No., Name, Gender, English, Nepali and Math’s and Computer. Write a program in QBASIC to display all the information of female students who pass in all subjects. [Note: Pass nark in all subjects is 40].
END
5. A sequential data file "emp.dat" contains employee's name, address, gender and salary. Write a program in QBASIC to display all the information of employees whose salary is more than Rs.20,000.
END
6. A sequential data file "class.dat" has several records with fields student's name, roll no, address and class. Write a program in QBASIC that reads all the records and displays only those records whose class is 10.
END
7. A sequential data file "abc.txt" has several records with fields student's name, roll no , address and class. Write a program in QBASIC that reads all the records and displays only those records whose address is "Pokhara".
END
8. A sequential data file "abc.txt" has several records with fields student's name, roll no , address, fee and class. Write a program in QBASIC that reads all the records and displays only those records whose fee is greater than 5000.
END
9. A sequential data file "student.txt" consists of Book's name, Author's name and Price of book. Write a program in QBASIC to count and display the total number of records present in the file.
END
10. A sequential data file called "record.txt" has stored data under the field heading Roll no, Name, Gender , English, Nepali, Maths and computer. Write a QBASIC program to display all the female student who passed in all subjects. [Note: Assume pass mark in each subject is 40.]
END
C Lab sheet 1 contains twenty programs(1 to 20),
Where as C Lab Sheet 2 contains twenty programs(21 to 40).

i. Input / Output Functions
Simple Mathematical Program using C

  • C program to display "Hello World"
  • C Program to find sum of two numbers
  • C Program to find the square of given number || Cube of given number
  • C Program to find simple interest
  • C Program to convert temperature from Centigrade (C) to Fahrenheit (F). Hint: f=1.8c+32
  • C program to find area and circumference of a circle.
  • C program that ask length and breath of a room. Find area and perimeter of a room.
  • C Program to find remainder of two number
  • C Program to convert minutes into hour and minutes || Converts Seconds into Hour, Minute and Seconds
  • C program to input number of days and convert it into years, months and days.
  • ii. Selection Statement
    Conditional Program without Loop using C

  • C Program to check whether the given number is divisible by 7 or not.
  • C Program to test whether the given number is even or odd.
  • C Program to input mark of computer science and check whether the student is pass or fail where the pass mark is 40.
  • C Program that asks two numbers and displays the smaller one || greater one.
  • C program to asks three numbers and display the greatest || smallest among them. 
  • C program to find the greatest || smallest number among four numbers.
  • C Program to input a number and check whether it is positive, negative, or zero.
  • C Program to find the middle number among three different numbers.
  • C program that inputs cost price and selling price and determine whether there is profit or loss.
  • C program to find the commission amount in the basis of sales amount as per the following conditions:
    Sales Amount    Commission
    0-1000                   5%-
    1001-2000             10%
    >2000                     12%
  • iii. Looping Statement
    Looping Program using C

  • C program to display 5,10,15,.....50
  • #include <stdio.h> int main() { int i; for(i=5;i<=50;i=i+5) { printf("%d\n",i); } return 0; }
  • C program to display 5,10,15,..... up to 50th terms
  • #include <stdio.h> int main() { int i,a=5; for(i=0;i<50;i++) { printf("%d\n",a); a=a+5; } return 0; }
  • C program to display 1,2,4,8,16,..... up to 10th terms
  • #include <stdio.h> int main() { int i,a=1; for(i=0;i<10;i++) { printf("%d\n",a); a=a*2; } return 0; }
  • C program to display 1,2,4,7,11,..... up to 10th terms
  • #include <stdio.h> int main() { int i,a=1; for(i=0;i<10;i++) { a=a+i; printf("%d\n",a); } return 0; }
  • C program to display 2,8,18,32,..... up to 10th terms
  • #include <stdio.h> int main() { int i,a; for(i=1;i<=10;i++) { a=i*i*2; printf("%d\n",a); } return 0; }
  • C program to display 999,728,511,..... up to 10th terms
  • #include <stdio.h> int main() { int i,ans; for(i=10;i>=1;i--) { ans=i*i*i-1; printf("%d\n",ans); } return 0; }
  • C program to display 100,98,94,88,80,..... up to 10th terms
  • #include <stdio.h> int main() { int i,ans=100,b=2; for(i=0;i<10;i++) { printf("%d\n",ans); ans=ans-b; b=b+2; } return 0; }
  • C program to display 7,22,11,34,17,..... up to 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
    #include <stdio.h> int main() { int a=7,r,i; for(i=0;i<10;i++) { printf("%d\n",a); r=a%2; if(r==0) a=a/2; else a=a*3+1; } return 0; }
  • C program to display 0,1,1,2,3,5,8,13,..... up to 50th 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.
    #include <stdio.h> int main() { int a=0,b=1,c,i; for(i=0;i<10;i++) { printf("%d\n",a); c=a+b; a=b; b=c; } return 0; }
  • C program to display the series with their sum 1,2,3,4,..... up to 10th terms
  • #include <stdio.h> int main() { int i,sum=0; for(i=1;i<=10;i++) { printf("%d\t",i); sum=sum+i; } printf("Sum of series from 1-10 is %d",sum); return 0; }

    iv. Looping with Condition
    Looping program with condition Program using C

  • C program to display all even numbers from 2 to 20 and find their sum.
  • #include <stdio.h> int main() { int i,sum; for(i=2;i<=20;i=i+2) { printf("%d\n",i); sum=sum+i; } printf("Sum of even numbers from 2 to 20 is %d " , sum); return 0; }
  • C program to find the factorial of the given number.
    The product of a given positive integer multiplied by all lesser positive integers: The quantity five factorial (5!) = 5x4 x 3 x 2 x 1 = 120.
  • #include <stdio.h> int main() { int num,i,f=1; printf("Enter any number : "); scanf("%d",&num); for(i=num;i>=1;i--) { f=f*i; } printf("\n Factorial of %d is %d",num,f); return 0; }
  • C program to display factors of a supplied number.
  • #include <stdio.h> int main() { int num,i; printf(" Enter any number : "); scanf("%d",&num); printf("Factors of %d are : ",num); for(i=1;i<=num;i++) { if(num%i==0) printf(" %d\n ",i); } return 0; }
  • C program to check whether the supplied number is prime or composite.
  • A prime number is an integer which has only two factors i.e. one and itself. For example: 17 is prime number because it is exactly divisible by 1 and 17 only.
    #include <stdio.h> int main() { int num,i,count=0; printf("Enter a number"); scanf("%d",&num); for( i=1;i<=num;i++) { if(num%i==0) count++; } if(count==2) printf("%d is prime number",num); else printf("%d is composite number",num); return 0; }
  • C program to display all prime numbers from 1 to 100.
  • #include <stdio.h> int main() { int num,i,count; for(num=1;num<=100;num++) { count=0; for(i=1;i<=num;i++) { if(num%i==0) count++; } if(count==2) printf(" %d\n ",num); } return 0; }
  • C program that asks a multi-digit number and calculates the sum of its individual digits.
  • #include <stdio.h> int main() { int num,r,sum=0; printf(" Enter any number "); scanf("%d",&num); while(num!=0) { r=num%10; sum=sum+r; num=num/10; } printf("Sum of individual digits is %d",sum); return 0; }
  • C program to display the reverse of a supplied number.
  • #include <stdio.h> int main() { int num,a,r,sum=0; printf("Enter any number "); scanf("%d",&num); a=num; while(num!=0) { r=num%10; sum=sum*10+r; num=num/10; } printf("Reverse of %d is %d",a,sum); return 0; }
  • C program to test whether the supplied number is Palindrome number or not.
  • Hint: A word, phrase, or sequence that reads the same backwards as forwards is called Palindrome. Example : 121 , 101 etc
    #include <stdio.h> int main() { int num,a,r,sum=0; printf("Enter any number "); scanf("%d",&num); a=num; while(num!=0) { r=num%10; sum=sum*10+r; num=num/10; } if(a==sum) printf(" %d is Palindrome Number",a); else printf(" %d is not Palindrome Number",a); return 0; }
  • C program to check whether the given number is Armstrong or not.
  • An Armstrong number is a number which equal to the sum of the cubes of its individual digits. For example: 153 is an Armstrong number as- 153=(1)3+(5)3+(3)3=1+125+27=153
    #include <stdio.h> int main() { int num,a,r,sum=0; printf("Enter any number"); scanf("%d",&num); a=num; while(num!=0) { r=num%10; sum=sum+r*r*r; num=num/10; } if(a==sum) printf("%d is Armstrong number",a); else printf("%d is not Armstrong number",a); return 0; }
  • C program to display:
    1
    2 2
    3 3 3
    4 4 4 4
    5 5 5 5 5
  • #include <stdio.h> int main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf(" %d ",i); } printf("\n"); } return 0; }

    1. Write a program in C language that asks a number and check whether it is odd or even.

    2. Write a program in C language to display the series with their sum 1, 2, 3, 4 ... up to 10th terms.
    3. Write a program in C language to input a number and then check whether the number is positive or negative or zero.[SEE 2078]
    4. Write a program in C language to display the series 1,1,2,3,5,8 ... up to 10th terms.
    5. Write a program in ‘C’ language to input two numbers and find greatest among two number.
    6. Write a program in C language to convert days into respective years, months and days.
    7. Write a program in C language to display first 10 odd numbers. [SEE 2078]
    8. Write C program to display the series : 1,2,3,6,11,20,37 .... up to 10th terms
    #include<stdio.h> int main() { int a=1,b=2,c=3,d=0,i; for(i=0;i<=10;i++) { printf(" %d ",a); d=a+b+c; a=b; b=c; c=d; } return (0); }
    DBMS (MS ACCESS)
    A. Create a database "school". Create a table "student" with following fields(Design View).
    Field Name Data type
    StudentID Autonumber
    StudentName Text
    ClassNumber
    English Number
    Nepali Number
    Computer Number
    1. Make StudentID as primary key.
    2. Set Caption "Eng Mark" , "Nep Mark" and "Comp Mark".
    3. Set default value 10 for field class .
    4. Set the validation rule: >=0 AND <=100
    5. Set the validation text : Enter the marks between 0 to 100.
    6. Enter any five data in table "student".[Datasheet View]
    B. Create following queries:
    1. Display all records of table "student".
    2. Display all records in descending order of marks of Computer.
    3. Display all those records, where the mark in "English" are greater than or equal to 80.
    4. Display all those records where marks in all subjects are greater than or equal to 80.
    5. Display all those records where student name start with "A" or "S".
    6. Display all the records for those students whose marks in "Computer" are between 60 to 80.
    7. Display records by student ID.
    8. Delete the records of "abc" student.
    9. Update the marks of English by 10 to all students.
    10. Calculate total by adding marks of Eng, Nepali and Computer.
    11. Calculate Average by dividing total by 3.
    12. Make remarks if average marks is greater than or equal to 40 then "Pass" , otherwise "Fail".
    C. Create form of table student using form Wizard.
    D. Add two more records using Form crated above.
    E. Make a report of table "student".
    F. Make a report of query "q7 " created above.