The Program in C# Program to Check Specified Number is present in a given array is given below:
using System;
namespace CheckNumberInArray
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family,enter the number of integers in the array to check if inputted number is present or not!:");
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());
}
Console.WriteLine("Well,enter the number here you want to check:");
int checkNumber = Convert.ToInt32(Console.ReadLine());
bool isPresent = false;
for (int i = 0; i < n; i++)
{
if (numbers[i] == checkNumber)
{
isPresent = true;
break;
}
}
if (isPresent)
{
Console.WriteLine(checkNumber + " is present in the array.");
}
else
{
Console.WriteLine(checkNumber + " is not present in the array.");
}
Console.ReadLine();
}
}
}
Output:
Hello Codeauri Family,enter the number of integers in the array to check if inputted number is present or not!:3
Enter the integers:
190
908
870
Well,enter the number here you want to check:
7
7 is not present in the array.
Pro-Tips💡
Here are the step by step execution of above program:
- The
using System;
statement at the top of the code is a directive that specifies theSystem
namespace should be used in the code. This namespace contains various types and classes, including theConsole
class which is used to interact with the user through the console. - The
namespace CheckNumberInArray
block defines a namespace for the code. This allows you to organize the code and avoid naming conflicts with other code in your project. - The
class Program
block defines a class in theCheckNumberInArray
namespace. The class contains theMain
method, which is the entry point for the program. - In the
Main
method,Console.WriteLine
is used to prompt the user to enter the number of integers in the array. TheConvert.ToInt32
method is then used to convert the user input, which is in the form of a string, to an integer. This integer is stored in then
variable. - An array of integers is created with
int[] numbers = new int[n];
. Then
variable determines the size of the array. - Another
for
loop is used to prompt the user to enter the integers in the array. TheConvert.ToInt32
method is used to convert the user input to an integer, which is then stored in thenumbers
array. - The user is prompted to enter the number they want to check. The number is converted to an integer and stored in the
checkNumber
variable. - A
bool
variableisPresent
is initialized tofalse
. - Another
for
loop is used to iterate through thenumbers
array. Within the loop, anif
statement checks if the current element of the array is equal tocheckNumber
. If it is, theisPresent
variable is set totrue
, and the loop is broken using thebreak
statement. - After the loop, an
if
statement checks the value ofisPresent
. If it istrue
, a message indicating that the specified number is present in the array is displayed. If it isfalse
, a message indicating that the specified number is not present in the array is displayed. - Finally, the
Console.ReadLine
method is used to pause the program and wait for the user to press enter before the program ends.
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.