Professional Documents
Culture Documents
Basic Programming Concept
Basic Programming Concept
Concept
1
Lesson Plan
Title: Basic Programming Concept Ref. No: Lecture
14, 15, 16
Target Population: CSC 103 Duration: 180 min
Aims: To introduce students to different programming concepts such as
algorithm, flow chart, variables, data types in C and program
Learning
structure outcomes: At the end of the lecture, the students will be able to
understand the basics and usefulness of algorithm, analyze various
algorithms, create flowcharts, have a knowledge of variables and data types.
3
Introduction
Computer programs are collections of
instructions that tell a computer how to
interact with the user, interact with the
computer hardware and process data. The
first programmable computers required the
programmers to write explicit (clear &
exact) instructions to directly manipulate the
hardware of the computer.
4
Computer Programs
Computer programs, known as software,
are instructions to the computer. You tell a
computer what to do through programs.
Without programs, a computer is an empty
machine.
Computers do not understand human
languages, so you need to use computer
languages to communicate with them.
Programming Languages
Programs are written using programming
languages.
Programming language is an agreed upon
format of symbols that allow a programmer to
instruct a computer to perform certain predefined
tasks.
Programming languages are what software
developers use to create computer
software, though they are themselves pieces of
software too.
Types of programming languages
Machine languages
Assembly languages
High-level languages
Machine language
The most basic (called low-level) computer
language is the machine
language that uses binary ('1' and '0') code which
a computer can execute (run) very fast.
Programming language that can be understood
and obeyed by a computer without conversion
(translation).
language
3.Maintenance Phase
Maintain/modify to meet changed requirements or
to correct errors.
19
Pre-Programming Phase
This phase requires four steps:
Analyzing the problem.
Developing the Input-Process-
Output (IPO) Chart.
Writing the algorithm.
Drawing the flowchart.
Analyzing the problem
+ for Addition
- for Subtraction
* for Multiplication
/ for Division and
for assignment. For example
A X*3 means A will have a value of
X*3.
The essential properties of
Algorithm are:
It should be simple
It should be clear with no ambiguity
It should lead to a unique solution of the problem.
It should involve a finite number of steps to arrive
at a solution.
It should have the capability to handle some-
unexpected situations.
Steps to Algorithm
1.First produce a general algorithm (one can
use pseudocode or Program Design
Language)
2.Refine the algorithm successively to get step
by step detailed algorithm that is very close to
a computer language.
Algorithm
Step1. Start.
Step2. Take water in pan
Step3. Boil the water
Step4. Add sugar and Tea leaves
Step5. Add milk
Step6. Boil it
Step7. Pour it into cup
Step8. Stop
Examples of Algorithm
Algorithm:
Step1: Start
Step2: Read\input the Radius r of the Circle
Step3: Area PI*r*r // calculation of area
Step4: Print Area
Step5: Stop
Problem 3: Write an algorithm and draw a flow chart
to read two numbers and find their sum.
Algorithm:
Step 1 : Start
Step 2 : Input first number A
Step 3 : Input second number B
Step 4 : Total = A+B
Step 5 : Print Total
Step 6 : Stop
Flow chart
Problem 3: Convert temperature Fahrenheit to
Celsius
Algorithm:
Step1: Start
Step 2: Read Temperature in Fahrenheit F
Step 3: C = 5*(F - 32)/9
Step 4: Print Temperature in Celsius C
Step5: End
Problem: Algorithm and a flow chart to
calculate area of square.
Algorithm
Step 1 : Start
Step 2 : Read value for a side A
Step 3 : [Compute] Area = A * A
Step 4 : Print Area
Step 5 : Stop
Flow Chart
Problem: Algorithm and flow chart to find
the average of three numbers.
Algorithm
Step1 : Start
Step 2 : Enter Three Numbers A, B and C
Step 3 : Compute Average = (A+B+C)/3
Step 4 : Print Average
Step 5 : Stop
Flow Chart
,C
Problem: Algorithm and flow chart to find
the largest of two numbers.
Algorithm
Step1: Start
Step 2: Enter two numbers A and B
Step 3: Check if A is greater than B if yes go to Step 4 else
go to Step 5
Step 4: Print A is greater than B
Step 5: Check if B is greater than A if yes go to Step 6 else
go to Step 7
Step 6: Print B is greater than A
Step 7: Print A is equal to B
Step 8: Stop
Flow Chart
Problem: Write algorithm and draw
flowchart to “Find the Greater of two
Numbers”
Step1. Start.
Step2. Input the two numbers
A and B.
Step3. Check if A>B. If yes,
go to step 4
else go to step 6
Step4. Display the number A
Step5. Go to step 7
Step6. Display the number B
Step7. Stop
3. Write algorithm and draw flowchart to
“Find the Area of a Rectangle”
Algorithm
Step1. Start.
Step2. Take the length (l) and breadth (b)
Step3. Find the Area as l x b
Step4. Give the result.
Step5. Stop
Write an algorithm to determine a student’s final grade
and indicate whether it is passing or failing. The final
grade is calculated as the average of four marks.
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “”FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<5
0
PRINT PRINT
“PASS” “FAIL”
STOP
Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
Flowchart
Algorithm START
•Step 1: Input Lft
•Step 2: Lcm Lft x 30 Input
Lft
Print
Lcm
STOP
Algorithm and a flow chart to find the
factorial of a number.
Algorithm
Step 1: Start
Step 2: Read N
Step 3: [Initialize all counters] Set FACT= 1, i = 1
Step 4: Compute Fact = Fact * I Increment i
Step 5: Check if i < = N if true repeat step 4 if false
go to step 6
Step 6: Print fact
Step 7: Stop
Program
As a programmer, after problem solving phase, next step
is to code the program-that is, to express our solution in a
programming language. We will translate the logic from
the flowchart or pseudocode-or some other tool-to a
programming language.
Program Structure
Virtually all structured programs share a similar overall
pattern:
68
Rules for writing variable name in C
Variable name can be composed of letters (both uppercase and
lowercase letters), digits and underscore '_' only. Example: Ab_1
The first letter of a variable should be either a letter or an
underscore. Example: num
69
Data Types
There are two types of data type
Built-in data types
Fundamental data types (int, char,
float, double, void, pointer)
Derived data types (array, string,
structure)
Programmer-defined data types
(Structure, Union, Enumeration)
72
Data Types
Data Description Storag Value Example
Type e size range
void Used to denote the type
with no values
int Used to denote an integer 2 bytes -32768 to Example: 10,
type. 32767 -10
{ int age;
age = 20;
}
char Used to denote a character 1 byte -128 to 127 or Example: ‘A’,
type. A single character 0 to 255 ‘b’
such as a letter of the
alphabet or punctuation. {
Character variables are char Letter;
letters of the alphabet, Letter = ‘H’
ASCII characters or
numbers 0-9. }
Data Description Storag Value Example
Type e size range
float Used to denote a floating 4 bytes 1.2E-38 to Example: 2.54,
point type. It stores real or 3.4E+38 -35.05
fractional numbers (also {
called float to indicate a float weight;
floating point number). weight = 65.50;
}
Array A finite sequence of variables of the same data type.
#include <stdio.h>
void main()
{
printf(“Hello World!”);
}
Example program
#include <stdio.h>
void main()
{
int age;
float weight;
char sex;
age = 21;
weight = 51.5;
sex = “M";
printf("I am %d years old\n ", age);
printf(“My weight is %f\n’’, weight);
printf("My sex is %c ", sex);
}
76