Question

In: Computer Science

[C#] Randomly generate two numbers that user chooses and then ask the user what the answer...

[C#] Randomly generate two numbers that user chooses and then ask the user what the answer is when some operator is applied. If user is correct, they will be congratulated. If they are wrong, they will be given the correct answer. This should be repeated based on how many times is chosen by user. I have the code which will do the following below:

User wants to know the answer to x % y,

What is smallest value of x: (user input)

What is largest value of x: (user input)

What is smallest value of y: (user input)

What is smallest value of y: (user input)

How many times should it happen?: (user input)

What I need help with is now randomly generating the x and y the user chooses and then using the operator that they decide whether it be %,/,+, or - and ask them what the answer is, and determine whether user is correct or not. For example if range of x is between 10 and 50 and y is 30 and 80 and the user chooses to be tested on %, the program will ask the user the amount of time that they specified. For this example let's say it is 3. They are then given 3 problems featuring numbers from the range above and they are then tested on it.

Solutions

Expert Solution

using System.IO;
using System;

class Program
{
static void Main()
{
int x1, x2, y1, y2;
Console.WriteLine("What is smallest value of x:");
x1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is largest value of x:");
x2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is smallest value of y:");
y1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What is largest value of y:");
y2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the operator:");
string op = Console.ReadLine();
Console.WriteLine("How many times should it happen?:");
int n = Convert.ToInt32(Console.ReadLine());
Random random = new Random();
for(int i=1;i<=n;i++) {
int x = random.Next(x1, x2);
int y = random.Next(y1, y2);
Console.Write("What is the result of "+x+" "+op+" "+y+" = ");
int answer = Convert.ToInt32(Console.ReadLine());
int result = 0;
if(op == "+") {
result = x + y;
} else if(op == "-") {
result = x - y;
} else if(op == "*") {
result = x * y;
} else if(op == "%") {
result = x % y;
} else {
result = x / y;   
}
if(result == answer) {
Console.WriteLine("Congratulations");
} else {
Console.WriteLine("Wrong answer. Correct answer is "+result);
}
}
}
}

Output:

$mcs *.cs -out:main.exe
$mono main.exe
What is smallest value of x:
What is largest value of x:
What is smallest value of y:
What is largest value of y:
Enter the operator:
How many times should it happen?:
What is the result of 55 + 31 = 86
Congratulations
What is the result of 50 + 30 = 70
Wrong answer. Cor

rect answer is 80


Related Solutions

C++ Programa that: // 1) Ask the user to enter two numbers greater than zero. Store...
C++ Programa that: // 1) Ask the user to enter two numbers greater than zero. Store the //    values into two variables named 'n' and 'm'. //    Additionally, implement a while-loop to keep asking the user to enter //    the numbers again if any of the two numbers are equal or smaller than zero. // 2) If the two numbers (n, m) are equal, display the message "The //    numbers are the same". If the two numbers are different, display...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
Write a MIPS program that will ask the user to enter two numbers at the console...
Write a MIPS program that will ask the user to enter two numbers at the console and pass the values to a function that does multiplication
Design a program that will ask the user to input two integer numbers and then perform...
Design a program that will ask the user to input two integer numbers and then perform the basic arithmetic operations such as addition and subtraction. Each calculation is done by a separate function. The main function gets the input from the user, then calls the addition function and the subtraction function one at a time to perform the calculations. Each calculation function (addition or subtraction function) performs an arithmetic operation and then returns the calculation results back to where it...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that in array. Display that all numbers before and after performing Bubble sort. You must have to create new function with required parameter to perform Bubble sort. Sample Run :- Enter 1 number :- 1 Enter 2 number :- 5 Enter 3 number :- 7 Enter 4 number :- 45 Enter 5 number :- 90 Enter 6 number :- 6 Enter 7 number :- 55...
CODE IN C PLEASE Ask for user to input 4 different numbers Use pointers in calculations...
CODE IN C PLEASE Ask for user to input 4 different numbers Use pointers in calculations instead of variables to calculate the sum of those 4 numbers Print the sum Use a pointer to a pointer for print operation
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code should print the numbers in descending order.
I need to ask a user what numbers they want to enter. They can enter as...
I need to ask a user what numbers they want to enter. They can enter as many as they like. Then inside I need to use conditionals to determine if the numbers are <=20, <=323 && > 30, >200. I can't figure out how to let the user enter as many inputs as they want. I know I need to use a loop to check each number entered and determine if it is small middle or greater but I can't...
its a c++ programme. In your program, you will randomly generate integer numbers between -50 and...
its a c++ programme. In your program, you will randomly generate integer numbers between -50 and +50 (including -50 and +50). You will repeat this process 1000 times. While generating these numbers, you need to count the numbers based on their signs separately, as positive and negative. For instance, if your program generate +25 15 times, and -25 19 times. Then this should be reported as, Num PosFre NegFre 25 15 19 For this problem, you need to use array...
c++ In this program ask the user what name they prefer for their file, and make...
c++ In this program ask the user what name they prefer for their file, and make a file, the file name should za file. Get a number from the user and save it into the create file. should be more than 20 number in any order. print out all 20 numbers in a sorted array by using for loop, last print out its total and average. At last create a function in which you will call it from main and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT