How to find maximum between 2 numbers using C#?

CsharpProgrammingServer Side Programming

Firstly, declare and initialize two numbers.

int num1 = 50;
int num2 = 90;

With that, use if-else to find the maximum number.

if (num1 > num2) {
   maxNum = num1;
} else {
   maxNum = num2;
}

Above, we have set the maximum value to the variable maxNum and printed it later on.

The following is the complete example to find maximum between 2 numbers in C#.

Example

 Live Demo

using System;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         int num1 = 50;
         int num2 = 90;
         int maxNum;
         Console.WriteLine("Number 1: "+num1);
         Console.WriteLine("Number 2: "+num2);
         if (num1 > num2) {
            maxNum = num1;
         } else {
            maxNum = num2;
         }
         Console.WriteLine("Maximum number is: "+maxNum);
         Console.ReadKey() ;
      }
   }
}

Output

Number 1: 50
Number 2: 90
Maximum number is: 90
raja
Published on 18-Sep-2018 08:35:58
Advertisements