Advertisements
Advertisements
प्रश्न
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.
कोड लेखन
Advertisements
उत्तर
Set player1 = 0
Set player2 = 0
FOR each flip from 1 to 5
INPUT result
IF result is 1 THEN
INCREMENT p1
ELSE IF result is 2 THEN
INCREMENT p2
IF p1 is 3 THEN
PRINT "Player 1 wins the cake."
EXIT the algorithm
ELSE IF p2 is 3 THEN
PRINT "Player 2 wins the cake."
EXIT the algorithmshaalaa.com
Algorithm
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
