In this tutorial, we will discuss the C# Program to display the smallest among the three numbers.
In this program, we will discuss a simple concept of the program to find the smallest number among three numbers in the C# programming language.
In this topic, we learn how to find the smallest number from given three numbers using if statements.
Using if statements to find the largest number
Among integer numbers
Program 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
class Program { static void Main(string[] args) { Console.WriteLine("** C# Program To Display Smallest Among Three Numbers **"); float num1, num2, num3; Console.WriteLine("Enter the first number to compare: "); num1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the second number to compare: "); num2 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the third number to compare: "); num3 = Convert.ToInt32(Console.ReadLine()); if (num1 <= num2 && num1 <= num3) { Console.WriteLine("\n Smallest number is:" + num1); } if (num2 <= num1 && num2 <= num3) { Console.WriteLine("\n Smallest number is:" + num2); } if (num3 <= num1 && num3 <= num2) { Console.WriteLine("\n Smallest number is:" + num3); } Console.ReadLine(); } } |
Output:
Using nested if statements to find the smallest number
Program 2:
In this tutorial, we will discuss the C# Program to display the smallest among the three numbers.
In this program, we will discuss a simple concept of the program to find the smallest number among three numbers in the C# programming language.
In this topic, we learn how to find the smallest number from given three numbers using if statements.
Using if statements to find the largest number
Among integer numbers
Program 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
class Program { static void Main(string[] args) { Console.WriteLine("** C# Program To Display Smallest Among Three Numbers **"); float num1, num2, num3; Console.WriteLine("Enter the first number to compare: "); num1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the second number to compare: "); num2 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the third number to compare: "); num3 = Convert.ToInt32(Console.ReadLine()); if (num1 <= num2 && num1 <= num3) { Console.WriteLine("\n Smallest number is:" + num1); } if (num2 <= num1 && num2 <= num3) { Console.WriteLine("\n Smallest number is:" + num2); } if (num3 <= num1 && num3 <= num2) { Console.WriteLine("\n Smallest number is:" + num3); } Console.ReadLine(); } } |
Output:
Using nested if statements to find the smallest number
Program 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
class Program { static void Main(string[] args) { Console.WriteLine("** C# Program To Display Smallest Among Three Numbers **"); float num1, num2, num3; Console.WriteLine("Enter the first number to compare: "); num1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the second number to compare: "); num2 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the third number to compare: "); num3 = Convert.ToInt32(Console.ReadLine()); if (num1 <= num2 && num1 <= num3) { Console.WriteLine("\n Smallest number is:" + num1); } else if (num2 <= num3) { Console.WriteLine("\n Smallest number is:" + num2); } else { Console.WriteLine("\n Smallest number is:" + num3); } Console.ReadLine(); } } |