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

Algorithms and Pseudocode Examples

The document outlines algorithms, pseudocode, and flowcharts for ten different programming tasks, including calculating the sum and average of numbers, finding the largest number, computing factorials, checking for odd/even numbers, solving quadratic equations, and determining prime numbers. Each task is presented with a clear step-by-step algorithm, corresponding pseudocode, and flowchart. This structured approach aids in understanding basic programming concepts and problem-solving techniques.

Uploaded by

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

Algorithms and Pseudocode Examples

The document outlines algorithms, pseudocode, and flowcharts for ten different programming tasks, including calculating the sum and average of numbers, finding the largest number, computing factorials, checking for odd/even numbers, solving quadratic equations, and determining prime numbers. Each task is presented with a clear step-by-step algorithm, corresponding pseudocode, and flowchart. This structured approach aids in understanding basic programming concepts and problem-solving techniques.

Uploaded by

amyy12001
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Algorithm ,

Pseudocode &
Flowchart for a
program to find :

1) Sum of two numbers


Algorithm

1. Start
[Link] two numbers, a and b.
[Link] the sum: sum = a + b.
[Link] the result.
[Link]

Pseudocode

START
INPUT a, b
SET sum = a + b
OUTPUT sum
STOP
Flowchart
2) Average of three
numbers

Algorithm
1. Start
2. Input three numbers, a, b and c

3. Calculate the average: avg =


(a + b + c) / 3
4. Output the result
5. Stop

Pseudocode
START
INPUT a, b, c
SET avg = (a + b + c) / 3
OUTPUT avg
STOP
Flowchart
3) Largest of three
numbers

Algorithm
1. Start
2. Input three numbers, a, b, and c
3. Compare a, b, and c:
- If a > b and a > c, then
largest = a
- Else if b > c, then largest = b

- Else largest = c
4. Output the largest number
5. Stop

Pseudocode
START
INPUT a, b, c
IF a > b AND a > c THEN
SET largest = a
ELSE IF b > c THEN
SET largest = b
ELSE
SET largest = c
END IF
Flowchart
4) Factorial of a number
Algorithm
1. Start
2. Input a number, n
3. Initialize fact = 1
4. Loop from i = 1 to n:
- Multiply fact = fact * i
5. Output fact
6. Stop

Pseudocode
START
INPUT n
SET fact = 1
FOR i FROM 1 TO n DO
SET fact = fact * i
END FOR
OUTPUT fact
STOP
Flowchart
5) Check if the given
number is Odd or Even

Algorithm
1. Start
2. Input a number, n
3. Check if n % 2 == 0:
- If true, output "Even Number"
- Else, output "Odd Number"
4. Stop

Pseudocode
START
INPUT n
IF n % 2 == 0 THEN
OUTPUT "Even Number "
ELSE
OUTPUT "Odd Number "
END IF
STOP
Flowchart
6) Roots of a Quadratic Equation

Algorithm
1. Start
2. Input coefficients a, b, and c
3. Calculate discriminant: D = b^2 - 4ac
4. If D > 0:
- Calculate root1 = (-b + sqrt(D)) / (2a)
- Calculate root2 = (-b - sqrt(D)) / (2a)
5. If D == 0:
- Calculate root = -b / (2a)
6. If D < 0:
- Output "Imaginary roots"
7. Stop

Pseudocode

START
INPUT a, b, c
SET D = b^2 - 4*a*c
IF D > 0 THEN
SET root1 = (-b + sqrt(D)) / (2*a)
SET root2 = (-b - sqrt(D)) / (2*a)
OUTPUT root1, root2
ELSE IF D == 0 THEN
SET root = -b / (2*a)
OUTPUT root
ELSE
OUTPUT "Imaginary roots"
END IF
STOP
Flowchart
7) Area and Perimeter of a
Square

Algorithm
1. Start
2. Input the side length, a
3. Calculate area: A = a^2
4. Calculate perimeter: P = 4 * a
5. Output A and P
6. Stop

Pseudocode
START
INPUT a
SET area = a^2
SET perimeter = 4 * a
OUTPUT area, perimeter
STOP
Flowchart
8) Check if a Number is
Prime

Algorithm

1. Start
2. Input a number, n
3. Initialize isPrime = true
4. Loop from i = 2 to sqrt(n):
- If n % i == 0:
- Set isPrime = false
- Break the loop
5. If isPrime is true and n > 1:
- Output "Prime"
- Else, output "Not Prime"
6. Stop

Pseudocode

START
INPUT n
SET isPrime = true
FOR i FROM 2 TO sqrt(n) DO
IF n % i == 0 THEN
SET isPrime = false
BREAK
END IF
END FOR
IF isPrime == true AND n > 1 THEN
OUTPUT "Prime"
ELSE
OUTPUT "Not Prime"
END IF
STOP

FLOWCHART
9) Area of a Triangle

Algorithm
1. Start
2. Input the base, B, and height, H
3. Calculate area: area = (B * H) / 2
4. Output the area
5. Stop

Pseudocode

START
INPUT B, H
SET area = (B * H) / 2
OUTPUT area
STOP
Flowchart
10) Sum of N Natural
Numbers

Algorithm
1. Start
2. Read the value of N
3. Initialize variables: sum = 0 and
i = 1
4. Repeat steps 5 and 6 while i ≤ N:
5. Add i to sum: sum = sum + i
6. Increment i by 1: i = i + 1
7. Print the value of sum
8. Stop

Pseudocode
START
READ N
SET sum = 0, i = 1
WHILE i ≤ N DO
sum = sum + i
i = i + 1
END WHILE
PRINT sum
STOP
Flowchart

You might also like