Equivalent Operations - C# Programming Exercise

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: a, b, and c, and the program will calculate and display two results:
1. The result of (a + b) · c
2. The result of a · c + b · c

The goal of this exercise is to understand the distributive property of multiplication over addition, which states that (a + b) · c = a · c + b · c. This is a fundamental concept in mathematics and programming.

This exercise will help you practice using Console.ReadLine() to capture user input and working with arithmetic operators in C#, such as addition and multiplication. Additionally, it will assist you in displaying the results of calculations using Console.WriteLine(), a vital tool for console output.

 Category

First contact with C# Sharp

 Exercise

Equivalent Operations

 Objective

Write a C# program to ask the user for three numbers (a, b, c) and display the result of (a+b)·c and the result of a·c + b·c.

 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 three variables to store the numbers entered by the user
        double a, b, c;

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

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

        // Asking the user to enter the third number and reading the input
        Console.Write("Enter the third number (c): ");
        c = Convert.ToDouble(Console.ReadLine());

        // Calculating the result of (a + b) * c
        double result1 = (a + b) * c;

        // Calculating the result of a * c + b * c
        double result2 = a * c + b * c;

        // Printing the results of the two operations to the screen
        Console.WriteLine("The result of (a + b) * c is: {0}", result1);
        Console.WriteLine("The result of a * c + b * c is: {0}", result2);
    }
}

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

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

  •  Conversion

    This C# exercise teaches you how to perform temperature unit conversions. The program prompts the user to input a temperature in Celsius degrees and then converts it ...

  •  First contact with C#

    This exercise is an excellent way to get started with programming in C#. In this program, you will learn how to print a message on the screen using C#. The program wi...

  •  Sum of two numbers

    This exercise is a great way to practice basic arithmetic operations in C#. In this program, you will learn how to perform an addition operation between two numbers, 12 and ...