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;
- The first line “using System;” imports the System namespace which provides access to various classes and methods, including Console.
- In the namespace “OddNumberCheck” the class “Program” is defined.
- In the class, the “Main” method is defined, which is the entry point of the program.
- The program prompts the user to enter the length of the array. This length is stored in the variable “n”.
- An array “arr” of length “n” is created and initialized.
- The program prompts the user to enter the elements of the array.
- The program prompts the user to enter an odd number to check for. This number is stored in the variable “oddNumber”.
- A variable “found” is initialized to “false”.
- 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.
- The program checks the value of “found” and outputs a message indicating if the array contains the odd number.
- 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.