w3resource

C#: Check the largest number among three given integers


Largest Among Three Integers

Write a C# Sharp program to check the largest number among three given integers.

Visual Presentation:

C# Sharp: Basic Algorithm Exercises - Check the largest number among three given integers.

Sample Solution:

C# Sharp Code:

using System;

// Namespace declaration
namespace exercises
{
    // Class declaration
    class Program
    {
        // Main method - entry point of the program
        static void Main(string[] args)
        {
            // Displaying the output of the 'test' method with different integer values
            Console.WriteLine(test(1, 2, 3));  // Output: 3
            Console.WriteLine(test(1, 3, 2));  // Output: 3
            Console.WriteLine(test(1, 1, 1));  // Output: 1
            Console.WriteLine(test(1, 2, 2));  // Output: 2
            Console.ReadLine();               // Keeping the console window open
        }

        // Method to find the maximum value among three integers
        public static int test(int x, int y, int z)
        {
            // Using Math.Max method to find the maximum value between x, y, and z
            // Math.Max is used twice to find the maximum value among the three integers
            var max = Math.Max(x, Math.Max(y, z));

            // Returning the maximum value among the three integers
            return max;
        }
    }
}

Sample Output:

3
3
1
2

Flowchart:

C# Sharp: Flowchart: Check the largest number among three given integers.

For more Practice: Solve these Related Problems:

  • Write a C# program to return the second-largest number among three integers.
  • Write a C# program to determine the largest number and check if it is more than the sum of the other two.
  • Write a C# program that returns true if the largest of three integers is even and a multiple of 10.
  • Write a C# program to return the largest absolute value among three integers.

Go to:


PREV : Remove 'yt' at Index 1.
NEXT : Closest to 100 Between Two.

C# Sharp Code Editor:



Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.