w3resource

C#: Print on screen the output of adding, subtracting, multiplying and dividing of two numbers which will be entered by the user


Arithmetic Operations

Write a C# Sharp program to print on screen the output of adding, subtracting, multiplying and dividing two numbers entered by the user.

C# sharp Exercises: Print on screen the output of adding, subtracting, multiplying and dividing of two numbers which will be entered by the user

Sample Solution:

C# Sharp Code:

using System;

// This is the beginning of the Exercise7 class
public class Exercise7
{
    // This is the main method where the program execution starts
    public static void Main()
    {
        // Prompting the user to enter the first number
        Console.Write("Enter a number: ");
        // Reading the first number entered by the user and converting it to an integer
        int num1 = Convert.ToInt32(Console.ReadLine());

        // Prompting the user to enter the second number
        Console.Write("Enter another number: ");
        // Reading the second number entered by the user and converting it to an integer
        int num2 = Convert.ToInt32(Console.ReadLine());

        // Displaying addition of the two numbers
        Console.WriteLine("{0} + {1} = {2}", num1, num2, num1 + num2);

        // Displaying subtraction of the two numbers
        Console.WriteLine("{0} - {1} = {2}", num1, num2, num1 - num2);

        // Displaying multiplication of the two numbers
        Console.WriteLine("{0} x {1} = {2}", num1, num2, num1 * num2);

        // Displaying division of the two numbers
        Console.WriteLine("{0} / {1} = {2}", num1, num2, num1 / num2);

        // Displaying modulus (remainder) of the two numbers
        Console.WriteLine("{0} mod {1} = {2}", num1, num2, num1 % num2);
    }
}

Sample Output:

Enter a number: 10                                                                                            
Enter another number: 2                                                                                       
10 + 2 = 12                                                                                                   
10 - 2 = 8                                                                                                    
10 x 2 = 20                                                                                                   
10 / 2 = 5                                                                                                    
10 mod 2 = 0 

Flowchart:

Flowchart: C# Sharp Exercises - Print on screen the output of adding, subtracting, multiplying and dividing of two numbers which will be entered by the user

For more Practice: Solve these Related Problems:

  • Write a C# program to perform arithmetic operations on two numbers and also display the result in binary format.
  • Write a C# program to take two numbers and perform all five operations but reverse the operands in each case.
  • Write a C# program to perform arithmetic operations on user input and check if the final results are all prime numbers.
  • Write a C# program that performs arithmetic operations only if both inputs are divisible by 3 or 7.

Go to:


PREV : Multiply Three Numbers.
NEXT : Multiplication Table.

C# Sharp Code Editor:



What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.