The Program in C# Program to Check largest Number among given integers is given below:
using System;
namespace LargestNumber
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family, enter the number of integers here you want to compare:");
int n = Convert.ToInt32(Console.ReadLine());
int[] numbers = new int[n];
Console.WriteLine("Enter the integers:");
for (int i = 0; i < n; i++)
{
numbers[i] = Convert.ToInt32(Console.ReadLine());
}
int largest = numbers[0];
for (int i = 1; i < n; i++)
{
if (numbers[i] > largest)
{
largest = numbers[i];
}
}
Console.WriteLine("Okay, the largest number is: " + largest);
Console.ReadLine();
}
}
}
Output:
Hello Codeauri Family, enter the number of integers here you want to compare:
Enter the integers:
20540707
20560423
20120292
Okay, the largest number is: 20560423
Pro-Tips💡
Here are the step by step execution of above program:
- The program starts with the line
using System;
, which is a directive that includes the System namespace. This namespace contains many classes and methods that are commonly used in C# programs, including the Console class that is used later in the program. - The next line
namespace LargestNumber
defines a namespace for the program, which can be used to organize related classes and avoid naming conflicts with other classes. - The following block
class Program
defines a class calledProgram
that contains the program logic. - The method
Main(string[] args)
is the entry point of the program. This method will be executed when the program is run. - The line
Console.WriteLine("Hello Codeauri Family, enter the number of integers here you want to compare:");
outputs the text “Hello Codeauri Family, enter the number of integers here you want to compare:” to the console. - The next line
int n = Convert.ToInt32(Console.ReadLine());
reads a line of text from the console and converts it to an integer, which is stored in the variablen
. - The line
int[] numbers = new int[n];
creates an array of integers withn
elements. - The following loop
for (int i = 0; i < n; i++)
prompts the user to entern
integers. The entered integers are stored in thenumbers
array. - The next block of code
for (int i = 1; i < n; i++)
loops through thenumbers
array and keeps track of the largest number. The variablelargest
is initialized with the first element of thenumbers
array. The loop then checks if each subsequent element is larger thanlargest
, and if so, updateslargest
with the new largest number. - The line
Console.WriteLine("Okay, the largest number is: " + largest);
outputs the text “Okay, the largest number is: ” followed by the largest number that was found in the previous step. - The last line
Console.ReadLine();
waits for the user to press the Enter key before closing the console.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.