24 Feb 2024




Beginner

To find the minimum number in an array in C# without using any built-in methods, you can iterate through the array and keep track of the minimum number encountered. Here's how you can do it:

using System;

class Program
{
    static void Main(string[] args)
    {
        // Sample array
        int[] numbers = { 10, 5, 8, 20, 3, 15 };

        // Variable to store the minimum number
        int min = numbers[0]; // Initialize min with the first element of the array

        // Iterate through the array starting from the second element
        for (int i = 1; i < numbers.Length; i++)
        {
            // Check if the current element is less than the current min
            if (numbers[i] < min)
            {
                // Update min if the current element is less
                min = numbers[i];
            }
        }

        // Print the minimum number
        Console.WriteLine("The minimum number in the array is: " + min);
    }
}

In the above code:

  • We initialize min with the first element of the array.
  • We iterate through the array starting from the second element.
  • In each iteration, we compare the current element with the current minimum (min).
  • If the current element is less than the current minimum, we update min with the value of the current element.
  • Finally, we print out the minimum number after the loop has finished iterating through the array.