0% found this document useful (0 votes)
70 views25 pages

Computer 10

Modular programming involves dividing a program into logical and manageable modules. It has advantages like easier debugging, code reusability, and improved readability. QBASIC supports modular programming using sub-procedures and function-procedures, which can take parameters and return values.

Uploaded by

Bipin Bhatta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views25 pages

Computer 10

Modular programming involves dividing a program into logical and manageable modules. It has advantages like easier debugging, code reusability, and improved readability. QBASIC supports modular programming using sub-procedures and function-procedures, which can take parameters and return values.

Uploaded by

Bipin Bhatta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Modular Programming

Modular programming is technique used to divide our program into


many small logical, manageable and functional modules or blocks.
Every modular program has one main module and have many sub
modules.
 Advantages of Modular programming:
-Many programmers can write different program modules
independently.
-The debugging of the program becomes easier and faster.
-The same procedure can be used in different places, which reduces
the program codes.
-It improves the readability of a program.
-It is easy to design code.
Modular programming in QBASIC
 We can handle modular programming in QBASIC by two ways:

Sub- Procedure Function- Procedure


Syntax: Syntax:
Declare SUB Declare FUNCTION
name_sub_procedure(parameter) name_function_procedure(Parameters)
{ body of main module {
CALL name_sub_procedure(parameter) Body of main module
} PRINT name_Sub_procedure(parameter)
End }
SUB name_Sub_Procedure(parameters) End
{ FUNCTION name_Sub_procedure(parameters)
Body of sub_procedure orsub_module {
} Body of sub_procedure or sub_module
END SUB }
END FUNCTION
WAP in qbasic to calculate area of rectangle
Using Sub Module Using function
Declare sub area(l!,b!) Declare function area(l,b)
Input "Enter length"; l Input "Enter length & breadth"; l,b
Input "Enter breadth"; b Print "Area of rectangle is:"; area(l, b)
Call area(l, b) End
End Function area (l, b)
Sub area (l, b) A=l*b
A=l*b area = A
Print "Area of rectangle is:"; A End Function
End Sub OR
Declare function area(l,b)
Input "Enter length and breadth"; l,b
X=area(l, b)
Print "Area of rectangle is:"; X
End
Function area (l, b)
A=l*b
area = A
End Function
WAP in qbasic to calculate volume of cylinder . (V=∏r2h )

Using Sub Procedure Using Function

Declare sub volume(h,r) Declare function volume(h,r)


Input "Enter height and radius"; Input "Enter height and radius";
h, r h, r
Call volume(h, r) X = volume(h, r)
End Print "Area of rectangle is:"; X
Sub volume (r, h) End
v = (22 / 7) * r ^ 2 * h Function volume (r, h)
Print "Area of rectangle is:"; v v = (22 / 7) * r ^ 2 * h
End Sub volume = v
End Function
WAP in qbasic to find input number is even or odd

Using sub procedure Using Function


Declare sub check(N) Declare function check$(N)
Input "Enter any number"; N Input "Enter any number"; N
Call check(N) x$ = check$(N)
End Print ; x$
Sub check (N) End
If N Mod 2 = 0 Then Function check$ (N)
Print "Even" If N Mod 2 = 0 Then
Else check$ = "Even"
Print "Odd" Else
End If check$ = "Odd"
End Sub End If
End Function
File handling in qbasic
File handling in QBASIC refers to the ability to read from and
write to files on disk using QBASIC commands and functions.
This capability allows QBASIC programs to interact with
external data stored in files, enabling tasks such as data input,
output, storage, and retrieval. File handling in QBASIC typically
involves operations like opening files, reading data from them,
writing data to them, and closing them when finished.
Different operations in file handling

Operation Mode

Write/Storage Output

Retrieve /Display/read/access Input

Add Append
Syntax for read and write data
Store/Write Display/Retrieve/read/access

Syntax: Syntax: Input #file no.,


write # file no.,v1,v2,v3…,Vn v1,v2,v3,…Vn
Eg: write #1,N$,a,a$ Eg: Input #1,N$,a,a$
……………. ……………..
Close #1 Close #1
Create a sequential data file name “library.txt” and store name address,
class, and roll no. of a student.
Open “library.txt” for output as #1
Cls
Input “Enter Name Address Class and Roll”;N$,A$,C,R
Write #1, N$,A$,C,R
Close #1
End
Create a sequential data file name “library.txt” and store name address,
class, and roll no. of few students.
Open “library.txt” for output as #1
Cls
Input “How many records?”;n
For i=1 to n
Input “Enter Name Address Class and Roll”;N$,A$,C,R
Write #1, N$,A$,C,R
Next i
Close #1
End
OPEN "MARKS.DAT" FOR OUTPUT AS #2
CLS
INPUT "Type Name of student "; n$
INPUT "Type Address "; a$
INPUT "Marks in English "; E
INPUT "Marks in Math "; M
INPUT "Marks in Science "; S
WRITE #2, n$, a$, E, M, S
CLOSE #2
END
===========================
OPEN "MARKS.DAT" FOR OUTPUT AS #2
top:
CLS
INPUT "Type Name of student "; n$
INPUT "Type Address "; a$
INPUT "Marks in English "; E
INPUT "Marks in Math "; M
INPUT "Marks in Science "; S
WRITE #2, n$, a$, E, M, S
INPUT "More Records (y/n) "; ans$
IF UCASE$(ans$) = "Y" THEN GOTO top
CLOSE #2
END
Program to append previous file

OPEN "MARKS.DAT" FOR APPEND AS #3


top:
CLS
INPUT "Type Name of student "; n$
INPUT "Type Address "; a$
INPUT "Marks in English "; E
INPUT "Marks in Math "; M
INPUT "Marks in Science "; S
WRITE #3, n$, a$, E, M, S
INPUT "More Records (y/n) "; ans$
IF UCASE$(ans$) = "Y" THEN GOTO top
CLOSE #2
END
Program to read data

