C# Exercise: 19 Greatest of three numbers

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.

 Lesson

Flow Control

 Exercise

Greatest of three numbers

 Objetive

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

 Example Code

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
        }
    }
}

More C# Exercises of Flow Control

 Positive and negative
Write a C# program to get a number and answer whether it is positive or negative....
 Multiply if not zero
Write a C# program to ask the user for a number; if it is not zero, then it will ask for a second number and display their sum; otherwise, it will dis...
 Divide if not zero
Write a C# program to ask the user for two numbers and display their division if the second number is not zero; otherwise, it will display "I cannot d...
 Divide if not zero (Using else)
Write a version of the previous program using 'else'. The program should ask the user for two numbers, and if the second number is not zero, it will d...
 Repeat until 0
Write a C# program to ask the user for a number "x" and display 10*x. It must repeat until the user enters 0 (using "while")....
 Repeat until 0 (Use Do While)
Write a C# program that asks the user for a number "x" and displays 10*x. The program must repeat the process until the user enters 0, using "do-while...
 While + Counter
Write a C# program to display the numbers 1 to 10 on the screen using "while"....
 Multiplication table (use while)
Write a C# program that prompts the user to enter a number and displays its multiplication table using a 'while' loop....
 Odd numbers descending
Write a C# program to display the odd numbers from 15 to 7 (downwards) on the screen using "while"....
 Sum numbers
Write a C# program to ask the user for an undetermined amount of numbers (until 0 is entered) and display their sum, as follows: Number? 5 Total =...
 Two negative numbers
Write a C# program to prompt the user for two numbers and determine if both numbers are negative or not....
 One or two negative numbers
Write a C# program to prompt the user for two numbers and determine whether both are negative, only one is negative, or neither is negative....
 Multiples
Write a C# program to display on the screen the numbers from 1 to 500 that are multiples of both 3 and 5. (Hint: Use the modulo operator to check for ...
 Number repeated
Write a C# program that asks the user for a number and a quantity, and displays that number repeated as many times as the user has specified. Here's a...
 Password
Write a C# program to prompt the user to enter their login and password (both must be integer numbers) and repeat the prompt as many times as necessar...
 Password V2
Write a C# program to ask the user for their login and password (both must be integer numbers) until the entered login is "12" and the password is "12...
 Many divisions
Write a C# program that asks the user for two numbers and displays their division and remainder of the division. If 0 is entered as the second number,...
 Several multiplication tables, (use do while)
Write a C# program that display multiplication tables from 2 to 6 using nested "do...while" loops....
 Square
Write a C# program that prompts the user to enter a number and a width, and displays a square of that width, using that number for the inner symbol, a...
 Break & continue
Write a C# program to write the even numbers from 10 to 20, both inclusive, except 16, in 3 different ways: Incrementing 2 in each step (use "conti...
 Rectangle V2
Write a C# program that asks for a number, width, and height, and displays a rectangle of that width and height, using that number for the inner symbo...
 Repetitive structures
Write a C# program that prompts the user for two numbers and displays the numbers between them (inclusive) three times using "for", "while", and "do w...
 Digits in a number
Write a C# program to calculate the number of digits in a positive integer (hint: this can be done by repeatedly dividing by 10). If the user enters a...
 Hollow square
Write a C# program that asks for a symbol and a width, and displays a hollow square of that width using that symbol for the outer border, as shown in ...
 Product
Write a C# program that asks the user for two integer numbers and shows their multiplication, but not using "*". It should use consecutive additions. ...
 Absolute value
Write a C# program to calculate (and display) the absolute value of a number x: if the number is positive, its absolute value is exactly the number x;...
 Hollow rectangle
Write a C# program that prompts for a symbol, a width, and a height, and displays a hollow rectangle of that width and height, using that symbol for t...
 Statistics
Write a C# program to calculate various basic statistical operations: it will accept numbers from the user and display their sum, average, minimum and...
 Switch
Write a C# program to display the text grade corresponding to a given numerical grade, using the following equivalence: 9,10 = Excellent 7,8 = Ver...
 Conditional operator, positive & smaller
Write a C# program that asks the user for two numbers and answers, using the conditional operator (?), for the following: If the first number is po...
 Prime number
Write a C# program that asks the user for an integer and determines if it is a prime number or not....
 Give change
Write a C# program to give change for a purchase, using the largest possible coins (or bills). Suppose we have an unlimited amount of coins (or bills)...
 Exceptions
Write a C# program to prompt the user for two numbers and display their division. Errors should be caught using "try..catch"...