C# program to find the maximum of three numbers

CsharpServer Side ProgrammingProgramming

Firstly, let’s set the three numbers −

int num1, num2, num3;
// set the value of the three numbers
num1 = 10;
num2 = 20;
num3 = 50;

Now check the first number with the second number. If num1 > num2, then check num1 with num3. If num1 is greater than num3, that would mean the largest number is num1.

Example

You can try to run the following code to find the maximum of three numbers.

Live Demo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
   class MyApplication {
      static void Main(string[] args) {
         int num1, num2, num3;
         // set the value of the three numbers
         num1 = 10;
         num2 = 20;
         num3 = 50;
         if (num1 > num2) {
            if (num1 > num3) {
               Console.Write("Number one is the largest!\n");
            } else {
               Console.Write("Number three is the largest!\n");
         }
      }
      else if (num2 > num3)
         Console.Write("Number two is the largest!\n");
      else
         Console.Write("Number three is the largest!\n");
      }
   }
}

Output

Number three is the largest!
raja
Published on 25-Jul-2018 09:21:09
Advertisements