Function Getminmax - C# Programming Exercise

In this exercise in C#, you will need to write a function named "GetMinMax", which will ask the user to enter a minimum value (a number) and a maximum value (another number). It should be called in a similar way to GetMinMax(n1, n2);. The function should first ask for the minimum value and then for the maximum value. If the entered maximum value is less than the minimum value, it should prompt the user to re-enter the maximum value. This exercise will help you handle user input in C# and apply value validation. You will also learn to work with parameters and ensure that the user inputs valid values. The goal of this exercise is to improve your skills in controlling user interaction and handling cases where the entered values are invalid.

By completing this exercise, you will be able to implement similar functions that interact with the user and validate input data in your C# programs.

 Category

Functions

 Exercise

Function Getminmax

 Objective

Write a C# function named "GetMinMax", which will ask the user for a minimum value (a number) and a maximum value (another number). It should be called in a similar way to

GetMinMax( n1, n2);

which would behave like this:
Enter the minimum value: 5
Enter the maximum value: 3.5
Incorrect. Should be 5 or more.
Enter the maximum value: 7

That is: it should ask for the minimum value and then for the maximum. If the maximum is less than the minimum, it must be reentered. It must return both values.

 Write Your C# Exercise

// Import the System namespace to use basic classes like Console
using System;

class Program
{
    // Function to ask for minimum and maximum values and validate that max >= min
    public static void GetMinMax(out double minValue, out double maxValue)
    {
        // Prompt the user to enter the minimum value
        Console.Write("Enter the minimum value: ");
        minValue = Convert.ToDouble(Console.ReadLine());

        // Ask for the maximum value, ensuring it's greater than or equal to the minimum value
        while (true)
        {
            Console.Write("Enter the maximum value: ");
            maxValue = Convert.ToDouble(Console.ReadLine());

            // If the maximum is less than the minimum, prompt the user to re-enter the maximum
            if (maxValue >= minValue)
            {
                break; // Exit the loop if the maximum is valid
            }
            else
            {
                Console.WriteLine($"Incorrect. Should be {minValue} or more.");
            }
        }
    }

    // Main function to test the GetMinMax function
    public static void Main()
    {
        double minValue, maxValue;

        // Call the GetMinMax function and capture the user input
        GetMinMax(out minValue, out maxValue);

        // Display the results
        Console.WriteLine($"The minimum value is: {minValue}");
        Console.WriteLine($"The maximum value is: {maxValue}");
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Function Multiply & MultiplyR

    In this exercise in C#, you will need to write two functions called "Multiply" and "MultiplyR" to calculate the product of two numbers using sums. The first ve...

  •  Functions: greeting + farewell

    In this C# exercise, you are asked to write a program where the main structure is the Main method. Inside this method, two functions should be called: SayHello...

  •  Function with parameters

    In this C# exercise, you are asked to write a program where the Main method must be like this:public static void Main(){ SayHello("John"...

  •  Function returning a value

    In this C# exercise, you are asked to write a program where the Main method must be like this:public static void Main(){ int x = 3;

  •  Function returning a value V2

    In this C# exercise, you are asked to write a program where the Main method must be like this:public static void Main(){ Console.WriteLi...

  •  Function write centered

    In this C# exercise, you are asked to write a function that displays the given text centered on the screen (assuming a screen width of 80 characters). The code should...