c-sharp
  1. c-sharp-program-to-find-the-largest-number-among-three-numbers

Program to Find the Largest Number Among Three Numbers - (C# Basic Programs)

Finding the largest number among three numbers is a basic programming exercise that helps improve problem-solving skills. In this tutorial, we will discuss a program to find the largest number among three numbers using C# programming language.

Syntax

The syntax for finding the largest number among three numbers using C# is as follows:

if (num1 > num2 && num1 > num3) {
    // num1 is the largest
} else if (num2 > num1 && num2 > num3) {
    // num2 is the largest
} else {
    // num3 is the largest
}

Example

using System;

namespace LargestNumberProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1, num2, num3;
            Console.WriteLine("Enter three numbers: ");
            num1 = Convert.ToInt32(Console.ReadLine());
            num2 = Convert.ToInt32(Console.ReadLine());
            num3 = Convert.ToInt32(Console.ReadLine());

            if (num1 > num2 && num1 > num3)
            {
                Console.WriteLine("Largest number is " + num1);
            }
            else if (num2 > num1 && num2 > num3)
            {
                Console.WriteLine("Largest number is " + num2);
            }
            else
            {
                Console.WriteLine("Largest number is " + num3);
            }

            Console.ReadLine();
        }
    }
}

Output:

Enter three numbers:
4
8
2
Largest number is 8

Explanation

The program prompts the user to enter three numbers. It then checks to see which of the three numbers is the largest by using conditional statements. If the first number is the largest, it is printed to the console. If the second number is the largest, it is printed to the console. If neither of these cases is true, the third number is the largest and printed to the console.

Use

Finding the largest number among three numbers is a basic programming exercise that helps to improve problem-solving skills and understanding of conditional statements. This program can be used as a basis for more complex programs that require comparison of multiple variables.

Summary

In this tutorial, we have discussed a basic program to find the largest number among three numbers using C# programming language. We have seen the syntax, example, explanation, and use of finding the largest number among three numbers using C#. This exercise helps programmers to develop their problem-solving skills and gain a better understanding of how conditional statements can be used to compare variables.

Published on: