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:
C# Sharp Code Editor:
Previous: Write a C# program to count a specified number in a given array of integers.
Next: Write a C# program to compute sum of all the elements of an array of integers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics