In: Computer Science
Create a C# console application (do not create a .NET CORE project) and name the project. Generate two random integers, each between 1 and 50, that you will be adding together to test the user's ability to perform the addition operator. Display the numbers in the console, such as:
7 + 22 = ?
Once the user provides their answer, check to see if it is correct and if not, tell them sorry, please try again. If their answer is correct, congratulate them on getting the right answer.
//C# program to test the user's ability to perform the addition
operator
using System;
class AdditionProblem {
static void Main() {
// create an object of Random class present in
System
Random rand = new Random();
// generate 2 integer in the range [1,50]
int n1 = rand.Next(1,51);
int n2 = rand.Next(1,51);
// calculate the result of addition
int result = n1 + n2;
// output the question on console
Console.WriteLine(n1 + " + "+n2 +" = ?");
// get the user input of the result of addition
int userResult = Convert.ToInt32(Console.ReadLine()); // convert
string to integer
// check if user result is correcy or not and display
a message accordingly
if(userResult == result)
Console.WriteLine("Congratulations! You got it right!");
else
Console.WriteLine("Sorry! Please try again");
}
}
//end of program
Output: