Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

question: Write a program to play lottery game. The program generates a random t

ID: 3632153 • Letter: Q

Question

question:

Write a program to play lottery game. The program generates a random two-digit number and prompts the player to input their two-digit guess. The player gets 10 chances to play the game. For each guess, the rules to determine the winner are:
- If the two digits match exactly the random number (in the right order), the player wins the jackpot ($1000).
- If the two digits match (but not in the right order), the player wins $100.
- If one digit match, the player wins $50.

output

Welcome to Supper Lottery!
What is your guess? 12
You win 50 dollars!
The Jackpot number is 91
Let's play again. You have 9 chance(s) left

What is your guess? 45
The Jackpot number is 83
Let's play again. You have 8 chance(s) left

What is your guess? 98
The Jackpot number is 22
Let's play again. You have 7 chance(s) left

What is your guess? 34
You win 50 dollars!
The Jackpot number is 84
Let's play again. You have 6 chance(s) left

What is your guess? 68
The Jackpot number is 19
Let's play again. You have 5 chance(s) left

What is your guess? 49
The Jackpot number is 26
Let's play again. You have 4 chance(s) left

What is your guess? 24
You win 100 dollars!
The Jackpot number is 42
Let's play again. You have 3 chance(s) left

What is your guess? 43
You win 50 dollars!
The Jackpot number is 40
Let's play again. You have 2 chance(s) left

What is your guess? 98
The Jackpot number is 65
Let's play again. You have 1 chance(s) left
What is your guess? 12
You win the jackpot!
The Jackpot number is 12
Game over!

You have won 1300 dollars.

Explanation / Answer

Matlab code:

clear all;
fprintf('Welcome to Supper Lottery!');

total = 0;

for i=1:10
x = fprintf('What is your guess?' );
y = round(100*rand(1));
if ( x = = y)
fprintf('You win 1000 dollars! ' );
total = total + 1000;
elseif (rem(x,10)==((y-rem(y,10))/10) & rem(y,10)==((x-rem(x,10))/10))
fprintf('You win 100 dollars! ' );
total = total + 100;
else ( rem(x,10)==rem(y,10)| rem(x,10)==((y-rem(y,10))/10) | rem(y,10)==((x-rem(x,10))/10) )
fprintf('You win 50 dollars! ' );
total = total + 50;
end;

fprintf('The Jackpot number is %d ', y);

if(i==0)
fprintf('Game Over!' );
elseif(i~=1)
fprintf('Let's play again. You have %d chance(s) left',10-i);
end;

fprintf(' ');
end;

fprintf('You have won %d dollars. ',total);