The Program in C# Program to Print Result of Specified operations are given below:
using System;
namespace Calculator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Famuly, Enter the first number and Second number here to find their arithemtic operations: ");
Console.WriteLine("Well, enter the first number: ");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Well,enter the second number: ");
int num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Now, enter the operation (+, -, *, /): ");
string operation = Console.ReadLine();
int result = 0;
switch (operation)
{
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
default:
Console.WriteLine("Invalid operator");
break;
}
if (result != 0)
{
Console.WriteLine("Okay, the result of " + num1 + " " + operation + " " + num2 + " is: " + result);
}
}
}
}
Output:
Hello Codeauri Famuly, Enter the first number and Second number here to find their arithemtic operations:
Well, enter the first number:
90
Well,enter the second number:
20
Now, enter the operation (+, -, *, /):
Okay, the result of 90 – 20 is: 70
Pro-Tips💡
Here are the steps by steps execution of above program:
- First, the program uses the
using System
directive to include theSystem
namespace, which provides access to theConsole
class for reading and writing to the console. - Next, the program declares a namespace called
Calculator
. All the classes in the code will be part of this namespace. - The
Program
class is declared inside theCalculator
namespace. This class contains theMain
method, which is the entry point of the program. - The
Main
method starts by printing a message to the console asking the user to enter two numbers and an arithmetic operator. - Then, the program prompts the user to enter the first number, which is read from the console and stored in the
num1
variable. - Similarly, the second number is read from the console and stored in the
num2
variable. - The user is then prompted to enter an arithmetic operator, which is stored in the
operation
variable. - A switch statement is used to perform the corresponding operation based on the value of
operation
. If theoperation
is+
, the sum ofnum1
andnum2
is stored inresult
. If theoperation
is-
, the difference ofnum1
andnum2
is stored inresult
. If theoperation
is*
, the product ofnum1
andnum2
is stored inresult
. If theoperation
is/
, the division ofnum1
bynum2
is stored inresult
. If the value ofoperation
is not one of the four supported arithmetic operators, the program prints a message to the console indicating that the operator is invalid. - Finally, if the value of
result
is not 0, the program prints the result of the operation to the console.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.