0% found this document useful (0 votes)
6 views

Unit 1 Introduction to Programmng

This document serves as an introduction to programming, covering key concepts such as computer programs, programming languages, algorithms, and flowcharts. It explains the differences between low-level and high-level programming languages, and provides examples of algorithms and flowcharts for problem-solving. The document emphasizes the importance of understanding syntax, semantics, and the use of pseudocode in programming.

Uploaded by

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

Unit 1 Introduction to Programmng

This document serves as an introduction to programming, covering key concepts such as computer programs, programming languages, algorithms, and flowcharts. It explains the differences between low-level and high-level programming languages, and provides examples of algorithms and flowcharts for problem-solving. The document emphasizes the importance of understanding syntax, semantics, and the use of pseudocode in programming.

Uploaded by

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

UNIT 1 Introduction to Programming

Course Learning Outcomes


[CLO1→C2]: Computer program and programming languages
[CLO2→C3]: Problem-solving using algorithm
[CLO3→C3]: Problem-solving using flowchart

What is a Computer?
A computer is an electronic computing machine used to solve a large variety of problems
related with all the aspects of our life.
It consists of hardware and software. Physical components of the computer are known as
hardware and software means computer programs.

Computer Program
A computer program is a set of instructions written in a programming language to solve a
particular problem and achieve specific results. People learn programming languages to
use the computer as a problem-solving tool. Each statement of a program has syntax and
semantic.

Syntax
Syntax of a programming language defines the rules for writing valid statements in a
program. It is similar to the grammar of a natural language. It determines the correct form
of statements of a computer program.
For example, an assignment statement consists of a variable and an expression separated by
equal sign. This is the syntax and it can be expressed as:
variable = expression; sum = a + b;

Semantic
Semantic gives meaning to correct statements of a programming language. It describes
the sequence of operations to be performed when executing the statements of a program
.
For example, in the assignment statement
sum = a + b;
the semantic of the statement is to perform the expression, that is, add the values stored in
variables a and b and then store the result in variable sum.

M. Sajjad Heder Pg. 1


Programming Languages
Programming languages have been developed to give instructions to a computer to perform
a specific task. In other words, these are used to write computer programs.
Programming languages are classified into two categories, low level and high level
languages.

Low level languages


Low level languages are associated with the architecture of the computer. To understand
a low level language, detailed knowledge of internal working of computer is required.
Low level languages include machine language and assembly language.

 Machine language
Programming language that is directly understood by the computer is known as machine
language.
 It consists of zeroes and ones (0s & 1s).
 It is almost impossible for humans to write programs in machine language.
Therefore, practically no programming is done in machine language. Instead,
assembly languages and high level languages are used.
 Machine language is associated with the architecture of computer. Therefore,
programs written in machine language for one computer will not work on another
because of design difference.

 Assembly Language
Assembly language consists of symbols or abbreviations known as mnemonics. A program
known as assembler is used to translate assembly language program into machine language
before it is executed by the computer. Assembly language is associated with the
architecture of the computer. Therefore, each computer has its own assembly language.
Some examples of mnemonics are given below with their meanings.

Mnemonic Meaning
MUL Perform multiplication
ADD Perform addition
JUMP Perform a jump operation
MOV Move data

 It was developed to make computer programming easier than machine language


 It allows programmers to have access to all the special features of the computer they
are using.

M. Sajjad Heder Pg. 2


 Programs written in assembly language requires less running time than those
prepared in a high level language.
 Assembly language may be the best choice for some applications but its use is
gradually declining.

High Level languages


High level languages are English-oriented languages and they are commonly used for
writing computer programs. These languages use English words such as print, go to, if, end,
etc. Some popular high level language are C, C++, C#, Java, JavaScript, Python, Visual Basic,
etc. A program known as compiler/interpreter is required to translate a high level language
program into machine language. Coding and debugging of a high level language program is
easier than a program written in a low level language.
High level language can be classified into procedural, structured and object-oriented
programming languages.

 Procedural Languages
Procedural language is based upon the concept of modular programming. In modular
programming programs are divided into smaller parts known as modules. Modular
programs consist of one or more modules. A module is a group of statements that
can be executed more than once in a program. Each module performs a specific task.
It is easy to design, modify and debug a program in a procedural language since it
provides better programming facilities.

 Structured Languages
Structured languages consist of three fundamental elements, which are sequence,
selection and repetition.

 Object-oriented Programming Languages


