0% found this document useful (0 votes)
22 views70 pages

Chapter One - Introduction To Programming

The document provides an introduction to programming, explaining the fundamentals of computers and programming languages, including low-level and high-level languages. It outlines the software development life cycle (SDLC), detailing phases such as planning, requirements definition, design, implementation, testing, deployment, and maintenance. Additionally, it discusses programming paradigms, emphasizing procedural, structured, and object-oriented programming, along with best practices for program development.

Uploaded by

Jemal
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)
22 views70 pages

Chapter One - Introduction To Programming

The document provides an introduction to programming, explaining the fundamentals of computers and programming languages, including low-level and high-level languages. It outlines the software development life cycle (SDLC), detailing phases such as planning, requirements definition, design, implementation, testing, deployment, and maintenance. Additionally, it discusses programming paradigms, emphasizing procedural, structured, and object-oriented programming, along with best practices for program development.

Uploaded by

Jemal
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

Chapter one

Introduction to Programming
Prepared by : Surafiel H.
Department of Computer Science,
Addis Ababa University
November, 2021
Steven Paul "Steve" Jobs (February 24, 1955 –
October 5, 2011) was an American businessman,
investor and co-founder and CEO of Apple Inc.

Fundamentals of Programming: Comp 2012 and Comp 2042 2


General Introduction to Computer & Programming
• A Computer is an electronic device that accepts data, performs
computations, and makes logical decisions according to instructions that
have been given to it; then produces meaningful information in a form that
is useful to the user.

• In order to solve a given problem, computers must be given the correct


instruction about how they can solve it.

Fundamentals of Programming: Comp 2012 and Comp 2042 3


What is Programming?
• Computer requires programs to function, and a computer programs does
nothing unless its instructions are executed by a CPU.

• The term Computer programs/Software programs/programs/ source code


are the instructions that tells the computer what to do.

• Computer programming (often shortened to programming or coding) is the


process of writing, testing, debugging/troubleshooting, and maintaining the
source code of computer programs.

Fundamentals of Programming: Comp 2012 and Comp 2042 4


Cont’d
• A Computer program/source code is often written by professionals
known as Computer Programmers (simply Programmers).

• Writing computer programs means writing instructions that will make


the computer follow and run a program based on those instructions.

• Source code is written in one of programming languages.

Fundamentals of Programming: Comp 2012 and Comp 2042 5


Programming Languages

• A programming language is an artificial language that can be used to


control the behavior of a machine, particularly a computer.

• The Primary purpose of programming languages is to provide


instructions to a computer.

Fundamentals of Programming: Comp 2012 and Comp 2042 6


Cont’d
• Programming languages, like natural language (such as Amharic, Afaan Oromo,
and English), are defined by syntactic and semantic rules which describe their
structure and meaning respectively.

• The syntax of a language describes the possible combinations of symbols that


form a syntactically correct program.

• The meaning given to a combination of symbols is handled by semantics.

Fundamentals of Programming: Comp 2012 and Comp 2042 7


Categories of Programming Language

• Available programming languages come in a variety of forms and types.

• Thousands of different programming languages have been developed, used, and

discarded.

• Programming languages can be divided in to two major categories: low-level

and high-level languages.

Fundamentals of Programming: Comp 2012 and Comp 2042 8


Low-level Programming languages
• Machine specific or dependent.

• Computers only understand directly one language and that is called binary
language (the language of 1’s and 0’s).

• In the initial years of computer programming, all the instructions were given
in binary form.

• Binary language is also known as Machine language.

Fundamentals of Programming: Comp 2012 and Comp 2042 9


Cont’d
• Machine languages allow the programmer to interact directly with the hardware, and it can
be executed by the computer without the need for a translator.

• Machine language written code (program) execute very quickly and use memory very
efficiently.
• Difficult to write application with machine languages.

• Although the computer easily understood machine language written codes, it proved too
difficult for a human being to remember all the instructions in the form of 0’s and 1’s.
• Machine languages are also known as the first generation of Programming language.

