9.1k views
1 vote
Design a program that gives simple math quizzes. The program should display two

random numbers that are to be added, an example is:

247
+ 129

The program should allow the student to enter the answer. If the answer is correct,
a message of congratulations should be displayed. If the answer is incorrect, a message showing the correct answer should be displayed.

In bash shell script, please.

User OneMore
by
8.2k points

1 Answer

4 votes

Answer:

Step-by-step explanation:

#!/bin/bash

# Generate two random numbers between 1 and 100

num1=$((1 + RANDOM % 100))

num2=$((1 + RANDOM % 100))

# Print the math problem

echo "$num1"

echo "+ $num2"

echo ""

# Read the user's answer

read -p "Enter your answer: " answer

# Calculate the correct answer

correct=$((num1 + num2))

# Check if the answer is correct

if [ "$answer" -eq "$correct" ]; then

echo "Congratulations, your answer is correct!"

else

echo "Sorry, the correct answer is $correct."

fi

User Fel
by
7.8k points