Ejercicio
Varias operaciones
Objetivo
Escriba un programa en C# para imprimir en pantalla el resultado de sumar, restar, multiplicar y dividir dos números escritos por el usuario. El resto de la división también debe mostrarse.
Podría verse así:
Introduzca un número: 12
Introduzca otro número: 3
12 + 3 = 15
12 - 3 = 9
12 x 3 = 36
12 / 3 = 4 <
12 mod 3 = 0
Código de Ejemplo
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);
}
}