Fundamentals of Programming: Comp 2012 and Comp 2042 10


Cont’d
• Although the computer easily understood machine language written codes, it proved
too difficult for a normal human being to remember all the instructions in the form of
0’s and 1’s.

• Therefore, computers remained mystery to a common person until other languages


such as assembly language was developed, which were easier to learn and
understand.

• Assembly language is nothing more than a symbolic representation of machine code,


which allows symbolic designation of memory locations.
• Assembly languages are also unique to a specific computer (machine).

Fundamentals of Programming: Comp 2012 and Comp 2042 11


Cont’d
• Assembly language correspondences symbolic instructions and executable machine
codes and was created to use letters (called mnemonics) to each machine language
instructions to make it easier to remember or write.
 For example, ADD A, B
– > Adds two numbers in a memory location A and B.
• However, no matter how close assembly language is to machine code, computers
still can’t understand it directly.
• The assembly language must be translated to machine code by a separate
system program called assembler.
• The machine instruction created by the assembler from the original program
(source code) is called object code.
Fundamentals of Programming: Comp 2012 and Comp 2042 12
Cont’d
• Assemblers are written for each unique machine language. Therefore,
programs written in assembly language are not portable.
• i.e., different computer architectures have their own machine and assembly languages.

• Assembly languages are highly used in system software development, such as


operating systems.

• The assembly language is referred to as the second generation of


programming language.

Fundamentals of Programming: Comp 2012 and Comp 2042 13


High-Level Programming Languages
• The lack of portability between different computers led to the development of high-
level languages.

• They permitted a programmer to ignore many low-level details of the computer's


hardware.
• Allow programmers to write instructions in a language that is easier to understand
than low-level languages.
• Uses English like instructions and programmers are able to define variables with
statements such as:
Sum = X + Y

Fundamentals of Programming: Comp 2012 and Comp 2042 14


Cont’d
• High-level programming languages are also known as the third generation of
programming language.

• FORTRAN, BASIC, PASCAL, C, C++, C#, JAVA, Python and PHP are
some of the examples of high-level programming languages.

• High-level languages also require translation to machine language before


execution.

• The translation is accomplished by either a compiler or an interpreter.

Fundamentals of Programming: Comp 2012 and Comp 2042 15


Translators
• Compilers translate the entire source code/program before execution.

• Interpreters translate source code/programs one line at a time.

Fundamentals of Programming: Comp 2012 and Comp 2042 16


Key differences between a compiler and an interpreter:
• Compiler transforms code written in a high-level programming language into
the machine code, at once, before program runs, whereas an Interpreter coverts
each high-level program statement, one by one, into the machine code, during
program run.
• Compiled code runs faster while interpreted code runs slower.
• Compiler displays all errors after compilation, on the other hand, the
Interpreter displays errors of each line one by one.
• Compiler is based on translation linking-loading model, whereas Interpreter is
based on Interpretation Method.
• Interpreters are more interactive than compilers.

Fundamentals of Programming: Comp 2012 and Comp 2042 17


Programming Paradigm
• It's the most fundamental way to characterize (categorize) programming
languages.

• It’s a way of classifying programming languages based on their features or


properties.

• It provides the programmer's view of code execution.

• The most influential programming paradigms are Procedural, Structural, and


Object-Oriented programming paradigms.

Fundamentals of Programming: Comp 2012 and Comp 2042 18


Procedural Programming Languages
• Specifies a list of operations that the program must complete to reach the
desired state.

• Each program has a starting state, a list of operations to complete, and an


ending point.

• This approach is also known as imperative programming


• Procedural programming is based upon the concept of procedure call.

Fundamentals of Programming: Comp 2012 and Comp 2042 19


Cont’d
• Procedures, also known as functions, subroutines, or methods, are small
sections of code that perform a particular function.

• A procedure is effectively a list of computations to be carried out.

• It allows a section of code to be re-used in the program without making


