w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Perform an operation on +, -, *, x, / and displays the result of that operation


C# Sharp Basic Data Types: Exercise-4 with Solution

Write a C# Sharp program that takes two numbers as input and perform an operation (+,-,*,x,/) on them and displays the result of that operation.

Sample Solution:-

C# Sharp Code:

using System;
public class Exercise4
{
    public static void Main()
    {
        int x, y;
        char operation;
         
        Console.Write("Input first number: ");
        x = Convert.ToInt32(Console.ReadLine());
        Console.Write("Input operation: ");
        operation = Convert.ToChar(Console.ReadLine());
        Console.Write("Input second number: ");
        y = Convert.ToInt32(Console.ReadLine());
         
        if (operation=='+')
            Console.WriteLine("{0} + {1} = {2}", x, y, x+y);
        else if (operation=='-')
            Console.WriteLine("{0} - {1} = {2}", x, y, x-y);
        else if ((operation=='x') || (operation=='*'))
            Console.WriteLine("{0} * {1} = {2}", x, y, x*y);
        else if (operation=='/')
            Console.WriteLine("{0} / {1} = {2}", x, y, x/y);
        else
            Console.WriteLine("Wrong Character");
    }
}

Sample Output:

Input first number: 4                                                                                         
Input operation: *                                                                                            
Input second number: 4                                                                                        
4 * 4 = 16 

Flowchart:

Flowchart: Perform an operation on and displays the result of that operation.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C# Sharp program that takes userid and password as input (type string). After 3 wrong attempts, user will be rejected.
Next: Write a C# Sharp program that takes the radius of a circle as input and calculate the perimeter and area of the circle.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming