Exercise
Several Operations
Objective
Write a C# program to print on screen the result of adding, subtracting, multiplying and dividing two numbers typed by the user. The remainder of the division must be displayed, too.
It might look like this:
Enter a number: 12
Enter another number: 3
12 + 3 = 15
12 - 3 = 9
12 x 3 = 36
12 / 3 = 4 <
12 mod 3 = 0
Write Your C# Exercise
C# Exercise Example
using System; // Importing the System namespace to use Console functionalities
// Main class of the program
class Program
{
// Main method where the program execution begins
static void Main()
{
// Declaring two variables to store the numbers entered by the user
int num1, num2;
// Asking the user to enter the first number and reading the input
Console.Write("Enter a number: ");
num1 = Convert.ToInt32(Console.ReadLine());
// Asking the user to enter the second number and reading the input
Console.Write("Enter another number: ");
num2 = Convert.ToInt32(Console.ReadLine());
// Calculating the sum of the two numbers
int sum = num1 + num2;
// Printing the result of the addition to the screen
Console.WriteLine("{0} + {1} = {2}", num1, num2, sum);
// Calculating the difference between the two numbers
int difference = num1 - num2;
// Printing the result of the subtraction to the screen
Console.WriteLine("{0} - {1} = {2}", num1, num2, difference);
// Calculating the product of the two numbers
int product = num1 * num2;
// Printing the result of the multiplication to the screen
Console.WriteLine("{0} x {1} = {2}", num1, num2, product);
// Calculating the quotient of the division
int quotient = num1 / num2;
// Printing the result of the division to the screen
Console.WriteLine("{0} / {1} = {2}", num1, num2, quotient);
// Calculating the remainder of the division
int remainder = num1 % num2;
// Printing the remainder of the division to the screen
Console.WriteLine("{0} mod {1} = {2}", num1, num2, remainder);
}
}