Write An Algorithm

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Write an algorithm & pseudo code that reads two numbers and

multiplies them
together and print out their product.
Pseudo code:
Input 2 numbers
Find the product by multiplying them
Print their product
Algorithm:
Input num1
Input num2
Product = num1*num2
print Product

Write an algorithm and pseudo code that tells a user that the
number they entered
is not a 5 or a 6
Pseudo code:
Input a number
If number not equal to 5 and not equal to 6
Then print “The number entered is neither ‘5’ nor ‘6’ ”

Algorithm:
Input num
If (num≠5 and ≠6)
Print “The number entered is neither ‘5’nor ‘6’”
Endif

Write an algorithm and pseudo code that performs the following:


Ask a user to
enter a number.
a. If the number is between 0 and 10, write the word blue.
b. If the number is between 10 and 20, write the word red.
c. If the number is between 20 and 30, write the word green.
d. If it is any other number, write that it is not a correct color option.
Pseudo code:
Write "Input a number"
Read num
If (num >= 0 and num <10)
Print blue
else If (num >= 0 and num <10)
Print red
else If (num >= 0 and num <=30)
Print green
else
Print "not a correct color option"
Algorithm:
Input num
If 0 <= num <10
Print blue
else If 10 <= num <20
Print red
else If 20 <= num <=30
Print green
else
Print "not a correct color option"
Endif

Consider the following problem: A Water Jug Problem: You are


given two jugs, a
4-gallon one and a 3-gallon one, a pump which has unlimited water
which you can
use to fill the jug, and the ground on which water may be poured.
Neither jug has
any measuring markings on it. How can you get exactly 2 gallons of
water in the
4-gallon jug? Write an algorithm.

1.Fill the 4-gallon jug completely.


2. Pour water from 4gallon jug to 3gallon jug until it
got filled completely. (4gallon jug is left with 1 gallon of water.)
4. Empty the 3-gallon jug by pouring the water on the
ground.
5. Now transfer the water from 4-gallon jug to 3-gallon jug,
(now 3-gallon jug has only 1 gallon water)
6. Now fill 4-gallon jug fully.
7.Pour water from 4-gallon jug to the 3gallon jug until 3gallon jug gets
filled completely.
8. lastly, we are left with 2 gallons of water in 4-gallon jug.

5. Two friends decide who gets the last slice of a cake by flipping a
coin five times.
The first person to win three flips wins the cake. An input of 1
means player 1 wins
a flip, and a 2 means player 2 wins a flip. Design an algorithm to
determine who
takes the cake?
SET i := 1, player1 := 0, player2 := 0
WHILE i <= 5 DO
     INPUT coin
     IF coin = 1 THEN
          player1 := player1 + 1
     ELSE
          player2 := player2 + 1
     END IF
     IF player1 = 3 THEN
  
      PRINT ‘Player 1 won the last slice.’
          BREAK
     END IF
     IF player2 = 3 THEN
          PRINT ‘Player 2 won the last slice.’
          BREAK
     END IF
     INCREASE i BY 1
END LOOP

You might also like