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
C# Exercise Example
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# 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#.
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...
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...
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...
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 ...
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...
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...