Greatest Of Three Numbers - C# Programming Exercise

In this C# exercise, you will learn how to write a program that prompts the user to enter three numbers and displays the greatest one. The program will use conditional structures like if or else to compare the values entered by the user and determine which one is the greatest. This exercise is perfect for practicing comparison logic and conditional decisions in C#, allowing the program to respond dynamically depending on the user's input. Moreover, this exercise will help you enhance your skills in decision-making within a program and in implementing simple comparisons.

Completing this exercise will enable you to handle more complex comparisons in C# in future projects, and you will also learn how to provide personalized responses based on the values entered by the user.

 Category

Flow Control

 Exercise

Greatest Of Three Numbers

 Objective

Write a C# program that prompts the user to enter three numbers and displays the greatest one.

 Write Your C# Exercise

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

class Program
{
    // Main method where the program execution begins
    static void Main()
    {
        double firstNumber, secondNumber, thirdNumber; // Declaring variables to store the three numbers entered by the user

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

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

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

        // Checking which number is the greatest using if-else statements
        if (firstNumber >= secondNumber && firstNumber >= thirdNumber) // If the first number is greater than or equal to both other numbers
        {
            // Displaying the first number as the greatest
            Console.WriteLine("The greatest number is: {0}", firstNumber); // Printing the greatest number
        }
        else if (secondNumber >= firstNumber && secondNumber >= thirdNumber) // If the second number is greater than or equal to both other numbers
        {
            // Displaying the second number as the greatest
            Console.WriteLine("The greatest number is: {0}", secondNumber); // Printing the greatest number
        }
        else // If the third number is greater than or equal to both other numbers
        {
            // Displaying the third number as the greatest
            Console.WriteLine("The greatest number is: {0}", thirdNumber); // Printing the greatest number
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Flow Control

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

  •  Repeat until 0

    In this C# exercise, you will learn how to write a program that asks the user to enter a number "x" and displays the result of multiplying it by 10. The program will keep as...

  •  Repeat until 0 (Use Do While)

    In this C# exercise, you will learn how to write a program that asks the user to enter a number "x" and displays the result of multiplying it by 10. The program will keep re...

  •  While + Counter

    In this C# exercise, you will learn how to write a program that displays the numbers from 1 to 10 on the screen using a while loop. The while loop is a control...

  •  Multiplication table (use while)

    In this C# exercise, you will learn how to write a program that asks the user to enter a number and then displays its multiplication table using a while loop. The ...

  •  Odd numbers descending

    In this C# exercise, you will learn how to write a program that displays the odd numbers from 15 down to 7 using a while loop. The while loop is a control stru...

  •  Sum numbers

    In this C# exercise, you will learn how to write a program that asks the user to enter an undetermined amount of numbers (until 0 is entered) and displays the total sum of t...