Several Operations - C# Programming Exercise

This exercise will help you understand how to perform several basic mathematical operations in C#, such as addition, subtraction, multiplication, division, and calculating the remainder (modulus). In this program, the user will need to enter two numbers, and the program will compute and display the results of all these operations. Additionally, the remainder of the division is displayed using the modulus operator in C#, helping you understand how to get the remainder of a division operation.

The instructions for the user are as follows:
1. The program will prompt the user to enter two numbers.
2. Then, the following operations will be performed and displayed on the console:
- Addition: 12 + 3
- Subtraction: 12 - 3
- Multiplication: 12 x 3
- Division: 12 / 3
- Modulus (remainder of the division): 12 mod 3

This exercise is perfect for practicing how to work with arithmetic operators in C#, how to perform user input with Console.ReadLine(), and how to display the results using Console.WriteLine(). Additionally, you will learn how to display the remainder of a division using the modulus operator in C#.

 Category

First contact with C# Sharp

 Exercise

Several Operations

 Objective

Write a C# program to print on screen the result of adding, subtracting, multiplying and dividing two numbers typed by the user. The remainder of the division must be displayed, too.

It might look like this:
Enter a number: 12
Enter another number: 3
12 + 3 = 15
12 - 3 = 9
12 x 3 = 36
12 / 3 = 4 <
12 mod 3 = 0

 Write Your C# Exercise

using System; // Importing the System namespace to use Console functionalities

// Main class of the program
class Program
{
    // Main method where the program execution begins
    static void Main()
    {
        // Declaring two variables to store the numbers entered by the user
        int num1, num2;

        // Asking the user to enter the first number and reading the input
        Console.Write("Enter a number: ");
        num1 = Convert.ToInt32(Console.ReadLine());

        // Asking the user to enter the second number and reading the input
        Console.Write("Enter another number: ");
        num2 = Convert.ToInt32(Console.ReadLine());

        // Calculating the sum of the two numbers
        int sum = num1 + num2;
        // Printing the result of the addition to the screen
        Console.WriteLine("{0} + {1} = {2}", num1, num2, sum);

        // Calculating the difference between the two numbers
        int difference = num1 - num2;
        // Printing the result of the subtraction to the screen
        Console.WriteLine("{0} - {1} = {2}", num1, num2, difference);

        // Calculating the product of the two numbers
        int product = num1 * num2;
        // Printing the result of the multiplication to the screen
        Console.WriteLine("{0} x {1} = {2}", num1, num2, product);

        // Calculating the quotient of the division
        int quotient = num1 / num2;
        // Printing the result of the division to the screen
        Console.WriteLine("{0} / {1} = {2}", num1, num2, quotient);

        // Calculating the remainder of the division
        int remainder = num1 % num2;
        // Printing the remainder of the division to the screen
        Console.WriteLine("{0} mod {1} = {2}", num1, num2, remainder);
    }
}

 Share this C# exercise

 More C# Programming Exercises of First contact with C# Sharp

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Multiplication table

    This exercise is perfect for learning how to generate multiplication tables dynamically in C#. In this program, the user enters a number, and the program displays the multip...

  •  Average

    This exercise is a great opportunity to learn how to calculate the average of several numbers in C#. In this program, the user must enter four numbers, and the program will ...

  •  Equivalent operations

    This C# exercise is perfect for learning how to perform mathematical operations with three numbers provided by the user. In this program, the user must enter three numbers: ...

  •  Age

    This C# exercise is perfect for learning how to interact with the user and use input data to personalize the program's output. In this case, the program will ask the user to...

  •  Formats

    This C# exercise teaches you how to handle console output using two different methods: Console.Write and formatting with {0}. In this program, the user will in...

  •  Rectangle

    This C# exercise helps you practice using loops and user input. In this program, the user will be asked to input a number (a digit), and the program will display a re...