Object-oriented programming refers to a programming method that is based on
objects such as student, vehicle, etc. It provides a set of rules for defining and
managing objects. An object can be considered a thing that can perform a set of
activities.
For example, the object vehicle can be defined as an object that has number of
wheels, number of doors, color, number of seats, etc. The set of activities that can
be performed on this object include, Steer, Accelerate, Brake, etc.
The concept of object-oriented programming provides facilities to design, develop
and easily debug large computer programs.

Problem Solving on the Computer


Solving problems is the main task of computer science which is the job of computer
programmer. Programmer must first understand how a human solves a problem then
translate it into a set of instructions in a programming language that a computer can
understand and execute.

M. Sajjad Heder Pg. 3


Algorithm
Algorithm means method, procedure, technique or plan. Algorithm is a step-by-step
problem solving method that is easy to understand and follow. It is a set of steps that
clearly defines a sequence of operations to solve a problem.
Algorithm plays an important role in computer programming. Computer programming is the
process of taking an algorithm and coding it in a programming language. Creating an
algorithm is the first step for developing a computer program.
Pseudocode
Pseudocode is used for developing computer programs. It is a method of describing the
steps of an algorithm using natural language. It represents algorithm in a form that humans
can easily understand. You don’t have to follow any set-out rules as pseudocode is an
informal way to designing solution of a problem. It is a great way of getting started with
computer programming for beginners.

Algorithms for Solving Problems


An algorithm has a finite number of steps. It may employ the following control structures.

 Sequence
Sequence means that each step of the algorithm is executed in a specified order. The
following algorithm adds two numbers. It performs the steps in a purely sequential
order.
Step 1: Input a number, A
Step 2: Input the second number, B
Step 3: Set SUM = A + B
Step 4: Output SUM
Step 5: End

 Selection (Decision)
Decision control structure is used when the execution of a process depends on the
result of some condition. For example, If x = y then print “EQUAL”. So the general
form of IF construct is:
If condition Then Process
If the condition is true then the Process will be executed.
A decision statement can also be stated in the following manner.

If condition
Then Process1
Else
Process2

M. Sajjad Heder Pg. 4


This form is known as If-Then-Else construct. Here, if the condition is true then
Process1 is executed otherwise Process 2 is executed. This is demonstrated in the
following algorithm.
Step 1: Input first number, A
Step 2: Input second number, B
Step 3: If A = B Then Output “EQUAL” Else Output “NOT EQUAL”
Step 4: End
 Repetition (Loop)
Repetition refers to executing one or more steps for a number of times. It is
demonstrated in the following algorithm that prints the first 10 natural numbers.

Step 1: Initialize, Set K=1


Step 2: Output K
Step 3: Set K = K + 1
Step 4: If K <= 10 Then Goto Step 2 Else Goto Step 5
Step 5 End
Problem 1: Write algorithm to print the area of a triangle when height and base are given.
Step 1: Initialize, Set H=10, B=18
Step 2: Find the Area, A
A=(B*H)/2
Step 3: Output A
Step 4: End
Problem 2: Write algorithm to input two numbers and swap their values and print.
Step 1: Input X and Y
Step 2: Set TEMP=X
Step 3: Set X=Y
Step 4: Set Y=TEMP
Step 5: Output X
Step 6: Output Y
Step 7: End
Problem 3: Write algorithm to print the sum of odd numbers between 1 to 100.
sum = 1 + 3 + 5 + 7 . . . 99
Step 1: Initialize, K=1, SUM=0
Step 2: Add K to SUM

M. Sajjad Heder Pg. 5


SUM=SUM + K
Step 3: Increment K by 2
K=K+2
Step 4: If K<=100 Then Goto Step 2 Else Goto Step 5
Step 5: Output SUM
Step 6 End
Problem 4: Write algorithm to print the following sequence of numbers.
27 24 21 18 15 12 9 6 3 0 -3 -6
Step 1: Initialize, K=27
Step 2: Output K
Step 3: Decrement K by 3
K=K-3
Step 4: Check the value of K
If K >= -6 Then Goto Step 2 Else Goto Step 5
Step 5: End
Problem 5: Write algorithm to find the factorial of a number.
Step 1: Input N
Step 2: Initialize K=1, F=1
Step 3: Calculate the product
F=F*K
Step 4: Increment K by 1
K=K+1
Step 5: Check the value of K
If K<=N Then Goto Step 3 Else Goto Step 6
Step 6: Output F
Step 7: End

Flowchart
Flowchart is a diagrammatic or graphical representation of algorithm. It describes what
operations are required to solve a given problem.

