0% found this document useful (0 votes)
8 views12 pages

Lecture 1

Uploaded by

h7ussin3
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)
8 views12 pages

Lecture 1

Uploaded by

h7ussin3
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

‫جامعة بغداد‪ /‬كلية التربية للعلوم الصرفة ابن الهيثم‪/‬قسم علوم الحاسبات‬

‫المرحلة االولى‪ /‬نظري‬


‫السنة الدراسية ‪2024-2023‬‬

‫‪Structured Programming‬‬
‫‪Lecture1 – Algorithms‬‬
Algorithm
Algorithm can be defined as: “A sequence of activities to be processed for
getting desired output from a given input”.

Then we can say that:


1. Getting specified output is essential after the algorithm is executed.
2. One will get output only if the algorithm stops after a finite time.
3. Activities in an algorithm to be clearly defined in other words for it to be
unambiguous
Algorithm
Before writing an algorithm for a problem, one should find out what is the:
• The inputs to the algorithm.
• The output after running the algorithm.

While writing algorithms we will use the following symbols for different operations:
• ‘+’ for Addition
• ‘-’ for Subtraction
• ‘*’ for Multiplication
• ‘/’ for Division
• ‘ ’ for assignment.
- For example A X*3 means A will have a value of X*3.
Examples of Algorithm

Example 1: Find the area of a Circle Algorithm:


of radius r. • Step1: Start
• Sol: Inputs to the algorithm: • Step2: Read\input the Radius r
of the Circle
• Radius r of the circle.
• Step3: Input PI 3.14
• Expected output: • Step4: Area PI*r*r //
• Area of the circle Algorithm. calculation of area
• Step5: Print Area
• Step6: End
Examples of Algorithm

• Example 2: Write an algorithm to Algorithm:


read two numbers and find their • Step1: Start
sum. • Step2: Read\input the first
• Sol: Inputs to the algorithm: num1.
• First num1. • Step3: Read\input the second
• Second num2. num2.
• Step4: Sum num1+num2 //
• Expected output:
calculation of sum
• Sum of the two numbers. • Step5: Print Sum
• Step6: End
Types of Algorithms
• The algorithm and flowchart, classification to the three types of control
structures. They are:
• 1. Sequence The sequence structure is the construct where one statement
is executed after another.
• 2. Selection The selection structure is the construct where statements can
be executed or skipped depending on whether a condition evaluates to
TRUE or FALSE.
• There are three selection structures in C:
• 1. IF
• 2. IF – ELSE
• 3. SWITCH
Types of Algorithms
• 3. Loop (Repetition) The repetition structure is the construct where
statements can be executed repeatedly until a condition evaluates to
TRUE or FALSE.
• There are three repetition structures in C:
• 1. WHILE
• 2. DO – WHILE
• 3. FOR
Examples of Algorithm
• Example 3: Write an algorithm to find the greater number between
two numbers
• Sol:
• Step1: Start
• Step2: Read/input A and B
• Step3: If A >= B then C A // A greater than or equal B
• Step4: if B >= A then C B // B greater than or equal A
• Step5: Print C
• Step6: End
Examples of Algorithm
• Example 4: Write an algorithm to find the average of any three
numbers.
• Sol: Step1: Start
• Step2: Read values of X, Y, Z
• Step3: S X + Y + Z
• Step4: A S / 3
• Step5: Print value of A
• Step6: End
Examples of Algorithm
• Example 5: Write an algorithm to find the largest value of any three
numbers.
• Sol:
• Step1: Start
• Step2: Read/input A, B and C
• Step3: If (A>=B) and (A>=C) then Max A
• Step4: If (B>=A) and (B>=C) then Max B
• Step5: If (C>=A) and (C>=B) then Max C
• Step6: Print Max
• Step7: End
Examples of Algorithm
• Example 6: Write an algorithm to calculate and print even numbers
between 0 and 99.
• Sol:
• 1. Start
• 2. I 0
• 3. Print I
• 4. I I+2
• 5. If (I <=98) then go to line 3
• 6. End
Exercises
• Q1: Write an algorithm to calculate even numbers between 9 and
100.
• Q2: Write an algorithm to calculate and print odd numbers between 1
and 95.
• Q3: Write an algorithm to find the sum of 50 numbers.
• Q4: Write an algorithm to find the value of A, B, C.
A=X +6Y
B=2X-A
C=A +XB
• Q5: Write an algorithm to print the series. 2,4,8,16,32………..1024.

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