Professional Documents
Culture Documents
CP Chapter 01 Introdution To Programming
CP Chapter 01 Introdution To Programming
programming
Prepared by:
Vishal Koshti
1
Introduction
Programming is an art, and to learn art we should learn how to make art.
Similarly, programming cannot be learnt just by knowing or reading syntax of a
particular programming language.
To learn programming, one must write programs on computer.
It is very necessary to know basic requirement of programming and basics of
programming before going directly for practical.
This basic knowledge is really helpful, once real programming using a language is
started.
There are different types of programming languages like,
1. Machine language programming
2. Assembly language programming
3. Higher level language programming
We can use programming to get the solution of given problem.
Algorithm and Flowchart are ways to get the solution of problem.
2
Introduction
Initially people use algorithm and flowchart for the solution purpose.
Algorithm is step by step solution of given problem.
Flowchart is graphical representation of given problem.
Explanation of different types of languages is mentioned below.
3
Types of programming languages
4
Types of programming languages
5
Types of programming languages
2. Assembly Language
Assembly language was developed to overcome some of the many inconveniences
of machine language.
This is another low-level but very important language in which operation codes
and operands are given in the form of alphanumeric symbols instead of 0’s and 1’s.
These alphanumeric symbols are known as mnemonic codes and can combine in a
maximum of five-letter combinations e.g. ADD for addition, SUB for subtraction,
START, LABEL etc.
Because of this feature, assembly language is also known as “Symbolic
Programming Language”.
This language is also very difficult and needs a lot of practice to master it because
there is only a little English support in this language.
Mostly assembly language is used to help in compiler orientations.
The instructions of the assembly language are converted to machine codes by a
language translator - Assembler and vice versa.
6
Types of programming languages
7
Types of programming languages
8
Types of programming languages
9
Types of programming languages
10
Types of programming languages
Compiler Interpreter
Scans the entire program and translates it Translates program one statement at a
as a whole into machine code. time.
It takes large amount of time to analyze It takes less amount of time to analyze the
the source code but the overall execution source code but the overall execution
time is comparatively faster. time is slower.
Generates intermediate object code No intermediate object code is generated,
which further requires linking, hence hence are memory efficient.
requires more memory.
It generates the error message only after Continues translating the program until
scanning the whole program. Hence the first error is met, in which case it
debugging is comparatively hard. stops. Hence debugging is easy.
Programming language like C, C++ use Programming language like Python, Ruby
compilers. use interpreters.
11
Characteristics of higher level
language
There are many higher level languages are available in the computer world like C,
C++, Java, C#, .NET, Php etc.
Among all these languages there are some characteristics which are common to all
the languages which are listed below.
1. Interactive
2. Variety of data types
3. Rich set of operators
4. Flexible control structures
5. Readability
6. Modularity
7. File handling
8. Memory management
9. Interface to other languages
10.Availability of library support
12
Characteristics of higher level
language
1. Interactive
Interactive means two way communication.
Most of the higher level languages are interactive with user, they allow user to
interact with program through input output devices while program is executing.
In c through library functions we can interact with input output devices like
<stdio.h>.
For example, printf() function is used to display the message on console and scanf()
function is used to take input from the keyboard.
13
Characteristics of higher level
language
Data types are divided into two categories,
A. Basic data types
B. User defined data types
Basic data types are those data types which are provided by language itself to
store characters, integers and real values.
User defined data types are those data types which are defined by user to store
complex information.
Examples of user defined data types are array, structure, union, class etc.
C language supports five basic data types,
A. void
B. char
C. int
D. float
E. double
14
Characteristics of higher level
language
C language has four data type qualifiers to extend the range and values.
A. short
B. long
C. signed
D. unsinged
15
Characteristics of higher level
language
4. Flexible control structures
The control structures are providing necessary mechanism for implementing
algorithms in language formats.
The structure programming languages provide mainly three types of controls
which are,
A. Sequence
B. Selection or decision
C. Repetition or looping
Any problem includes any kind of operations, to get the solution of problem
respective language should provide above control structure.
A. Sequence
Almost all programming languages supports sequence control structure.
It means that execute the set instruction line by line, one by one.
16
Characteristics of higher level
language
B. Selection or decision
While solving any problem it might be possible that we end up at such situation
where we can execute one flow from two different flow.
So in such case we need to take decision where to go?
Either on left side or right side.
C support different kinds of mechanism through which we can make decision
depends on give situation,
a. only if
b. if-else
c. nested if
d. if-else-if ladder
e. switch case
17
Characteristics of higher level
language
C. Repetition or looping
In programming language it is also possible that we need to execute some set of
instruction again and again.
So instead of writing same lines of code again and again we can put such
statement inside the loop.
C supports three different types of loops,
a. for loop
b. while loop
c. do....while loop
5. Readability
A comment is a sentence which explains some aspect of a program segment or
statement.
18
Characteristics of higher level
language
Thus, the comments help in process of reading and understanding programs.
For example, at the beginning user may write problem statement as comment
which explains the purpose for which program is written.
In C comment is written in /*….*/.
Anything written between /* and */, is considered as comment and removed
during compilation process as comments are not part of program.
They are written just for improving readability of a program.
6. Modularity
As the size and complexity increases, developing a single large program is too
difficult.
It is also unmanageable, even once it is developed.
Programming languages support concept known as modularity in order to handle
size and complexity effectively.
19
Characteristics of higher level
language
Modularity is nothing but dividing large program into collection of small
subroutines.
Each subroutine is itself a small program and perform well defined task.
Modularity not only reduces development complexity, but also reduces complexity
of managing the code i.e. changes, bug fixing, increasing functionalities etc.
In C programming, function is a vehicle to support modularity.
Any C program is a collection of one or more functions.
As function is small and performing specified task, it is easy to write and
understand also.
7. File handling
In many applications, input to program is taken from another file which is already
available or output of program is stored in file for future use.
20
Characteristics of higher level
language
If output of the program is stored in a file, it can be transferred to other machine
through a disk.
For example, in payroll application it is not possible that every time when program
runs, all the details about employees are entered again.
Rather, details are kept in a file which is read by program when runs and changes
made during execution are stored into file so that when program runs again, it
starts with latest detail.
In C programming, file management is supported through rich set of file handling
function which allows user to open a file, read from a file, write into file etc.
8. Memory management
Memory management covers the issue of allocating memory dynamically at run
time and deallocating memory during execution of it is no longer required so that
it can be used later for other purpose.
21
Characteristics of higher level
language
The memory management can be supported either through library calls or directly
using system calls.
The C programming supports memory management function in library known as
“<malloc.h>”.
It provides function like “malloc()” to allocate memory dynamically and “free()” to
release the memory.
22
Characteristics of higher level
language
If then facilitates to access operating system services directly or some critical
portion can be written in assembly for efficiency purpose.
23
Problem decomposition
and partitioning
The first step in developing a program is to understand the problem for which
program is to be developed.
Unless until problem is not clearly understood, program cannot be developed or it
may be developed incorrectly.
The size and complexity of problem are two major factors which needs to be
considered while understanding a problem and developing a solution.
A small and simple problem can be understood easily, while large and complex
problem requires some proper method or procedure to simplify it in order to get
its details and to find solution.
One of the well-known and widely used method is decomposition or partitioning
of a problem.
In decomposition or partitioning, given problem which is very difficult to
understand as single piece is divided into sub problems.
Each sub problem then can be further divided into smaller problems.
24
Problem decomposition
and partitioning
This process is repeated until, you get a set of subproblems which are small, easy
to understand and manage.
After developing solution to each small problem, integrating their solutions
properly gives total solution.
The basic idea is to take small part of large problem at a time and solve it.
Solving small problems is easier than considering whole problem at a time.
The decomposition must be done on logical basis not on physical basis.
Consider an example of banking system mentioned below.
25
Problem decomposition
and partitioning
Banking system
Saving Current
Payroll Leave
account account
Figure: Decomposition of banking system
26
Problem decomposition
and partitioning
As shown in following figure, one of the major functions of bank is to manage the
various types of accounts like saving, current, loan etc.
Within a loan account, there are various types of loans like housing, vehicle etc.
Another major function of bank is to manage their personal resources which cover
payroll, leave and other information.
Note that it is very difficult to understand even whole accounts management as a
single function.
Rather at a time, if we take only housing load problem in hand, we can easily
understand its rules and regulations.
Once you understand all small problems which are results of decomposition,
problem is solved.
In this, example, decomposition is made on basis of functionality of bank.
27
Problem solving methods
Once a problem is understood, next step is to develop a solution which later can
be translated to a program.
We will take here advantage of decomposition made as a first step.
For each decomposed sub problem, a solution is developed in order to simplify the
work.
Solution of each sub problem is then translated to a sub program.
Resulting set of sub programs are then integrated to form a single large program or
software.
There are widely used problem solving methods.
1. Algorithms
2. Flowcharts
28
Problem solving methods
1. Algorithms
An algorithm is a stepwise solution to a problem.
Each step in an algorithm represents a solution to a small problem.
Hence, algorithm gives a sequence of steps which makes complete solution of a
problem in hand.
An algorithm itself is decomposition of problem into small steps which are easily
understandable.
Following are the examples of algorithms.
29
Algorithm – 001
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to display AIM Computer Academy.
Algorithm number : 001
*/
30
Algorithm – 002
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm for the addition of two integer number.
Algorithm number : 002
*/
31
Algorithm – 002
32
Algorithm – 003
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm for the subtraction of two integer number.
Algorithm number : 003
*/
33
Algorithm – 003
34
Algorithm – 004
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm for the multiplication of two integer number.
Algorithm number : 004
*/
35
Algorithm – 004
36
Algorithm – 005
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm for the division of two integer number.
Algorithm number : 005
*/
37
Algorithm – 005
38
Algorithm – 006
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to find area of circle.
Algorithm number : 006
*/
39
Algorithm – 006
40
Algorithm – 007
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to find area of square.
Algorithm number : 007
*/
41
Algorithm – 007
42
Algorithm – 008
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to find area of rectangle.
Algorithm number : 008
*/
43
Algorithm – 008
44
Algorithm – 009
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to find area of triangle.
Algorithm number : 009
*/
45
Algorithm – 009
46
Problem solving methods
2. Flowcharts
A flowchart is a special representation of an algorithm.
As it represents solution in form of picture, it is more easier to understand and
develop.
A main advantage of flowchart is visibility of paths within solution.
Each path (or logical sequence) is clearly visible as arrows are used to represent
flow.
Before developing flowcharts for example problems, different flowchart symbols
must be known.
They are shows in following figure.
47
Problem solving methods
Input / Output
Page break
Process or
Computation Continue
Sub-routine
Arrows
48
Flowchart – 001
Start
Display “AIM
Computer
Academy”
Stop
Figure: Flowchart to display the message
49
Flowchart – 002
Start
Input num1
Input num2
Display ans
Stop
Figure: Flowchart to display the addition of two integer numbers
50
Flowchart – 003
Start
Input num1
Input num2
Display ans
Stop
Figure: Flowchart to display the subtraction of two integer numbers
51
Flowchart – 004
Start
Input num1
Input num2
Display ans
Stop
Figure: Flowchart to display the multiplication of two integer numbers
52
Flowchart – 005
Start
Input num1
Input num2
Display ans
Stop
Figure: Flowchart to display the division of two integer numbers
53
Flowchart – 006
Start
Input radius
Display area
Stop
Figure: Flowchart to find area of circle
54
Flowchart – 007
Start
Input side
Display area
Stop
Figure: Flowchart to find area of square
55
Flowchart – 008
Start
Input length
Input width
Display area
Stop
Figure: Flowchart to find area of rectangle
56
Flowchart – 009
Start
Input breadth
Input height
Display area
Stop
Figure: Flowchart to find area of triangle
57
Algorithm – 010
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to find maximum number from the two numbers.
Algorithm number : 010
*/
58
Algorithm – 010
59
Algorithm – 011
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to find minimum number from the two numbers.
Algorithm number : 011
*/
60
Algorithm – 011
61
Algorithm – 012
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to check whether entered number is positive or
negative.
Algorithm number : 012
*/
62
Algorithm – 012
63
Algorithm – 013
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to check whether entered number is even or odd.
Algorithm number : 013
*/
64
Algorithm – 013
65
Flowchart – 010
Start
Input num1
Input num2
Stop
Figure: Flowchart to find maximum number from two numbers
66
Flowchart – 011
Start
Input num1
Input num2
Stop
Figure: Flowchart to find minimum number from two numbers
67
Flowchart – 012
Start
num <- 0
Input num
Stop
Figure: Flowchart to check whether number is positive or negative
68
Flowchart – 013
Start
num <- 0
Input num
No Is num % Yes
Number is odd Number is even
2=0?
Stop
Figure: Flowchart to check whether number is even or odd
69
Algorithm – 014
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to find maximum number from the three numbers.
Algorithm number : 014
*/
70
Algorithm – 014
71
Algorithm – 014
Else
Go to Step-09
End if
Step 06: [Check for maximum number]
If n2 > n3 Then
Go to Step-08
Else
Go to Step-09
End if
Step 07: [Display the output]
Display "First number is maximum"
Go to Step-10
Step 08: [Display the output]
72
Algorithm – 014
73
Algorithm – 015
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to find minimum number from the three numbers.
Algorithm number : 015
*/
74
Algorithm – 015
75
Algorithm – 015
Else
Go to Step-09
End if
Step 06: [Check for minimum number]
If n2 < n3 Then
Go to Step-08
Else
Go to Step-09
End if
Step 07: [Display the output]
Display "First number is minimum"
Go to Step-10
Step 08: [Display the output]
76
Algorithm – 015
77
Flowchart – 014
Start
No Is n1 Yes
> n2?
No Is n2 Yes No Is n1 Yes
> n3? > n3?
n3 n2 n3 n1
maximum maximum maximum maximum
78
Flowchart – 015
Start
No Is n1 Yes
< n2?
No Is n2 Yes No Is n1 Yes
< n3? < n3?
n3 n2 n3 n1
minimum minimum minimum minimum
79
Algorithm – 016
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to print the grade of the student according to the
percentage.
Algorithm number : 016
*/
80
Algorithm – 016
Read percentage
Step 04: [Assign grade]
If percentage > 90 Then
grade <- 'A'
Go to step-09
Else
Go to step-05
End if
Step 05: [Assign grade]
If percentage > 80 Then
grade <- 'B'
Go to step-09
Else
81
Algorithm – 016
Go to step-06
End if
Step 06: [Assign grade]
If percentage > 70 Then
grade <- 'C'
Go to step-09
Else
Go to step-07
End if
Step 07: [Assign grade]
If percentage > 60 Then
grade <- 'D'
Go to step-09
82
Algorithm – 016
Else
Go to step-08
End if
Step 08: [Assign grade]
If percentage > 50 Then
grade <- 'E'
Else
grade <- 'F'
End if
Step 09: [Display the grade]
Display "The grade of the student is : ", grade
Step 10: Stop
83
Flowchart – 016
Start
Read percentage
Is
Yes
percentage > grade <- ‘A’ 2
90
No
Is
Yes
percentage > grade <- ‘B’ 2
80
No
1 Figure: Flowchart to display the grade
84
Flowchart – 016
Is
Yes
1 percentage > grade <- ‘C’
70
No
Is
Yes
percentage > grade <- ‘D’
60
No
Is
Yes
percentage > grade <- ‘E’
50
No
grade <- ‘F’ Display grade
85
Algorithm – 017
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to display first 10 natural numbers.
Algorithm number : 017
*/
86
Algorithm – 017
i <- i + 1
End while
Step 04: Stop
87
Algorithm – 018
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to add first 10 natural numbers.
Algorithm number : 018
*/
88
Algorithm – 018
i <- i + 1
End while
Step 04: [Display the output]
Display "Sum of first 10 natural number is : ", sum
Step 05: Stop
89
Algorithm – 019
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to add first 10 even natural numbers.
Algorithm number : 019
*/
90
Algorithm – 019
i <- i + 2
End while
Step 04: [Display the output]
Display "Sum of first 10 even natural number is : ", sum
Step 05: Stop
91
Algorithm – 020
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to add first 10 odd natural numbers.
Algorithm number : 020
*/
92
Algorithm – 020
i <- i + 2
End while
Step 04: [Display the output]
Display "Sum of first 10 odd natural number is : ", sum
Step 05: Stop
93
Algorithm – 021
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to add first 10 numbers entered by user.
Algorithm number : 021
*/
94
Algorithm – 021
Read num
sum <- sum + num
i <- i + 1
End while
Step 04: [Display the output]
Display "Sum of 10 numbers entered by user is : ", sum
Step 05: Stop
95
Algorithm – 022
/*
AIM Computer Academy
Author name : Vishal Koshti
Algorithm : Write an algorithm to print Fibonacci series up to N terms.
Algorithm number : 022
*/
96
Algorithm – 022
97
Flowchart – 017
Start
i <- 1
No
Is i <= 10?
Yes
Display i
i <- i + 1
Stop
Figure: Flowchart to print first 10 natural numbers
98
Flowchart – 018
Start
No
Is i <= 10?
Yes
sum <- sum + i
Display sum
i <- i + 1
Stop
99
Flowchart – 019
Start
No
Is i <= 20?
Yes
sum <- sum + i
Display sum
i <- i + 2
Stop
100
Flowchart – 020
Start
No
Is i <= 19?
Yes
sum <- sum + i
Display sum
i <- i + 2
Stop
101
Flowchart – 021
Start
No
Is i <= 10?
Yes
Input num
Display sum
Stop
Figure: Flowchart to add first 10 numbers entered by user
102
Flowchart – 022
Start
Input terms
N terms
Is i <= No
Stop
terms?
Yes
thirdTerm <- firstTerm + secondTerm
Display thirdTerm