Home » C# » C# programs » C# basic programs

C# program to find largest of two numbers

C# example to find largest of two numbers: Here, we are going to learn how to find largest number among two numbers using the simple if-else statement and ternary operator in C#?
Submitted by IncludeHelp, on April 09, 2019

Given two integer numbers and we have to find largest number using C# program.

Finding largest of two numbers

To find the largest number from given two numbers – we will compare their values using either the simple if-else statement or ternary operator. If the first number is greater than the second number, assign the first number to the variable in which we will store the largest number, if the first number is not the greater than the second number then assign the second number to the variable.

Let suppose given numbers are a and b and we have to store the largest number in large.

Syntax using if-else

    if (a > b)
        large = a;
    else
        large = b;

Syntax using ternary operator

    large = (a > b) ? a : b;

C# code to find largest of two numbers

Here, we are asking for two integer numbers from the user and finding the largest one using if-else and ternary operator .

span style='color:#696969; '>// C# program to find largest of two numbers
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            int a;
            int b;
            int large;

            //input the numbers
            Console.Write("Enter first number : ");
            a = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter second number: ");
            b = Convert.ToInt32(Console.ReadLine());

            //finding largest number using if-else
            if (a > b)
                large = a;
            else
                large = b;

            //printing 
            Console.WriteLine("Using if-else...");
            Console.WriteLine("Largest number is {0}", large);

            //finding largest number using ternary operator 
            large = (a > b) ? a : b;

            //printing 
            Console.WriteLine("Using if-else...");
            Console.WriteLine("Largest number is {0}", large);

            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output

First run:
Enter first number : 10
Enter second number: 20
Using if-else...
Largest number is 20
Using if-else...
Largest number is 20

Second run:
Enter first number : 20
Enter second number: 10
Using if-else...
Largest number is 20
Using if-else...
Largest number is 20

Third run:
Enter first number : 20
Enter second number: 20
Using if-else...
Largest number is 20
Using if-else...
Largest number is 20





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.