Basic Programming C# Examples

C# Program to check if an array contains an odd Number

The Program in C# Program to check if an array contains an odd Number is given below:

using System;

namespace OddNumberCheck
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter the length of the array here to check whether there is an odd number or not!: ");
            int n = int.Parse(Console.ReadLine());
            int[] arr = new int[n];

            Console.WriteLine("Well, enter the elements of the array: ");
            for (int i = 0; i < n; i++)
            {
                arr[i] = int.Parse(Console.ReadLine());
            }

            Console.WriteLine("Enter an odd number to check: ");
            int oddNumber = int.Parse(Console.ReadLine());

            bool found = false;
            for (int i = 0; i < n; i++)
            {
                if (arr[i] == oddNumber)
                {
                    found = true;
                    break;
                }
            }

            if (found)
                Console.WriteLine("Well, the array contains the odd number.");
            else
                Console.WriteLine("The array does not contain the odd number.");

            Console.ReadLine();
        }
    }
}

Output:

Hello Codeauri Family, enter the length of the array here to check whether there is an odd number or not!: 2

Well, enter the elements of the array:
190
199
Enter an odd number to check:
199
Well, the array contains the odd number.

Pro-Tips💡

Here are the step by step execution of above program;

  1. The first line “using System;” imports the System namespace which provides access to various classes and methods, including Console.
  2. In the namespace “OddNumberCheck” the class “Program” is defined.
  3. In the class, the “Main” method is defined, which is the entry point of the program.
  4. The program prompts the user to enter the length of the array. This length is stored in the variable “n”.
  5. An array “arr” of length “n” is created and initialized.
  6. The program prompts the user to enter the elements of the array.
  7. The program prompts the user to enter an odd number to check for. This number is stored in the variable “oddNumber”.
  8. A variable “found” is initialized to “false”.
  9. A for loop iterates over the array and checks if the element is equal to the “oddNumber”. If a match is found, the “found” variable is set to “true” and the loop is exited.
  10. The program checks the value of “found” and outputs a message indicating if the array contains the odd number.
  11. The program waits for the user to press Enter to close 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.

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