Writing Pseudocode for Simple Algorithms
February 9, 2025
Pseudocode is a structured way of writing an algorithm in plain English before converting it into actual code. It helps programmers plan, organize, and visualize the steps of a program before implementation.
1. Pseudocode for Finding the Sum of Two Numbers
Problem Statement:
Write a pseudocode to take two numbers as input and display their sum.
Pseudocode:
BEGIN
INPUT num1, num2
SET sum = num1 + num2
PRINT sum
END
Explanation:
- Take two inputs (num1 and num2).
- Perform addition and store the result in sum.
- Display the sum as output.
2. Pseudocode for Finding the Largest of Three Numbers
Problem Statement:
Write a pseudocode to find the largest among three numbers.
Pseudocode:
BEGIN
INPUT num1, num2, num3
IF num1 > num2 AND num1 > num3 THEN
PRINT "num1 is the largest"
ELSE IF num2 > num1 AND num2 > num3 THEN
PRINT "num2 is the largest"
ELSE
PRINT "num3 is the largest"
ENDIF
END
Explanation:
- Compare num1, num2, and num3.
- Print the largest number based on conditions.
3. Pseudocode for Checking if a Number is Even or Odd
Problem Statement:
Write a pseudocode to determine whether a number is even or odd.
Pseudocode:
BEGIN
INPUT number
IF number MOD 2 == 0 THEN
PRINT "Even Number"
ELSE
PRINT "Odd Number"
ENDIF
END
Explanation:
- Use the modulus operator (MOD 2) to check divisibility by 2.
- If remainder is 0, print "Even Number", otherwise print "Odd Number".
4. Pseudocode for Calculating Factorial of a Number
Problem Statement:
Write a pseudocode to calculate the factorial of a given number N.
Pseudocode:
BEGIN
INPUT N
SET factorial = 1
FOR i FROM 1 TO N DO
factorial = factorial * i
ENDFOR
PRINT factorial
END
Explanation:
- Multiply numbers from 1 to N to compute the factorial.
- Print the final value of factorial.
5. Pseudocode for Finding the Sum of First N Natural Numbers
Problem Statement:
Write a pseudocode to find the sum of the first N natural numbers.
Pseudocode:
BEGIN
INPUT N
SET sum = 0
FOR i FROM 1 TO N DO
sum = sum + i
ENDFOR
PRINT sum
END
Explanation:
- Initialize sum = 0.
- Use a loop to add numbers from 1 to N.
- Print the final sum.
6. Pseudocode for Checking if a Number is Prime
Problem Statement:
Write a pseudocode to check if a number N is prime.
Pseudocode:
BEGIN
INPUT N
SET isPrime = True
IF N <= 1 THEN
PRINT "Not Prime"
ELSE
FOR i FROM 2 TO N/2 DO
IF N MOD i == 0 THEN
SET isPrime = False
BREAK
ENDIF
ENDFOR
IF isPrime THEN
PRINT "Prime Number"
ELSE
PRINT "Not Prime"
ENDIF
ENDIF
END
Explanation:
- Prime numbers are greater than 1 and divisible only by 1 and itself.
- Check divisibility for numbers from 2 to N/2.
- If N is divisible by any number in this range, it’s not prime.