Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
How to find maximum between 2 numbers using C#?
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
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
Advertisements