multiple copies.

• Procedural Programming makes it easier for programmers to understand and


maintain the program structure.

• Two of the most popular procedural programming languages are FORTRAN


and BASIC.
Fundamentals of Programming: Comp 2012 and Comp 2042 20
Structured Programming Languages
• Is a special type of procedural programming.

• Requires that programmers break program structure into small pieces of code
that are easily understood.

• It makes it easier to control a program's development and to design and test its
individual component parts.

• It is often associated with a "top-down" approach to design.


Fundamentals of Programming: Comp 2012 and Comp 2042 21
Cont’d
• It is most famous for removing or reducing reliance on the GO TO statement.

• Structured programs are built up from units called modules, which normally
correspond to single procedures or functions.

• The most popular structured programming languages include C and Pascal.

Fundamentals of Programming: Comp 2012 and Comp 2042 22


Object-Oriented Programming Language

• OOP is composed of a collection of individual units, or objects as opposed to


traditional view in which a program is a list of instructions to the computer.

• In OOP programs, the designer specifies both the data structures and the types
of operations that can be applied to those data structures.

• This pairing of a piece of data with the operations that can be performed on it
is known as an object.

Fundamentals of Programming: Comp 2012 and Comp 2042 23


Cont’d

• OOP is claimed to give more flexibility, easing changes to programs.

• The OOP approach is often simpler to develop and maintain.

• Significant object-oriented languages include:


 C++, Java, C#, Python, R, PHP, JavaScript, etc.

Fundamentals of Programming: Comp 2012 and Comp 2042 24


Software Development Life Cycle (SDLC)
• Software is a collection of programs and routines that support the operations of
performing a task using a computer.
• The method used in solving problems in Computer Science or Software
Engineering is known as Software (System) Development Life Cycle (SDLC).
• It’s a framework defining tasks performed at each step in the software
development life process.
• In software development project management, the SDLC is a conceptual model
or structure that the software development team follows.
• It’s the application of standard business practices to building software
applications.
Fundamentals of Programming: Comp 2012 and Comp 2042 25
Cont’d

Fundamentals of Programming: Comp 2012 and Comp 2042 26


1. Planning
• In the Planning phase, project leaders evaluate the terms of the project.

• This includes calculating labor and material costs, creating a timetable with
target goals, and creating the project’s teams and leadership structure.

• Planning should clearly define the scope and purpose of the application.

• Planning can also include feedback from stakeholders.

• Stakeholders are anyone who stands to benefit from the software/application.

Fundamentals of Programming: Comp 2012 and Comp 2042 27


2. Define Requirements
• It includes the scope of work necessary to define, analyze and document business
and end-user requirements.
• Defining requirements is considered part of planning to determine what the
application is supposed to do and its requirements.
• For example, a social media application would require the ability to connect with a friend.
An inventory program might require a search feature.
• Requirements also include defining the resources needed to build the project.
• For example, a team might develop software to control a custom manufacturing machine.
The machine is a requirement in the process.

Fundamentals of Programming: Comp 2012 and Comp 2042 28


3. Design and Prototyping
• The Design phase models the way a software application will work.
• Some aspects of the design include:
• Architecture: Specifies programming language, industry practices, overall design, and use of any
templates or boilerplate.
• User Interface: Defines the ways customers/users interact with the software, and how the
software responds to input.
• Platforms: Defines the platforms on which the software will run, such as Apple, Android,
Windows version, Linux, or even gaming consoles.
• Programming: Not just the programming language, but including methods of solving problems
and performing tasks in the application.
• Communications: Defines the methods that the application can communicate with other assets,
such as a central server or other instances of the application.
• Security: Defines the measures taken to secure the application, and may include SSL traffic
encryption, password protection, and secure storage of user credentials.

Fundamentals of Programming: Comp 2012 and Comp 2042 29


Cont’d
• Prototyping can be a part of the design phase.

• It demonstrates a basic idea of how the application looks and works.

