Multiple Operations And Precedences - C# Programming Exercise

This exercise is perfect for learning how to perform complex arithmetic operations in C#. In this program, you will be asked to solve several mathematical operations using different operators. The operations include multiplication, addition, subtraction, modulus, and the combination of these operators. The program will print the result of each operation on the console.

The instructions for the user are simple:
1. The program will perform the following operations:
-1 + 3 * 5
(24 + 5) % 7
15 + (-4) * 6 / 11
2 + 10 / 6 * 1 - 7 % 2
2. The result of each operation will be displayed on the screen.

This exercise will help you understand how arithmetic operators are used in C# and how they apply in complex expressions. Additionally, you will learn about operator precedence and how operations are evaluated in C#, using Console.WriteLine() to display the results.

 Category

First contact with C# Sharp

 Exercise

Multiple Operations And Precedences

 Objective

Write a C# program to print the result of the following operations:
-1 + 3 * 5
(24 + 5) % 7
15 + (-4) * 6 / 11
2 + 10 / 6 * 1 - 7 % 2

 Write Your C# Exercise

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

// Main class of the program
public class Program
{
    // Main method where the program execution begins
    static void Main()
    {
        // Calculating the result of -1 + 3 * 5
        int result1 = -1 + 3 * 5;
        // Printing the result of -1 + 3 * 5 to the screen
        Console.WriteLine("The result of -1 + 3 * 5 is: " + result1);

        // Calculating the result of (24 + 5) % 7
        int result2 = (24 + 5) % 7;
        // Printing the result of (24 + 5) % 7 to the screen
        Console.WriteLine("The result of (24 + 5) % 7 is: " + result2);

        // Calculating the result of 15 + (-4) * 6 / 11
        int result3 = 15 + (-4) * 6 / 11;
        // Printing the result of 15 + (-4) * 6 / 11 to the screen
        Console.WriteLine("The result of 15 + (-4) * 6 / 11 is: " + result3);

        // Calculating the result of 2 + 10 / 6 * 1 - 7 % 2
        int result4 = 2 + 10 / 6 * 1 - 7 % 2;
        // Printing the result of 2 + 10 / 6 * 1 - 7 % 2 to the screen
        Console.WriteLine("The result of 2 + 10 / 6 * 1 - 7 % 2 is: " + result4);
    }
}

 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#.

  •  Multiply using variables

    This exercise is perfect for learning how to perform a multiplication in C# with numbers entered by the user. In this program, the user will be asked to input two numbers, a...

  •  Use of {0} and comments

    This exercise is great for learning how to interact with the user in C# and perform basic mathematical operations. In this program, the user will be asked to input three num...

  •  Several operations

    This exercise will help you understand how to perform several basic mathematical operations in C#, such as addition, subtraction, multiplication, division, and calculating t...

  •  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: ...