w3resource

C#: Find the largest of three numbers


C# Sharp Conditional Statement : Exercise-8 with Solution

Write a C program to find the largest of three numbers.

C# Sharp: Find the largest of three numbers.

Sample Solution:-

C# Sharp Code:

using System;  // Importing the System namespace

public class Exercise8  // Declaration of the Exercise8 class
{  
    public static void Main()  // Entry point of the program
    {
        int num1, num2, num3;  // Declaration of integer variables num1, num2, and num3
        Console.Write("\n\n");  // Printing new lines
        Console.Write("Find the largest of three numbers:\n");  // Displaying the purpose of the program
        Console.Write("------------------------------------");  // Displaying a separator
        Console.Write("\n\n");  // Printing new lines

        Console.Write("Input the 1st number :");  // Prompting user to input the first number
        num1 = Convert.ToInt32(Console.ReadLine());  // Reading the input first number from the user

        Console.Write("Input the 2nd number :");  // Prompting user to input the second number
        num2 = Convert.ToInt32(Console.ReadLine());  // Reading the input second number from the user

        Console.Write("Input the 3rd number :");  // Prompting user to input the third number
        num3 = Convert.ToInt32(Console.ReadLine());  // Reading the input third number from the user

        if (num1 > num2)  // Checking if num1 is greater than num2
        {
            if (num1 > num3)  // Checking if num1 is greater than num3
            {
                Console.Write("The 1st Number is the greatest among three. \n\n");  // Printing a message if num1 is the greatest
            }
            else
            {
                Console.Write("The 3rd Number is the greatest among three. \n\n");  // Printing a message if num3 is the greatest
            }
        }
        else if (num2 > num3)  // Checking if num2 is greater than num3
        {
            Console.Write("The 2nd Number is the greatest among three \n\n");  // Printing a message if num2 is the greatest
        }
        else
        {
            Console.Write("The 3rd Number is the greatest among three \n\n");  // Printing a message if num3 is the greatest
        }
    }
}

Sample Output:

Find the largest of three numbers:                                                                            
------------------------------------                                                                                                  
Input the 1st number :20                                                                                      
Input the  2nd number :25                                                                                     
Input the 3rd  number :15                                                                                     
The 2nd Number is the greatest among three 

Flowchart:

Flowchart: Find the largest of three numbers.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C# Sharp program to accept the height of a person in centimeter and categorize the person according to their height.
Next: Write a C program to accept a coordinate point in an XY coordinate system and determine in which quadrant the coordinate point lies.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/csharp-exercises/conditional-statement/csharp-conditional-statement-exercise-8.php