• This “hands-on” design can be shown to stakeholders/customers/users.

• Use feedback to improve the application.

• It’s less expensive to change the prototype phase than to rewrite code to make a
change in the development phase.

Fundamentals of Programming: Comp 2012 and Comp 2042 30


4. Software Development/Implementation
• This is the actual writing of the program.
• It’s the construction of the new system and its delivery into production or
day-to-day operation.
• This SDLC phase may necessitate much more than programming or writing
code, such as database creation and populating, network installation and
testing.
• The developer team also needs to make sure the users/customers are taken
care of with effective training and documentation.

Fundamentals of Programming: Comp 2012 and Comp 2042 31


5. Testing
• It’s critical to test an application before making it available to users.
• Testing should ensure that each function works correctly.
• Different parts of the application should also be tested to work seamlessly
together.
• A performance test of the developed system has to be performed to reduce
any hangs or lags in processing.
• The testing phase helps reduce the number of bugs and glitches that users
encounter.
• This leads to a higher user satisfaction and a better usage rate.

Fundamentals of Programming: Comp 2012 and Comp 2042 32


6. Deployment
• In the deployment phase, the application is made available to users.
• At this stage, the goal is to deploy the software to a business/production
environment for customers to start using and consuming;
• Deployment can also be complex.
• Upgrading a company-wide database to a newly-developed application is one example. Because
there are several other systems used by the database, integrating the upgrade can take more time
and effort.

Fundamentals of Programming: Comp 2012 and Comp 2042 33


7. Operations and Maintenance
• At this stage, the development cycle is almost finished and the
software/application is done and being used in the field.
• In this phase, users discover bugs that weren’t found during testing.
• These errors need to be resolved, which can spawn new development
cycles.
• In general, this phase could include bug fixes, upgrading security
protocols, updating features and specifications, among many others.

Fundamentals of Programming: Comp 2012 and Comp 2042 34


Basic Computer Program Development Tips
• The program we design in any programming language need to be:
 Reliable:
 The program should always do what it is expected to do and handle all types of exception.
 Maintainable:
 The program should be in a way that it could be modified and upgraded when the need arises.
 Portable:
 It needs to be possible to adapt the software written for one type of computer to another with
minimum modification.
 Efficient:
 The program should be designed to make optimal use of time, space and other resources of
the computer.

Fundamentals of Programming: Comp 2012 and Comp 2042 35


Which side you are?

Fundamentals of Programming: Comp 2012 and Comp 2042 36


Problem, Solution and Problem-Solving

• A problem is the state of situation that needs to be resolved.

• It is something that we are not happy or satisfied about it.

• A solution is an idea or artifact that removes the problematic situation.

• Problem-solving is a process to derive a solution to a problem.

Fundamentals of Programming: Comp 2012 and Comp 2042 37


Problem Solving Techniques
• In computer programming two facts are given more weight:

 The first part focuses on defining the problem and logical procedures to
follow in solving it.

 The second one introduces the means of how programmers


communicate those procedures to the computer system so that it can be
executed.

Fundamentals of Programming: Comp 2012 and Comp 2042 38


Cont’d
• Any computing problem can be solved by executing a series of actions in a
specific order.

• A procedure for solving a problem in terms of:


 The actions to execute and

 The order in which these actions execute.


} Algorithm

Fundamentals of Programming: Comp 2012 and Comp 2042 39


Algorithm
• Simply, algorithm is a sequence of instructions.
• An algorithm is a step-by-step sequence of instructions that must terminate and
describe how the data is to be processed to produce the desired outputs.
• An algorithm is defined as a well-ordered collection of unambiguous and
effectively computable operations, that when executed, produces a result and
halts in a finite amount of time.
• Generally, an algorithm is defined as a finite set of well-defined instructions for
accomplishing some task which, given an initial state, will terminate in a
corresponding recognizable end-state.

Fundamentals of Programming: Comp 2012 and Comp 2042 40