M. Sajjad Heder Pg. 6


Flowchart illustrates the sequence of operations to be performed to solve a problem in the
form of a diagram. Computer programmers draw flowcharts before writing computer
programs. It provides an easy way to analyze and find solutions of problems. Once the
flowchart is drawn, it becomes very easy to write the program in any high level language. It
is very helpful in communicating the problem solving method to other people. It also helps
in finding and removing errors in computer programs.

Steps for Drawing Flowchart


The flowchart developer must determine the following requirements for the given problem
or algorithm before drawing a flowchart. These apply to algorithm as well.
 Input to the flowchart
 Type of processing required
 Decisions/repetitions to be taken if required
 Output to be produced after processing.

Flowchart Symbols
Flowcharts are drawn using standard symbols. These symbols have specific meaning and are
connected by arrows indicating the flow from one step to another. These symbols are given
in the following table.

Symbol Symbol
Symbol Shape Symbol Shape
Description Description

Flow Line Process

Start/Stop Decision

Input/Ouput Connector

Flow Line: It is a line with arrow head used to connect various flowchart symbols and
indicates the flow of control in the flowchart.
Start/Stop Symbol: It is a rounded rectangular shaped symbol. It is used to indicate the start
or end of a flowchart. We can only write the words Start or Stop inside this symbol. A
flowchart can only have one start but it may have many ends.
Input/Output Symbol: Parallelogram represents input or output operations in a flowchart. It
contains the word READ or INPUT along with the variables for input operation or PRINT or
OUTPUT along with the output data for output operation.

M. Sajjad Heder Pg. 7


Process Symbol: A rectangular block is used for any data processing operation. All the
calculations appear inside the processing symbol, such as, “SUM = A + B”. Variables are also
initialized inside this symbol, such as, “K = 1”.
Decision Symbol: A diamond shaped symbol represents decision in a flowchart and it
contains a condition. If the condition is true, the path marked YES is to be followed. If the
condition if false, the path marked NO is to be followed. The words TRUE or FALSE can also
be used instead of YES or NO.
Connector Symbol: This symbol is used to connect one part of a flowchart to the other.

Flowcharts to Solve Problems


Problem 1: Draw flowchart to find the sum, product and average of three numbers.

START

INPUT A,B,C

SUM=A+B+C

PROD=A*B*C

AVG=SUM/3

OUTPUT SUM,PROD,AVG

STOP

Problem 2: Draw flowchart to input the length of one side of cube and print its volume.

START

INPUT L

V = L*L*L

OUTPUT V

STOP

M. Sajjad Heder Pg. 8


Problem 3: Draw flowchart to print odd numbers between 1 to 100.

START

K=1

OUTPUT K

K=K+2

YES
K < 100 ?

NO

STOP

Problem 4: Draw flowchart to print sum of even numbers from 2 to 100.


sum = 2 + 4 + 6 + 8 + . . . + 100

START

SUM=0, K=2

SUM=SUM+K

K=K+2

YES
K ≤ 100 ?

NO

OUTPUT SUM

STOP

M. Sajjad Heder Pg. 9


Problem 5: Draw flowchart that inputs two number, X and Y and swaps them.

START

INPUT X, Y

TEMP=X

X=Y

Y=TEMP

OUTPUT X, Y

STOP

Problem 6: Draw flowchart to convert temperature from Fahrenheit to Celsius.

START

INPUT F

C = 5/9(F-32)

OUTPUT C

STOP

M. Sajjad Heder Pg. 10


Problem 7: Draw flowchart that reads marks (M) and prints the letter grade as given
below.

Marks Letter Grade


M≥90 and M≤100 A
M≥80 and M˂90 B
M≥70 and M˂80 C
M≥60 and M˂70 D
M≥50 and M˂60 E
M˂50 F

START

INPUT M

YES
M ≥ 90 ? OUTPUT
YES“A”

NO
YES
M ≥ 80 ? OUTPUT “B”

NO
YES
M ≥ 70 ? OUTPUT “C”

NO
STOP
YES
M ≥ 60 ? OUTPUT “D”

NO
YES
M ≥ 50 ? OUTPUT “E”

NO
OUTPUT “F”

M. Sajjad Heder Pg. 11


Problem 8: Draw flowchart to find the factorial of a number.

START

INPUT N

F=1, K=1

F=F*K

K=K+1

YES
K≤N?

NO

OUTPUT F

STOP

M. Sajjad Heder Pg. 12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy