C#: Check if a number appears as either the first or last element of an array of integers
Check Number First or Last in Array
Write a C# program to check if a number appears as the first or last element of an array of integers. The array length is 1 or more.
Pictorial Presentation:

Sample Solution:
C# Sharp Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class Exercise46
{
public static void Main()
{
// Prompt the user to input an integer
Console.WriteLine("\nInput an integer:");
// Read the input integer and store it in the variable 'x'
int x = Convert.ToInt32(Console.ReadLine());
// Define an array of integers 'nums' with pre-defined values
int[] nums = {1, 2, 2, 3, 3, 4, 5, 6, 5, 7, 7, 7, 8, 8, 9};
// Check if the first element of the array 'nums' is equal to 'x'
// OR if the last element of the array 'nums' is equal to 'x'
// Print the result of the logical OR operation between these conditions
Console.WriteLine((nums[0] == x) || (nums[nums.Length - 1] == x));
}
}
Sample Output:
Input an integer: 25 False
Flowchart:

For more Practice: Solve these Related Problems:
- Write a C# program to verify if a number appears at both first and last positions of an array.
- Write a C# program to check if the first element is greater than the last element in an array.
- Write a C# program to check if any given number is in the first 3 or last 3 positions of an array.
- Write a C# program to replace the last number with the first if both are equal.
Go to:
PREV : Count Specific Number in Array.
NEXT : Sum of Array Elements.
C# Sharp Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.