Program vs Algorithm
• A program is written in one of the programming languages, whereas an
algorithm is in a natural language such as English using pseudo code or
flowchart.

• When an algorithm is coded/written using any programming language (e.g.


C++), then it is called a program.

Fundamentals of Programming: Comp 2012 and Comp 2042 41


Algorithm Development and Representation
• Computer solves varieties of problems that can be expressed in a finite number
of steps leading to a precisely defined goal by writing different programs.

• Before a program is written, the programmer must clearly understand:


1. What data are to be used,

2. The desired result, and

3. The procedure to be used to produce the result.

Fundamentals of Programming: Comp 2012 and Comp 2042 42


Characterstics of Algorithm
• An algorithm should have the following characteristics:
1. Unambiguous: an algorithm should be clear and unambiguous.
 Each of its steps (or phases), and their inputs/outputs should be clear and must lead to only one meaning.

2. Input: An algorithm should have ZERO or more well-defined inputs.

3. Output: An algorithm should have ONE or more well-defined outputs, and should match the
desired output.

4. Finiteness: Algorithms must terminate after a finite number of steps.

5. Feasibility: an algorithm should be feasible with the available resources.

6. Independent: an algorithm should be independent of any programming language.

Fundamentals of Programming: Comp 2012 and Comp 2042 43


Basic Steps in Writing Algorithm

1. Define the problem: State the problem you are trying to solve in clear and
concise terms.

2. Analyze the problem to identify the inputs, the outputs and the process that
convert the input to the output.

3. Describe the steps needed to convert or manipulate the inputs to produce the
outputs (Write the algorithm).

4. Test the algorithm: choose data sets and verify that your algorithm works!

Fundamentals of Programming: Comp 2012 and Comp 2042 44


Basic Algorithm Constructs
• All algorithm are constructed using three control structure or
construct. These are:-
1. Sequence structure: is the construct where one statement is executed after
another.

2. Selection structure: is the construct where statements can executed or


skipped depending on whether a condition evaluates to TRUE or FALSE.

3. Repetition structure: is the construct where statements can be executed


repeatedly until a condition evaluates to TRUE or FALSE.

Fundamentals of Programming: Comp 2012 and Comp 2042 45


Algorithm Languages/tools
• The two most commonly used tools to help document the algorithm (or
program logic) are:

1. Pseudocode, and

2. Flowcharts

Fundamentals of Programming: Comp 2012 and Comp 2042 46


Pseudocode
• Derived from pseudo and code.
• It’s a compact and informal high-level description of an algorithm.
• Pseudocode consists of natural language-like statements that precisely describe
the steps of an algorithm or a program.
• Uses the structural conventions of programming languages.
• It typically omits details such as subroutines, variables declarations and
system-specific syntax.
• The programming language is augmented with natural language descriptions of
the details, where convenient, or with compact mathematical notation.

Fundamentals of Programming: Comp 2012 and Comp 2042 47


Cont’d
• The purpose of using pseudocode is that it may be easier for humans to read
than conventional programming languages.

• No standard for pseudocode syntax exists, as a program in pseudocode is not an


executable program.

• As the name suggests, pseudocode generally does not actually obey the syntax
rules of any particular language;

• There is no systematic standard form, although any particular writer will


generally borrow the appearance of a particular language.
Fundamentals of Programming: Comp 2012 and Comp 2042 48
Pseudocode Constructs
1. Input/output:
Input: GET var1, var2, ……. Enter var 1, var2, …… or Accept var1, var2 or
READ var1, var2, ………….
Output: DISPLAY var1, var2 or PRINT var1, var2

2. Computation/Assignment:
• Compute var1 as Sum of X and Y or var1 = X + Y
• ASSIGN X to Y or Y to X
• INCREMENT counter by 1 or counter + 1
• DECREMENT counter by 1 or counter -1

Fundamentals of Programming: Comp 2012 and Comp 2042 49


Cont’d
3. Selection:
a. Single-Selection IF:
IF condition THEN
statement 1
statement2
etc
END IF

