This question is for C++ Write a program to play the lottery. The program random
ID: 3726036 • Letter: T
Question
This question is for C++
Write a program to play the lottery. The program randomly generates a lottery of a three-digit number, prompts the user to enter a three-digit number, and determines whether the user wins according to the following rules:
If the user input matches the lottery number in the exact order, the award is $10,000.
If all the digits in the user input match all the digits in the lottery number, the award is $3,000.
If one digit (or two digits) in the user input matches a digit in the lottery number, the award is $1,000.
Explanation / Answer
import java.util.Scanner; public class Exercise_03_15 { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Generate a random three-digit number int lottery = (int)(Math.random() * 1000); // Prompt the user to enter a three-digit number System.out.print("Enter a three-digit number: "); int guess = input.nextInt(); // Extract digits from lottery int lotteryDigit1 = lottery / 100; int remainingDigits = lottery % 100; int lotteryDigit2 = remainingDigits / 10; int lotteryDigit3 = remainingDigits % 10; // Extract digits from guess int guessDigit1 = guess / 100; int remainingDigits = guess % 100; int guessDigit2 = remainingDigits / 10; int guessDigit3 = remainingDigits % 10; System.out.println("The lottery number is " + lottery); // Check the guess if (guess == lottery) System.out.println("Exact match: you win $10,000"); if (guessDigit1 == lotteryDigit2) { } } } import java.util.Scanner; public class Exercise_03_15 { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Generate a random three-digit number int lottery = (int)(Math.random() * 1000); // Prompt the user to enter a three-digit number System.out.print("Enter a three-digit number: "); int guess = input.nextInt(); // Extract digits from lottery int lotteryDigit1 = lottery / 100; int remainingDigits = lottery % 100; int lotteryDigit2 = remainingDigits / 10; int lotteryDigit3 = remainingDigits % 10; // Extract digits from guess int guessDigit1 = guess / 100; int remainingDigits = guess % 100; int guessDigit2 = remainingDigits / 10; int guessDigit3 = remainingDigits % 10; System.out.println("The lottery number is " + lottery); // Check the guess if (guess == lottery) System.out.println("Exact match: you win $10,000"); if (guessDigit1 == lotteryDigit2) { } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.