xii (intermediate-2nd year) computer practicals · xii (intermediate-2nd year) computer practicals...
TRANSCRIPT
-
7
C/C++ & Databases
Mohsin Ahmed Khan Ghori
(Lecturer)
Pakistan Shipowners Govt. College
XII (Intermediate-2nd year) COMPUTER PRACTICALS
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 2
Preface
We take refugee to ALLAH from the saitan the repelled. In the name of ALLAH the MOST BENEFICIENT
and the MOST MERCIFUL. Let me say about this book, actually I was trying for very long period to
write a book in English and Urdu (i.e single book with English and Urdu.). I have had various topics like
in my mind to write a book e.g. MS-Word/MS-Excell/Power Point/Access, Programming language, Guide
for intermediate students. As I hadn’t have any experience of writing any book. So , I spent lots of time,
once I started I just went into the depth of each and every topic in this way I failed several time to
conclude the topic, as knowledge has no limit. My major problem was composing I wasn’t only behind the
concept I was composing(typing) the text to the book my-self in English and Urdu, so typing was also
wasting my enormous amount of time specially in Urdu typing. Finally, I cope up with the solution that I
must not go into the depth of topic and suspend my Urdu Chapter to limited time, with this idea I alhamdo
LILLAH succeed in writting, this book as resultant of my solution. I am hopeful that this book will be
helpful not only for the intermediate(second year) students but will also be helpful to the beginners of
Computer Science and Information Technology field.
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 3
ACKNOWLEDGEMENT
The basic idea for creating this Practical book was taken from my friend’s
(Prof. Syed Imran Ali )Practical Book, he introduced this sort of book in his university for the ease of his
students. So, I am following this concept for my students. I am thankful to Mr. Asher Saeed Alam, Mr. Ali
Akber for their support for reviewing this book as an English and Urdu experts. I am also thankful to
Mr.Asif Ali Sanghi and Mr.Shahzada Waseem Shahid for providing me moral support for writing the
book. I am grateful to all those people whom directly or in-directly supported me during the period of
writing this book may ALLAH provide all of us the righteous way and show HIS blessing upon all of us
(ameen).
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 4
Contents
Flow chart
Algorithm / Pseudo code, installation of C/C++,
Structure of C/C++,
Essential Elements of C/C++,
I/O Statements
Control Structure in C/C++,
Comments in C/C++,
first program in C/C++,
C/C++ data types,
Escape Sequences
Format specifiers,
Arithmetic Operator,
Relational Operators,
Logical Operators,
Bitwise Operators,
Loops,
Practicals (1-20) with output.
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 5
FLOW CHART FIGURES, THEIR NAME AND PURPOSE
SNO. SHAPES NAME PURPOSE
1 OVAL START / STOP
2 PARALLELOGRAM DISPLAY/PRINT
3 RECTANGLE PROCESS/CALCULATE
4 DIAMOND DECISION/CONTROL
5 CIRCLE LINE CONNECTOR
6 PENTANGULAR PAGE CONNECTOR
7 ARROW FLOW DIRECTION
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 6
ALGORITHMIC/PSEUDOCODIC NOTATIONS
C-Language Program Statement Algorithmic /Pseudocode Notations
{ // Begin of main body:
printf(); puts(); putch(); cout() ; etc.
scanf();getch();getche();getchar(); gets(); etc.
sum= total + 5;
if ( sum = 536 && percentage >= 33)
{
printf(“Pass”); getch();
}
Else
{
printf(“Fail”); getch();
}
for( variable =1; variable
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 7
Structure of C/C++
Element1 Preprocessor Directive #include
#include
Element2 Public/Global variable/functions
declaration.
void hilal(void);
int Ali(void);
void kashif_1(char,int);
char Husain_Gul(int,char);
Element3 Main function void main(void) / char main(int)
Element4 Body of main function {
clrscr();
printf(“Body of main function”);
getch();
}
Element5 Definition of Element2 declared
functions
void shipowners(void)
{ clrscr();}
int psgc(void)
{return 957;}
void kashif_1(char c,int i)
{printf(“C= %c, i= %d”,c,i);}
char Husain_Gul(int ii,char cc)
{printf(“C= %c, i= %d”,c,i);
return ‘t’;}
Essential elements in Structure of C/C++
Element1 Preprocessor Directive #inlcude
#include
Element3 Main function void main(void) / char main(int)
main() / int main(char)
Element4 Body of main function {
clrscr();
printf(“Body of main function”);
getch();
}
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 8
Three Control Structure in C
Sequential
Control
Top-Down approach
Statements are executed in
sequence. Top given statement is
executed first then next given
statements then next given
statement then so on till last
statement.
Decision/Selection
Control
If , else if, else
Switch/case
Iteration/
Repetitive Control
Loops
1 For loop
2 While loop
3 Do-While loop
How to comment in C/C++
Two(02) ways/methods to comment in C/C++
1.First Method double slash ( // comment ):
// comments single comment/ remarks/ descriptions
2. Second Method Slash Staric ( /* comment */ ):
/*
************************************************
Comments single and more than single line comments/remarks
***********************************************
*/
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 9
/***( enter date here) **********************************
This program is for learning C/C++ programming from scratch/zero.
Enter your Task/Practical Object here: Following program(Code) is for displaying/printing Welcome
message on screen.
***************************************************/
#include
void main(void)
{
printf(“Assalam Alykum from Pakistan Shipowners Govt. College”);
}// eof program.
/***( 31-12-2017) ************************************
Task2: This program is for printing/display welcome message with the use of format specifier %s.
***************************************************/
#include
void main(void)
{
printf(“%s”,Assalam Alykum from Pakistan Shipowners Govt. College”);
}
// eof program.
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 10
I/O Statements
Statements that are used for reading/inputing data/information from input devices (keyboard, mouse,
scanner etc.) are called input statements
e.g. scanf(), getch(), getche(), getchar(), gets() etc.
Statements that are used for printing/displaying/writing data/information on output devices
(Screen/Printer/Projector etc) are called Output statements
e.g. printf(), putch(), puts() etc
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 11
Data Types:
Following are the data types(Variables) used in c/c++ language with their size/sign qualifiers, format
specifiers, ranges and size.
Data Type Size
qualifier
Sign
qualifier
Denoted
by
Format
Specifier
Range Size
in
bytes
Size
in
Bits
Character n/a signed char %c [0 to 255] 1 8
n/a unsigned unsigned
char
%c [-128 to 127] 1 8
Integer(Whole
Number)
short signed short %d [-128 to 127] 1 8
short unsigned unsigned
short
%u [0 to 255] 1 8
n/a signed int %d [-32768] to
[32767]
2 16
n/a unsigned unsigned
int
%u [0 to 65535] 2 16
long signed long int %ld [-2147483648
to
2147483647]
4 32
long unsigned unsigned
long int
%lu [0 to
4294967295]
4 32
Float n/a signed float %f [3.4x10e-38
to
3.4x10e+38
4 32
Double Float double signed double %lf [1.7x10e-308
to
1.7x10e+308]
8 64
Long Double long signed long
double
%Lf 3.4x10e-4932
to
3.4x10e+4932
10 80
Character Data Type:
Character is an English alphabet, it occupies one(01) byte, with range mentioned above
It is denoted by the keyword/reserve word char. Format specifier %c is used to represent character
Explicit declaration of signed char is required whereas unsigned qualifier implicitly added to it when not
declared as signed.
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 12
Character data type: char is the keyword used in C-Programming language to define variables of
character data type. Char data type can store a single alphabet or digit or special characters like (*,$,;.’)
etc. the size of char data type is 1byte.
Integer Data Type:
Integers are whole numbers, it occupies two(02) bytes, with range mentioned above
It is denoted by the keyword/reserve word int. Format specifier %d, %u, %ld, %lu are used to represent
integer numbers.
Explicit declaration of sign qualifier unsigned int is required for declaration of unsigned integers,
whereas no need for specify signed qualifier as it is implicitly added to it.
Two more integer data types that are available in C/C++ are short and long.
For declaring short and long integers size qualifier short/long must be declared before integer i.e.
short int shortNum;
long int longNum;
or
Only short/long can also be used to declare data types of short and long integers respectively
short shortNum,
long longNum;
Same format specifier %d %u is used for signed and unsigned short. But for long %ld and %lu is used to
mention signed and unsigned long.
Float Data Type:
Float are real numbers, it occupies four(04) bytes, with range mentioned above.
It is denoted by the keyword/reserve word float. Format specifier %f is used to represent float.
No declaration of unsigned/signed is required for float point number, because they are always treated as
signed numbers. The calculation made on float data type is comparatively slower as compare to integer
data type.
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 13
Double (double float) Data Type:
Double are real numbers same like float with larger capacity and precision, that’s why they are also
called double float, it occupies Eight(08) bytes, which is double in size of float, with range mentioned
above.
It is denoted by the keyword/reserve word double. Format specifier %lf is used to represent double
numbers.
No declaration of unsigned/signed is required for double number, because they are always treated as
signed numbers.
Long double (long double float) Data Type:
Long Double are real numbers same like float,double float with larger capacity and precision, that’s why
they are called long double, it occupies ten(10) bytes, which is two more bytes as compare to the size of
double, its range mentioned above.
It is denoted by the keyword/reserve word long double. Format specifier %Ld is used to represent
double numbers.
No declaration of unsigned/signed is required for long double number, because they are always treated
as signed numbers.
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 14
Escape Sequences
Some English alphabets and punctuating character, which are used with back-slash( \ )within the output
statements of C/C++ language, are called Escape Sequences, these Escape Squences have special
purpose, whose description is given below in the table.
Escape
Sequence
Purpose Example Output
\n It is used
for new line
Printf(“ My name is Ali \n and my
college is ShipOwners College”)
My name is Ali
And my college is ShipOwners
College
\t 8-spaces
vertical gap
Printf(“ My name is \t Wali”)
My name is Wali
\b One Space
Back
Printf(“ My name it\bs Mohsin”) My name is Mohsin
\r Start of
Current line
Printf(“My name is\r and my college
name is ShipOwers College”)
and my college name is
ShipOwners College
\a Produce
Beep/Sound
Printf(“ Enter your name ? \a”) Enter your name ?
\v Vertical tab Printf(“Enter \v name ? “); Enter name?
\f Next line Printf(“ your name is \f Mohsin”); Your name is
Mohsin
\” Prints
double
Quotation
Printf(“\”S\”hipOwners College”) “S”hipOwners College
\\ Prints back
slash
Printf(“\\ Mohsin \\”) \Mohsin\
\xdd Prints
Hexa-
decimal
ASCII
value
Keywords
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 15
The following list shows the reserved words in C. These reserved words may not be used as constants or
variables or any other identifier names.
auto else long switch
break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct _Packed
double
Assignment operator (=)
The assignment operator assigns a value to a variable.
x = 5;
This statement assigns the integer value 5 to the variable x. The assignment operation always takes place
from right to left, and never the other way around:
x = y;
This statement assigns to variable x the value contained in variable y. The value of x at the moment this
statement is executed is lost and replaced by the value of y.
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 16
Assignment operations are expressions that can be evaluated. That means that the assignment itself has a
value, and -for fundamental types- this value is the one assigned in the operation. For example:
y = 2 + (x = 5);
In this expression, y is assigned the result of adding 2 and the value of another assignment expression
(which has itself a value of 5). It is roughly equivalent to:
x = 5;
y = 2 + x;
With the final result of assigning 7 to y.
The following expression is also valid in C++:
x = y = z = 5;
It assigns 5 to the all three variables: x, y and z; always from right-to-left.
Arithmetic operators ( +, -, *, /, % )
Five (05) arithmetical operations
supported in C and C++ are given below
Description
+ addition
- subtraction
* multiplication
/ division
% modulus
Operations of addition, subtraction, multiplication and division correspond literally to their respective
mathematical operators. The last one, modulo operator, represented by a percentage sign (%), gives the
remainder of a division of two values. For example:
x = 17 % 5;
results in variable x containing the value 2, since dividing 17 by 5 results in 3, with a remainder of 2.
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 17
Compound assignment (+=, -=, *=, /=, %=, >>=,
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 18
In Example 1, the value assigned to y is the value of x after being increased. While in Example 2,
it is the value x had before being increased.
Relational and comparison operators ( ==, !=, >, =, Greater than
= Greater than or equal to
Here there are some examples:
1
2
3
4
5
(7 == 5) // evaluates to false
(5 > 4) // evaluates to true
(3 != 2) // evaluates to true
(6 >= 6) // evaluates to true
(5 < 5) // evaluates to false
Of course, it's not just numeric constants that can be compared, but just any value, including, of
course, variables. Suppose that a=2, b=3 and c=6, then:
1
2
3
4
(a == 5) // evaluates to false, since a is not equal to 5
(a*b >= c) // evaluates to true, since (2*3 >= 6) is true
(b+4 > a*c) // evaluates to false, since (3+4 > 2*6) is false
((b=2) == a) // evaluates to true
Be careful! The assignment operator (operator =, with one equal sign) is not the same as the
equality comparison operator (operator ==, with two equal signs); the first one (=) assigns the
value on the right-hand to the variable on its left, while the other (==) compares whether the
values on both sides of the operator are equal. Therefore, in the last expression ((b=2) == a),
we first assigned the value 2 to b and then we compared it to a (that also stores the value 2),
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 19
yielding true.
Logical operators ( !, &&, || )
The operator ! is the C++ operator for the Boolean operation NOT. It has only one operand, to
its right, and inverts it, producing false if its operand is true, and true if its operand is false.
Basically, it returns the opposite Boolean value of evaluating its operand. For example:
1
2
3
4
!(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true
!(6 6) ) // evaluates to true ( true || false )
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 20
When using the logical operators, C++ only evaluates what is necessary from left to right to
come up with the combined relational result, ignoring the rest. Therefore, in the last example
((5==5)||(3>6)), C++ evaluates first whether 5==5 is true, and if so, it never checks
whether 3>6 is true or not. This is known as short-circuit evaluation, and works like this for
these operators:
Operator Short-circuit
&& if the left-hand side expression is false, the combined result is false (the right-hand
side expression is never evaluated).
|| if the left-hand side expression is true, the combined result is true (the right-hand side
expression is never evaluated).
This is mostly important when the right-hand expression has side effects, such as altering values:
if ( (i
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 21
a = (b=3, b+2);
would first assign the value 3 to b, and then assign b+2 to variable a. So, at the end,
variable a would contain the value 5 while variable b would contain value 3.
Bitwise operators ( &, |, ^, ~, )
Bitwise operators modify variables considering the bit patterns that represent the values they
store.
Operator Equivalent Description
& AND Bitwise AND
| OR Bitwise inclusive OR
^ XOR Bitwise exclusive OR
~ NOT Unary complement (bit inversion)
> SHR Shift bits right
Explicit type casting operator
Type casting operators allow to convert a value of a given type to another type. There are
several ways to do this in C++. The simplest one, which has been inherited from the C language,
is to precede the expression to be converted by the new type enclosed between parentheses (()):
1
2
3
int i;
float f = 3.14;
i = (int) f;
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 22
The previous code converts the floating-point number 3.14 to an integer value (3); the remainder
is lost. Here, the typecasting operator was (int). Another way to do the same thing in C++ is to
use the functional notation preceding the expression to be converted by the type and enclosing
the expression between parentheses:
i = int (f);
Both ways of casting types are valid in C++.
sizeof
This operator accepts one parameter, which can be either a type or a variable, and returns the
size in bytes of that type or object:
x = sizeof (char);
Here, x is assigned the value 1, because char is a type with a size of one byte.
The value returned by sizeof is a compile-time constant, so it is always determined before
program execution.
Other operators
Later in these tutorials, we will see a few more operators, like the ones referring to pointers or
the specifics for object-oriented programming.
Precedence of operators (BODMAS / PEMDAS)
A single expression may have multiple operators. For example:
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 23
x = 5 + 7 % 2;
In C++, the above expression always assigns 6 to variable x, because the % operator has a
higher precedence than the + operator, and is always evaluated before. Parts of the expressions
can be enclosed in parenthesis to override this precedence order, or to make explicitly clear the
intended effect. Notice the difference:
1
2
x = 5 + (7 % 2); // x = 6 (same as without parenthesis)
x = (5 + 7) % 2; // x = 0
From greatest to smallest priority, C++ operators are evaluated in the following order:
Level Precedence group Operator Description Grouping
1 Scope :: scope qualifier Left-to-right
2 Postfix (unary)
++ -- postfix increment / decrement
Left-to-right
( ) functional forms
[ ] subscript
. -> member access
3 Prefix (unary) ++ -- prefix increment / decrement Right-to-left
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 24
~ ! bitwise NOT / logical NOT
+ - unary prefix
& * reference / dereference
new delete allocation / deallocation
sizeof parameter pack
(type) C-style type-casting
4 Pointer-to-member .* ->* access pointer Left-to-right
5 Arithmetic: scaling * / % multiply, divide, modulo Left-to-right
6 Arithmetic:
addition
+ - addition, subtraction Left-to-right
7 Bitwise shift > shift left, shift right Left-to-right
8 Relational < > = comparison operators Left-to-right
9 Equality == != equality / inequality Left-to-right
10 And & bitwise AND Left-to-right
11 Exclusive or ^ bitwise XOR Left-to-right
12 Inclusive or | bitwise OR Left-to-right
13 Conjunction && logical AND Left-to-right
14 Disjunction || logical OR Left-to-right
15 Assignment-level
expressions
= *= /= %= += -=
>>=
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 25
which one is evaluated first: either left-to-right or right-to-left.
Enclosing all sub-statements in parentheses (even those unnecessary because of their
precedence) improves code readability.
Loops
Three types of loops in c/c++
1-For-loop 2-While-loop 3-Do while loop
(Must Run atleast 1 time)
For(initial value;condition(s);
step up/down)
While(condition(s))
{
Statement(s);
}
Do
{
Statement(s);
} while(condtion(s));
for( a=1;a
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 26
} } while(a
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 27
3. Find the factorial of an inputted number.
4. Write a program that finds out the greatest number among three input numbers.
5. Write a program which uses arithmetic operators to calculate the area of triangle and volume of sphere .
6. Write a program ,that convert upper case letter to lower case letter and vise versa (using function ) .
7. Write a program to draw a check-board using if-else statement and nested for loops.
8. Write a program that generate characters corresponding to ASCII codes from 32 to 127 (using any loop).
9. Write a program that reads and prints the data using escape sequence (asking the name, age ,height and gender of student using scanf() and printf() statements).
10. Write a program that input a number and then check it whether it is prime or not . 11. Write a program which print a text of four lines consisting of characters, integers
value and floating point value using printf() statement and escape sequences.
12. Write a program ,which uses Switch and break statement . 13. Write a program using nested for loop to print the following output . *
* *
* * *
* * * *
* * * * *
14. Write a program that input any two number and then pass these numbers as arguments a function sum1 and then print their sum.
15. Write a program to calculate a pay roll of employees .Read the Basic pay from key board .Calculate medical allowance as 25% of basic pay , conveyance allowance as
20% and house rent 45 % of basic pay and deduction of GP fund 7% of basic pay .
Calculate gross pay and net pay .
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 28
/* ************************************************************************
Object: Write a program that input a year and then check whether its leap year or Not
*************************************************************************/
#include
#include
int givenYear; // variable declaration.
void main(void) // main function.
{ // start of-(sof) body of main function.
clrscr();
// below code is for showing heading of the output
printf("\n\t\t______________________________");
printf("\n\t\tThis is sample Practical No.01");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\t Enter any year to check wether it's LEAP year or NOT? ");
scanf("%d",&givenYear);
if(givenYear%4==0)
{printf("\n\t Given year %d is LEAP year",givenYear);}
else
{printf("\n\t Given year %d ISN'T leap year",givenYear);}
getch(); // pause screen till any button is pressed.
} // end of-(eof) body of main function.
/************************** OUTPUT ***************************************
_______________________________
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 29
This is sample Practical No.01;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter any year to check wether it's LEAP year or NOT? 1999
Given year 1999 ISN'T leap year
**************************************************************************
/* ***PSGC***|PRACTICAL02:Write a program that read marks of 5 subjects, calculate the
total marks, percentage & state whether candidate is Pass or Fail.*****/
#include
#include
int marks[5],total,counter;
float percentage;
// varible declaration.
void main(void) // main function.
{ // start of-(sof) body of main function.
clrscr();
// below code is for showing heading of the output
printf("\n\t\t______________________________");
printf("\n\t\tThis is sample Practical No.02");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
total=0;percentage=1.0;
for(counter=0;counter
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 30
percentage=total*100.0/500;
for(counter=0;counter
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 31
**************************************************************************/
/* ***PSGC***|PRACTICAL03: Write program to generate the table of any inputted
number *****/
#include
#include
int inputtedNumber,multiple; // variables declaration.
void main(void) // main function.
{ // start of-(sof) body of main function.
clrscr();
// below code is for showing heading of the output
printf("\n\t\t______________________________");
printf("\n\t\tThis is sample Practical No.02");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\t Enter any number to generate it's table? ");
scanf("%d",&inputtedNumber);
// for-loop to generate table till 12.
for(multiple=1;multiple
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 32
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
5 * 11 = 55
5 * 12 = 60
**************************************************************************/
/* ***PSGC***|PRACTICAL04: Write a program that finds the factorial of an inputted
number.*****/
#include
#include
int inputtedNumber,factorial;
// variable declaration.
void main(void) // main function.
{ // start of-(sof) body of main function.
clrscr();
// below code is for showing heading of the output
printf("\n\t\t______________________________");
printf("\n\t\tThis is sample Practical No.04");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\t Enter number to find it's factorial ? ");
scanf("%d",&inputtedNumber);
printf("\n\t Factorial of [%d]",inputtedNumber);
factorial=1.0;
for(inputtedNumber=inputtedNumber;inputtedNumber>=1;inputtedNumber--)
{factorial*=inputtedNumber;}
printf(" is [%d]",factorial);
getch(); // pause screen till any button is pressed.
} // end of-(eof) body of main function.
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 33
/************************ OUTPUT *************************************
_______________________________
This is sample Practical No.04
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter number to find it's factorial ? 6
Factorial of [6] is [720]
***********************************************************************/
/* ***PSGC***|PRACTICAL05:Write a program that finds out the greatest number among
three inputted numbers.*****/
#include
#include
int number1,number2,number3; // variable declaration.
void main(void) // main function.
{ // start of-(sof) body of main function.
clrscr();
// below code is for showing heading of the output
printf("\n\t\t______________________________");
printf("\n\t\tThis is sample Practical No.05");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\t Enter Number1 ? ");
scanf("%d",&number1);
printf("\n\t Enter Number2 ? ");
scanf("%d",&number2);
printf("\n\t Enter Number3 ? ");
scanf("%d",&number3);
if(number1>number2)
{ // body of outer-most if statement.
if(number1>number3)
{ // body of inner if statement.
printf("\n\t\t number1=[%d] is GREATEST among all three inputted Numbers",number1);
}
else
{ // else of inner if.
printf("\n\t\t number3=[%d] is GREATEST among all three inputted Numbers",number3);
}
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 34
}
else if(number2>=number3)
{ // else-if of outer-most if statement.
printf("\n\t\t number2=[%d] is GREATEST among all three inputted Numbers",number2);
}
else
{ // else of outer-most if statement.
printf("\n\t\t number3=[%d] is GREATEST among all three inputted Numbers",number3);
}
getch(); // pause screen till any button is pressed.
} // end of-(eof) body of main function.
/************************ OUTPUT *************************************
_______________________________
This is sample Practical No.05
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter number1 ? 6
Enter number2 ? 7
Enter number3 ? 5
number2=[7] is GREATEST among all three inputted Numbers
***********************************************************************/
/* ***PSGC***|PRACTICAL06:Write a program which uses arithmetic operators to calculate
the area of triangle and volume of sphere.
area of a triangle=(1/2)Base x Height
volume of sphere=(4/3 x pi x radius x radius x radius) *****/
#include
#include
#include
#define pi 3.14
float areaOfTriangle,volumeOfSphere; // variable declaration.
int radius,base,height;
void main(void) // main function.
{ // start of-(sof) body of main function.
clrscr();
// below code is for showing heading of the output
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 35
printf("\n\t\t______________________________");
printf("\n\t\tThis is sample Practical No.06");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\t Enter Base of a triangle? ");
scanf("%d",&base);
printf("\n\t Enter Height of a triangle? ");
scanf("%d",&height);
areaOfTriangle=(1.0/2.0*base*height);
printf("\n\t Enter radius of an sphere? ");
scanf("%d",&radius);
volumeOfSphere= 4.0/3*pi*pow(radius,3);
printf("\n\t Area of Triangle[base=%d height=%d] is %.2f",base,height,areaOfTriangle);
printf("\n\t Volume of Sphere[radius=%d] is %.2f",radius,volumeOfSphere);
getch(); // pause screen till any button is pressed.
} // end of-(eof) body of main function.
/************************ OUTPUT *************************************
_______________________________
This is sample Practical No.06
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter Breath of a triangle? 4
Enter Height of a triangle? 6
Enter radius of an sphere? 3
Area of Triangle[base=6 height=4] is 12.00
Volume of Sphere[radius=3] is 113.04
***********************************************************************/
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 36
/* ***PSGC***|PRACTICAL07:write a program to draw a check-board using if-else statement
and Nested for loops. *****/
#include
#include
int rowCounter,columnCounter; // variable declaration.
void main(void) // main function.
{ // start of-(sof) body of main function.
clrscr();
// below code is for showing heading of the output
printf("\n\t\t______________________________");
printf("\n\t\tThis is sample Practical No.07");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\n"); // printf two line gap on screen.
for(rowCounter=1;rowCounter
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 37
█ █ █ █
█ █ █ █
█ █ █ █
█ █ █ █
***********************************************************************/
/* ***PSGC***|PRACTICAL08:Write a program that generate characters corresponding to
ASCII codes from 32 to 127 (using any loop). *****/
#include
#include
int asciiCode; // variable declaration.
void main(void) // main function.
{ // start of-(sof) body of main function.
clrscr();
// below code is for showing heading of the output
printf("\n\t\t______________________________");
printf("\n\t\tThis is sample Practical No.08");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
for(asciiCode=32;asciiCode
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 38
/* ***PSGC***|PRACTICAL09:Write a program that reads and prints the data using escape
sequence (asking the name, age ,height and gender of student using scanf() and printf()
statements) *****/
#include
#include
char name[20],gender[10]; // variable declaration.
unsigned int age;
float height;
void main(void) // main function.
{ // start of-(sof) body of main function.
clrscr();
// below code is for showing heading of the output
printf("\n\t\t______________________________");
printf("\n\t\tThis is sample Practical No.09");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\t Enter Student name? ");
scanf("%s",name);
printf("\n\t Enter Student age? ");
scanf("%u",&age);
printf("\n\t Enter height (5.4/6.0 etc)? ");
scanf("%f",&height);
printf("\n\t Enter Student gender(male/female)? ");
scanf("%s",gender);
printf("\n\t Name of Student is %s",name);
printf("\n\t Age of Student is %u",name);
printf("\n\t Height of Student is %f",name);
printf("\n\t Gender of Student is %s",name);
getch(); // pause screen till any button is pressed.
} // end of-(eof) body of main function.
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 39
/************************ OUTPUT *************************************
_______________________________
This is sample Practical No.09
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter Student name? Hasan
Enter Student age? 3
Enter height (5.4/6.0 etc)? 3.25
Enter Student gender(male/female)? male
Name of Student is Hasan
Age of Student is 3
Height of Student is 3.25
Gender of Student is male
***********************************************************************/
/* ***PSGC***|PRACTICAL10:Write a program that input a number and then check it whether
it is prime or not . *****/
#include
#include
int number,divisor,flag; // variable declaration.
void main(void) // main function.
{ // start of-(sof) body of main function.
clrscr();
// below code is for showing heading of the output
printf("\n\t\t______________________________");
printf("\n\t\tThis is sample Practical No.10");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\t Enter Number to check whether is PRIME OR NOT? ");
scanf("%d",&number);
flag=0;
for(divisor=number/2;divisor>=2;divisor--)
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 40
{
if(number%divisor==0) {flag=1;printf("%d absolutely divide %d",divisor,number); break;}
}
if(flag==0) printf("\n\t Given Number %d is PRIME number",number);
else
printf(",Hence given Number %d is NOT a prime number",number);
getch(); // pause screen till any button is pressed.
} // end of-(eof) body of main function.
/************************ OUTPUT *************************************
_______________________________
This is sample Practical No.10
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter Number to check wheter its PRIME OR NOT? 7
Given Number 7 is PRIME number
***********************************************************************/
/* ***PSGC***|PRACTICAL11:Write a program which print a text of four lines consisting
of characters, integers value and floating point value using printf() statement and escape
sequences. . *****/
#include
#include
char name[30],favoriteAlphabet; // variable declaration.
unsigned int age;
float height;
void main(void) // main function.
{ // start of-(sof) body of main function.
clrscr();
// below code is for showing heading of the output
printf("\n\t\t______________________________");
printf("\n\t\tThis is sample Practical No.11");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\t Enter your name? ");
gets(name);
printf("\n\t Enter your favorite English alphabet? ");
favoriteAlphabet=getchar();
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 41
printf("\n\t Enter your age (5-100 etc)? ");
scanf("%u",&age);
printf("\n\t Enter your height (5.4/6.0 etc)? ");
scanf("%f",&height);
printf("\n\t Your Name is %s,",name);
printf("\n\t you like English alphabet %c,",favoriteAlphabet);
printf("\n\t you are %u years old,",age);
printf("\n\t and %.2f feet tall.",height);
getch(); // pause screen till any button is pressed.
} // end of-(eof) body of main function.
/************************ OUTPUT *************************************
_______________________________
This is sample Practical No.11
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your name? Muhammad Omar Khan Ghori
Enter your favorite English alphabet? M
Enter your age (3-100 etc)? 3
Enter your height (5.4/6.0 etc)? 3.25
Your Name is Muhammad Omar Khan Ghori,
you like English alphabet M,
you are 3 years old,
and 3.25 feet tall.
***********************************************************************/
/* ***PSGC***|PRACTICAL12:Write a program ,which uses Switch and break statement
.*****/
#include
#include
int rowCounter,columnCounter,result; // variable declaration.
void main(void) // main function.
{ // start of-(sof) body of main function.
clrscr();
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 42
// below code is for showing heading of the output
printf("\n\t\t______________________________");
printf("\n\t\tThis is sample Practical No.12");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\n"); // printf two line gap on screen.
for(rowCounter=1;rowCounter
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 43
/* ***PSGC***|PRACTICAL13:Write a program using nested for loop to print the following
output
*
* *
* * *
* * * *
* * * * *
**********************************************/
#include
#include
int rowCounter,columnCounter; // variable declaration.
void main(void) // main function.
{ // start of-(sof) body of main function.
clrscr();
// below code is for showing heading of the output
printf("\n\t\t______________________________");
printf("\n\t\tThis is sample Practical No.13");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\n"); // printf two line gap on screen.
for(rowCounter=1;rowCounter
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 44
/* ***PSGC***|PRACTICAL14:Write a program that input any two number and then pass
these
numbers as arguments to function sum1 and then print their sum.
****************************************************************/
#include
#include
void sum1(int,int); // function sum1 declaration.
int number1,number2; // variable declaration.
void main(void) // main function.
{ // start of-(sof) body of main function.
clrscr();
// below code is for showing heading of the output
printf("\n\t\t______________________________");
printf("\n\t\tThis is sample Practical No.14");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\n"); // printf two line gap on screen.
printf("\n\t Enter Number1 ? ");
scanf("%d",&number1);
printf("\n\t Enter Number2 ? ");
scanf("%d",&number2);
sum1(number1,number2); // calling sum1 function & passing number1 & number2 to it.
getch(); // pause screen till any button is pressed.
} // end of-(eof) body of main function.
// below is the definition of function sum1.
void sum1(int digit1,int digit2)
{ // start of sum1 function body.
printf("\n\t Sum of %d + %d = %d",digit1,digit2,(digit1+digit2));
return;
} // end of sum1 function body.
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 45
/************************ OUTPUT *************************************
_______________________________
This is sample Practical No.14
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter Number1 ? 7
Enter Number2 ? 6
Sum of 7 + 6 = 13
***********************************************************************/
/* ***PSGC***|PRACTICAL15:Write a program to calculate a pay roll of employees .Read
the
Basic pay from key board .Calculate medical allowance as 25% of basic pay , conveyance
allowance as 20% and house rent 45 % of basic pay and deduction of GP fund 7% of basic
pay
. Calculate gross pay and net pay .
****************************************************************/
#include
#include
float percentageCalculator(float,int); // function sum1 declaration.
float basicPay,medicalAllowance,conveyanceAllowance,houseRent,gpFund,grossPay,netPay;
// variable declaration.
void main(void) // main function.
{ // start of-(sof) body of main function.
clrscr();
// below code is for showing heading of the output
printf("\n\t\t______________________________");
printf("\n\t\tThis is sample Practical No.15");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\n"); // printf two line gap on screen.
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 46
printf("\n\t Enter Basic Pay ? ");
scanf("%f",&basicPay);
// calling percentageCalculator function & passing values to it.
medicalAllowance=percentageCalculator(basicPay,25);
conveyanceAllowance=percentageCalculator(basicPay,20);
houseRent=percentageCalculator(basicPay,45);
gpFund=percentageCalculator(basicPay,7);
grossPay=basicPay+medicalAllowance+conveyanceAllowance+houseRent;
netPay=grossPay-gpFund;
printf("\n\t BASIC PAY= %.2f",basicPay);
printf("\n\t MEDICAL ALLOWANCE= %.2f",medicalAllowance);
printf("\n\t ConveyanceALLoWANCE= %.2f",conveyanceAllowance);
printf("\n\t House Rent= %.2f",houseRent);
printf("\n\t Gross PAY= %.2f",grossPay);
printf("\n\t Deduction of GPFund= %.2f",gpFund);
printf("\n\t NET PAY= %.2f",netPay);
getch(); // pause screen till any button is pressed.
} // end of-(eof) body of main function.
// below is the definition of percentageCalculator function.
float percentageCalculator(float amount,int percent)
{ // start of function body.
return (amount*percent/100.0);
} // end of function body.
/************************ OUTPUT *************************************
_______________________________
This is sample Practical No.15
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter Basic Pay ? 1000
BASIC PAY= 1000.00
MEDICAL ALLOWANCE= 250.00
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 47
ConveyanceALLoWANCE= 200.00
House Rent= 450.00
Gross PAY= 1900.00
Deduction of GPFund= 70.00
NET PAY= 1830.00
***********************************************************************/
ALGORITHMS https://study.com/academy/lesson/what-is-an-algorithm-in-programming-definition-examples-
analysis.html
https://www.edrawsoft.com/explain-algorithm-flowchart.php
Definition of Algorithm
To WRITE a logical step-by-step method to solve the problem is called algorithm, in other
words, an algorithm is a procedure FOR solving problems. In order to solve a mathematical or
computer problem, this is the first step of the procedure. An algorithm includes calculations,
reasoning & data processing. Algorithms can be presented by natural languages, pseudo code
and flowcharts, etc.
Definition of Flowchart
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 48
A flowchart is the graphical or pictorial representation of an algorithm with the help of different
symbols, shapes and arrows in order to demonstrate a process or a program. With algorithms,
we can easily understand a program. The main purpose of a flowchart is to analyze different
processes. Several standard graphics are applied in a flowchart:
ALGORITHM-01(Create a simple Form to display Bio-data using label & textbox ,which
includes
IdentIFication No.,Name ,Father name,Address & Qualification etc)
Step1: BEGIN
Step2: DEFINE name,fatherName,address,qualIFication as TEXT
Step3: WRITE "Enter name? "
Step4: READ name
Step5: WRITE "Enter Father name? "
Step6: READ fatherName
Step7: WRITE "Enter Address? "
Step8: READ address
Step9: WRITE "Enter qualification? "
Step10: READ qualification
Step11: WRITE "Your Entered Name is ",name
Step12: WRITE "Your Entered Father Name is ",fatherName
Step13: WRITE "Your Entered Address is ",address
Step14: WRITE "Your Enter Qualification is ",qualification
Step15: END
ALGORITHM-02(Create a simple Form to add and multiply three numbers. Display the result
separately using Labels, Textboxes and Buttons)
Step1: BEGIN
Step2: DEFINE number1,number2,number3,result as INTEGER
Step3: WRITE "Enter number1? "
Step4: READ number1
Step5: WRITE "Enter number2? "
Step6: READ number2
Step7: WRITE "Enter number3? "
Step8: READ number3
Step9: SET result=(number1+number2+number3)
Step10: WRITE "The Result of addition of ",number1,"+",number2,"+",number3,"=",result
Step11: SET result=(number1*number2*number3)
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 49
Step12: WRITE "The Result of Multiplication of
",number1,"+",number2,"+",number3,"=",result
Step13: END
ALGORITHM-03(Create simple FORm to perform arithmetic operation using Labels and
Textboxes, each
operation should be performed by the buttons in the single Form.)
Step1: BEGIN
Step2: DEFINE number1,number2,result as INTEGER
Step3: DECLARE
INTEGER:onClickAddition(INTEGER,INTEGER),
INTEGER:onClickSubraction(INTEGER,INTEGER)
INTEGER:onClickMultiplication(INTEGER,INTEGER),
INTEGER:onClickDivision(INTEGER,INTEGER)
Step4: REPEAT
Step5: IF clickedAdditionButton THEN
result=onClickAddition(number1,number2)
WRITE "Result=",result
Step6: IF clickedSubtractionButton THEN
result=onClickSubraction(number1,number2)
WRITE "Result=",result
Step7: IF clickedMultiplicationButton THEN
result=onClickMultiplication(number1,number2)
WRITE "Result=",result
Step8: IF clickedDivisionButton THEN
result=onClickDivision(number1,number2)
WRITE "Result=",result
Step9: UNTIL NOT-onClickExit()
Step10: END
FUNCTION INTEGER:onClickAddition(number1,number2)
BEGIN
RETURN (number1+number2)
END_FUNCTION onClickAddition
FUNCTION INTEGER:onClickSubtraction(number1,number2)
BEGIN
RETURN (number1-number2)
END_FUNCTION onClickSubtraction
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 50
FUNCTION INTEGER:onClickMultiplication(number1,number2)
BEGIN
RETURN (number1*number2)
END_FUNCTION onClickMultiplication
FUNCTION INTEGER:onClickAddition(number1,number2)
BEGIN
RETURN (number1/number2)
END_FUNCTION onClickAddition
ALGORITHM-04(Create a Form to generate & display numbers from 1 to 10 using do-
WHILE/FOR loop)
REM 'do-WHILE loop of VB is equivalent to REPEAT-UNTIL loop of algorithm
Step1: BEGIN
Step2: DEFINE number as Integer
Step3: FOR number=1 to 10
Step4: WRITE number
Step5: NEXT number
Step6: Set number=1
Step7: REPEAT
Step8: WRITE number
Step9: UNTIL number
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 51
ALGORITHM-06(Find factorial of number by using any loop statement, READ values from
Textbox &
Write its factorial in the other Textbox)
Step1: BEGIN
Step2: DEFINE number,factorial as INTEGER
Step3: WRITE "Enter Number Whose Factorial is Required ? "
Step4: READ number
Step5: WRITE "Given Number ",number
Step6: SET factorial=number
Step7: FOR number=(number-1) to 2 STEP=-1
Step8: SET factorial=(factorial*number)
Step9: NEXT number
Step10: WRITE " Factorial = ",factorial
Step11: END
ALGORITHM-07(Create a function,which returns area of a circle of a given radius)
Step1: BEGIN
Step2: DEFINE GLOBAL PI as 3.142
Step3: DEFINE radius as INTEGER
Step4: DEFINE area as REALNUMBER
Step5: DECLARE REALNUMBER:areaOfCircle(INTEGER)
Step5: WRITE "Enter radius of Circle whose AREA is Required ? "
Step6: READ radius
Step7: SET area=areaOfCircle(radius) REM calling FUNCTION areaOfCircle(integer)
Step8: WRITE " AREA of a Circle = ",area
Step9: END
FUNCTION REALNUMBER:areaOfCircle(INTEGER rad)
BEGIN
RETURN (2*PI*rad)
END_FUNCTION areaOfCircle
ALGORITHM-08(Create a program to input users salary from a Textbox & calculate its tax
depending
upon the income & allowances,using nested if-else)
Step1: BEGIN
Step2: DEFINE basicSalary,medialAllowance,travellingAllowance,taxPayable as
REALNUMBER
Step3: WRITE "Enter Basic Salary of User ? "
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 52
Step4: READ basicSalary
Step5: SET medicalAllowance=(basicSalary*25)/100
Step6: SET travellingAllowance=(basicSalary*20)/100
Step7: SET grossSalary=basicSalary+medicalAllowance+travellingAllowance
Step8: IF grossSalary>=70000 THEN
taxPayable=(grossSalary*12)/100
ELSE IF grossSalary>=50000 and grossSalary
-
XII (Intermediate-2nd year) COMPUTER PRACTICALS C/C++:Language Databases
[MOHSIN AHMED KHAN GHORI: Lecturer], Pakistan ShipOwners’ Govt. College www.Pak-ShipOwnersGC.Com Page 53
Step4: WRITE oddNumber
Step5: NEXT oddNumber
Step6: END
Increment and decrement (++, --)Relational and comparison operators ( ==, !=, >, =,