Basic Programming C# Examples

C# Program to Print Result of Specified operations

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:

  1. First, the program uses the using System directive to include the System namespace, which provides access to the Console class for reading and writing to the console.
  2. Next, the program declares a namespace called Calculator. All the classes in the code will be part of this namespace.
  3. The Program class is declared inside the Calculator namespace. This class contains the Main method, which is the entry point of the program.
  4. The Main method starts by printing a message to the console asking the user to enter two numbers and an arithmetic operator.
  5. Then, the program prompts the user to enter the first number, which is read from the console and stored in the num1 variable.
  6. Similarly, the second number is read from the console and stored in the num2 variable.
  7. The user is then prompted to enter an arithmetic operator, which is stored in the operation variable.
  8. A switch statement is used to perform the corresponding operation based on the value of operation. If the operation is +, the sum of num1 and num2 is stored in result. If the operation is -, the difference of num1 and num2 is stored in result. If the operation is *, the product of num1 and num2 is stored in result. If the operation is /, the division of num1 by num2 is stored in result. If the value of operation is not one of the four supported arithmetic operators, the program prints a message to the console indicating that the operator is invalid.
  9. 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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X