Basic Programming C# Examples

C# Program to Check Specified Number is present in a given array

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:

  1. The using System; statement at the top of the code is a directive that specifies the System namespace should be used in the code. This namespace contains various types and classes, including the Console class which is used to interact with the user through the console.
  2. 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.
  3. The class Program block defines a class in the CheckNumberInArray namespace. The class contains the Main method, which is the entry point for the program.
  4. In the Main method, Console.WriteLine is used to prompt the user to enter the number of integers in the array. The Convert.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 the n variable.
  5. An array of integers is created with int[] numbers = new int[n];. The n variable determines the size of the array.
  6. Another for loop is used to prompt the user to enter the integers in the array. The Convert.ToInt32 method is used to convert the user input to an integer, which is then stored in the numbers array.
  7. 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.
  8. A bool variable isPresent is initialized to false.
  9. Another for loop is used to iterate through the numbers array. Within the loop, an if statement checks if the current element of the array is equal to checkNumber. If it is, the isPresent variable is set to true, and the loop is broken using the break statement.
  10. After the loop, an if statement checks the value of isPresent. If it is true, a message indicating that the specified number is present in the array is displayed. If it is false, a message indicating that the specified number is not present in the array is displayed.
  11. 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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X