• Statement 1, statement 2, etc. are executed if the condition is evaluated true


otherwise they are skipped.

Fundamentals of Programming: Comp 2012 and Comp 2042 50


Cont’d
b. Two-Way-Selection IF:
IF condition THEN
statement 1
statement2
etc
ELSE
statement 1
statement2
etc
END IF
• Statement 1, statement 2, etc. under the IF are executed if the condition is evaluated True
otherwise Statement 1, statement 2, etc. under the ELSE is executed.
Fundamentals of Programming: Comp 2012 and Comp 2042 51
Cont’d
C. Multiple-Selection IF:
• The first condition is evaluated. If it is true, the statements
IF condition THEN
statement 1 under that condition are executed; otherwise, these statements
statement2 are skipped and the second condition is evaluated; if it is true,
etc
the statements under that condition will be executed;
ELSE IF condition THEN
statement 1 otherwise, these statements are skipped and so on. If none of
statement2 the conditions are evaluated to be true, the statements under
etc "ELSE" will be executed.
….
• Only the statements under one of the conditions or the ELSE
…..
ELSE condition will be executed.
statement 1 • The ELSE IF and ELSE parts are optional.
statement2
etc
END IF
.

Fundamentals of Programming: Comp 2012 and Comp 2042 52


Cont’d
d. Multiple-Selection Switch:
SWITCH expression TO • Expression is evaluated. Depending on the value of the
case 1: expression, control is transferred to one of the cases or the
statement 1
statement 2 default case. If the value is 1, the statements under the label
etc "case 1" will be executed. If it is 2, the statements under
case 2 :
statement 1 case 2 will be executed, and so on. If the expression matches
statement 2 none of the cases, the statements under default will be
etc
…. executed. Only the statements under one of the cases or the
default: default are executed.
statement 1
statement 2 • The expression value and the case numbers must be
etc integers.
END SWITCH • The default case is optional.

Fundamentals of Programming: Comp 2012 and Comp 2042 53


Cont’d
4. Repetition:

• Sometimes there are conditions in which it is necessary to execute a group of


statements repeatedly until some condition is satisfied.

• This condition is called a loop.

• Loop is a sequence of instructions, which is repeated until some specific condition


occurs.

Fundamentals of Programming: Comp 2012 and Comp 2042 54


Cont’d
• A loop normally consists of four parts, and these are:

 Initialization: - Setting of variables of the computation to their initial values and setting
the counter for determining to exit from the loop.

 Computation: - Processing.

 Test: - Every loop must have some way of exiting from it or else the program would
endlessly remain in a loop.

 Increment/Decrement: - Re-initialization of the loop for the next loop.

Fundamentals of Programming: Comp 2012 and Comp 2042 55


Cont’d
a. While Structure:
WHILE condition
statement 1
statement 2
etc.
END WHILE

• while condition is true, then do subordinate statements.

Fundamentals of Programming: Comp 2012 and Comp 2042 56


Cont’d
b. Do-While Structure:
DO
statement 1
statement 2
etc.
WHILE condition

• The DO – WHILE structure is like a WHILE loop, but tests conditions at the end
of the loop. Thus, statements in the structure will always be executed at least
once.

Fundamentals of Programming: Comp 2012 and Comp 2042 57


Cont’d
c. FOR Loop Structure:

• The FOR loop structure is a specialized version of the WHILE loop for repeating the execution of
statements a specific number of times.

FOR Variable_Name = Initial_value TO Terminating_value


statement 1

statement 2

etc

END FOR

• The statements under the for loop will be executed for every iteration of the Variable_Name
starting from the Initial_value to the Terminating_value.

Fundamentals of Programming: Comp 2012 and Comp 2042 58


Cont’d
• Example: write an algorithm for a program that obtains two integer numbers
from the user and prints out the sum of those numbers.
• Pseudocode:
1. Start the program

2. Prompt the user to enter the first integer num1

