C# Program to Perform All Arithmetic Operations

This is a C# Program to perform all basic arithmetic operations.

Problem Description

This C# Program Performs all Basic Arithmetic Operations.

Problem Solution

Two numbers are obtained from the user and the operation which the user needs to perform. Based on the option entered by the user the arithmetic operation is performed.

Program/Source Code

Here is source code of the C# Program to Perform all Basic Arithmetic Operations. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Perform all Basic Arithmetic Operations
 */
using System;
using System.Collections.Generic;
using System.Text;
namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            int Num1, Num2, result;
            char option;
            Console.Write("Enter the First Number : ");
            Num1 = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the Second Number : ");
            Num2 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Main Menu");
            Console.WriteLine("1. Addition");
            Console.WriteLine("2. Subtraction");
            Console.WriteLine("3. Multiplication");
            Console.WriteLine("4. Division");
            Console.Write("Enter the Operation you want to perform : ");
            option = Convert.ToChar(Console.ReadLine());
            switch (option)
            {
            case '1':
                result = Num1 + Num2;
                Console.WriteLine("The result of Addition is : {0}", result);
                break;
            case '2':
                result = Num1 - Num2;
                Console.WriteLine("The result of Subtraction is : {0}", result);
                break;
            case '3':
                result = Num1 * Num2;
                Console.WriteLine("The result of Multiplication is : {0}", result);
                break;
            case '4':
                result = Num1 / Num2;
                Console.WriteLine("The result of Division is : {0}", result);
                break;
            default:
                Console.WriteLine("Invalid Option");
                break;
            }
            Console.ReadLine();
        }
 
    }
}
Program Explanation

In this C# program, we are reading the two numbers using ‘Num1’ and ‘Num2’ variables respectively. The user will enter the operation to perform. Based on the option entered by the user, using switch case statement the arithmetic operation is performed. Hence we are displaying the output of the program.

advertisement
advertisement
Runtime Test Cases
 
Enter the First Number : 100
Enter the Second Number : 2
Main Menu
1.	Addition
2.	Subtraction
3.	Multiplication
4.	Division
Enter the Operation you want to perform : 3
The Result of Multiplication is : 200

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.