REM Read data from a file


OPEN "MARKS.DAT" FOR INPUT AS #7
CLS
WHILE NOT EOF(7)
INPUT #7, n$, add$, a, b, c
PRINT n$, add$, a, b, c
WEND
CLOSE #7
END
Program to Read only 5 data

REM Read data from a file


OPEN "MARKS.DAT" FOR INPUT AS #7
CLS
FOR i = 1 TO 5
INPUT #7, n$, add$, a, b, c
PRINT n$, add$, a, b, c
NEXT i
CLOSE #7
END
Write a program in QBASIC to read all the records from
the data file “socre.dat” containing NAME, ROLL no. ,and
score. Then find and display the average score of the class.
Open "marks1.txt" For Input As #1
Cls
While Not EOF(1)
Input #1, N$, R, S
sum = sum + S
count = count + 1
Wend
avg = sum / count
Print "Average score is"; avg
Close #1
End
A sequential data file “student.txt” has name, class, roll no. and contact
number of students. WAP to enter name of student and delete the record
associated with that name.
Open "student.txt" For Input As #1
Open "temp.txt" For Output As #2
Input "Enter name to delete: "; X$
While Not EOF(1)
Input #1, N$, Class, Roll, Con#
If UCase$(N$) <> UCase$(X$) Then
Print #2, N$, Class, Roll, Con#
End If
Wend
Close #1
Close #2
Kill "student.txt"
Name "temp.txt" As "student.txt"
Print "Record deleted successfully."
End
A Sequential data file named “std.doc” contain name, class, Roll number
and contact number of few student. WAP to edit/modify/update records
according to name entered by user.
Open "student.txt" For Input As #1
Open "temp.txt" For Output As #2
Cls
Input "Enter name to update: "; X$
While Not EOF(1)
Input #1, N$, Class, Roll, Cont
If UCase$(N$) = UCase$(X$) Then
Input "Enter updated Name, Class, Roll Number, and Contact number: "; N$, Class, Roll, Cont
Write #2, N$, Class, Roll, Cont
Else
Write #2, N$, Class, Roll, Cont
End If
Wend
Close #1
Close #2
Kill "student.txt"
Name "temp.txt" As "student.txt"
Print "Record updated successfully."
End
Write a qbasic program to update the rate by 10% from a
sequential data file "Data.dat" that store item name, rate and
quantities
Open “Data.dat" For Input As #1
Open "temp.dat" For Output As #2
Cls
While Not EOF(1)
Input #1, N$, r, q
newRate = r * 1.1 ' Increase the rate by 10%
Write #2, N$, newRate, q
Wend
Kill “Data.dat"
Name "temp.dat" As “Data.dat"
Print "Rates updated successfully.“
Close #1
Close #2
End
Dry Run Table
declare sub show(A,B)
Cls
x = 1: y = 2 Loop A B X Y I Condition Output
Call show(x, y)
End - - - 1 2 - - -
- 1 2 1 2 1 - 1
Sub show (A, B)
1 3 5 1 2 2 2<=5(T) 3
i=1
Do 2 8 13 1 2 3 3<=5(T) 8

Print A; 3 21 34 1 2 4 4<=5(T) 21
A=A+B
B=A+B 4 55 89 1 2 5 5<=5(T) 55

i=i+1 5 144 233 1 2 6 6<=5(F) -


Loop While i <= 5
End Sub
Debug
REM display Records of students from data file
Open “Srudent.dat” for display AS #1
Print “Roll”, “Name”, “Address”, “Class”, “Section”
Do while not EOF
Input #1, RN, N$, AD$, CL, S$
Print RN, N$, AD$, CL, S$
Next
Finish #1
End
Rem Program to reverse the string or word
Declare function rev(w$)
Cls
Input “Enter the word”;w$
X$=rev(w$)
Print “Reverse is”; X$
End
Function rev(w$)
For i=len(w$) to 1 step 1
C$=left$(w$),i,1
D$=d$+c$
Loop
D$=rev
End function
C Programming
C programming is a computer programming language originally
developed by Dennis Ritchie at Bell Labs in the early 1970s. It is a
general-purpose, procedural programming language with a syntax
that allows for low-level memory manipulation, making it suitable
for system programming such as operating systems and device
drivers, as well as application programming.
C is often referred to as a middle-level language because it combines
the features of low-level languages (like assembly language, which
deals with hardware components directly) with high-level languages
(like Python or Java, which offer easier abstraction and portability).
This makes C versatile, allowing programmers to write efficient and
fast code while still maintaining a level of abstraction that simplifies
development compared to purely low-level languages.
Things to remember in c programming
Keywords and identifier
- Int money: Here int is keyword and money is identifier
Variable and constant
Data types
- Int (%d), float (%f), char(%c)
C input/output
- Scanf , Printf
C operator
- +, - , *, / , > , <, =, >=, &&, ||
C flow control
- C if else
- C for Loop
- C while loop
- C switch case
Program to find the sum of two numbers

#include<stdio.h>
void main()
{
int a,b,sum=0;
printf("Enter any two numbers:\n");
scanf("%d%d",&a,&b);
sum = a+b;
printf("Sum of two number is %d",sum);
getch();
}
Program to check input character is vowel or consonant
#include <stdio.h>
int main() {
char ch;
// Prompt the user to enter a character
printf("Enter a character: ");
scanf("%c", &ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
printf("%c is a vowel.\n", ch);
else if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
printf("%c is a vowel.\n", ch);
else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
printf("%c is a consonant.\n", ch);
else
printf("%c is not an alphabet.\n", ch);
return 0;

You might also like