3. Prompt the user to enter a second integer num2

4. Compute the sum of the two user inputs as sum= num1 + num2

5. Display an output prompt that explains the answer as the sum

6. End the program

Fundamentals of Programming: Comp 2012 and Comp 2042 59


Flowchart
• A flowchart is a schematic representation of an algorithm or a process.

• It doesn’t depend on any particular programming language and can be used to

translate an algorithm to more than one programming language.

• It uses different symbols (geometrical shapes) to represent different processes.

Fundamentals of Programming: Comp 2012 and Comp 2042 60


The following table shows some of the common symbols used in a flowchart:

Fundamentals of Programming: Comp 2012 and Comp 2042 61


• Example 1: Draw a flow chart to add two numbers given by the user.

Fundamentals of Programming: Comp 2012 and Comp 2042 62


Example 2: Write a pseudocode and draw a flow chart to check a given number is
negative or not.
• Pseudocode:
1. Start the program
2. Read an integer n
3. If n is greater than zero display a message n is positive
3.1 Else if n is less than zero display a message n is negative
3.2 Else display a message n is neither positive nor negative
3.3 End if
4. End/Stop the program.
Fundamentals of Programming: Comp 2012 and Comp 2042 63
Fundamentals of Programming: Comp 2012 and Comp 2042 64
• Example 3: Write the a pseudocode and draw a flow chart to find the following
sum.
Sum = 1+2+3+…. + 50
• Pseudocode:
1. Start the program
2. Initialize sum to 0 and counter to 1
2.1 If the counter is less than or equal to 50
2.1.1 Add counter to sum
2.1.2 Increase counter by 1
2.1.3 Repeat step 2.1 until the counter is greater than 50
2.2 Else end the program
Fundamentals of Programming: Comp 2012 and Comp 2042 65
Fundamentals of Programming: Comp 2012 and Comp 2042 66
Exercises
1. Write an algorithm that calculates and display the area and perimeter of a
rectangle. The width and the length given by the user.
2. Write a pseudo code and draw a flow chart that calculates and display the
volume of a box. The length, width, and height values are given by the user.
3. Write an algorithm that calculates class average of n students for math's score
where n is supplied by the user.
4. Write an algorithm that determines the square root of a number.
5. Write the pseudocode and draw a flow chart to find whether a given number is
odd or even.
6. Write the pseudocode and draw a flow chart to find the sum of even numbers
between 1 to 100.
Fundamentals of Programming: Comp 2012 and Comp 2042 67
Exercises (Cont’d)
7. Write the pseudocode and draw a flow chart to find the average of two given
integer values.
8. Write the pseudocode and draw a flow chart maximum, minimum, and sum of
three numbers given by the user.
9. It has been decided that a bonus of 12% of gross salary is to be given for each
employee in an organization. It was also agreed that if an employee has
worked for more than 13 years she/he is to receive an additional amount of
ETB 1050.00. Write an algorithm that calculate and display the bonus and the
net salary.
10. Write an algorithm that chooses a random number between 1 and 20 and allow
the user to guess the number. The user is allowed only five trials. The
algorithm should display guess result (success/failure) and the number of trial.
Fundamentals of Programming: Comp 2012 and Comp 2042 68
Exercises (Cont’d)
11. Write an algorithm that determining the monthly income of a salesperson by
using the following commission.

Monthly Sales Income


Greater than or equal to $50,000. $375 plus 16% of sales
Less than $50,000 but greater than or equal to $40,000 $350 plus 14% of sales
Less than $40,000 but greater than or equal to $30,000 $325 plus 12% of sales
Less than $30,000 but greater than or equal to $20,000 $300 plus 9% of sales
Less than $20,000 but greater than or equal to $10,000 $250 plus 5% of sales
Less than $10,000 $200 plus 3% of sales

Fundamentals of Programming: Comp 2012 and Comp 2042 69


Fundamentals of Programming: Comp 2012 and Comp 2042 70

You might also like