5.7.25

Writing algorithms using pseudocode

Pseudocode Examples

1. Find the Maximum Number

DECLARE max, num
READ num1, num2, num3
max = num1
IF num2 > max THEN
  max = num2
IF num3 > max THEN
  max = num3
PRINT max

2. Calculate the Sum of Numbers

DECLARE sum, num, i
sum = 0
FOR i = 1 TO 5 DO
  READ num
  sum = sum + num
PRINT sum

3. Check if a Number is Even or Odd

DECLARE num
READ num
IF num MOD 2 = 0 THEN
  PRINT "Even"
ELSE
  PRINT "Odd"

4. Find the Average of Numbers

DECLARE sum, avg, num, i
sum = 0
FOR i = 1 TO 5 DO
  READ num
  sum = sum + num
avg = sum / 5
PRINT avg

5. Check if a String is a Palindrome

DECLARE str, rev
READ str
rev = REVERSE(str)
IF str = rev THEN
  PRINT "Palindrome"
ELSE
  PRINT "Not a Palindrome"

6. Calculate the Factorial of a Number

DECLARE num, fact, i
fact = 1
READ num
FOR i = 1 TO num DO
  fact = fact * i
PRINT fact

7. Find the Minimum Number

DECLARE min, num
READ num1, num2, num3
min = num1
IF num2 < min THEN
  min = num2
IF num3 < min THEN
  min = num3
PRINT min

8. Check if a Number is Prime

DECLARE num, i
READ num
FOR i = 2 TO num-1 DO
  IF num MOD i = 0 THEN
    PRINT "Not Prime"
    EXIT
PRINT "Prime"

No comments:

Post a Comment

Difference between File and Folder

10 Differences Between Files and Folders Definition: File: A collection of data or information stored on a computer. ...