Class Math from the System namespace of Microsoft NET Framew

Class Math (from the System namespace) of Microsoft .NET Framework Class Library has a static method Min(x, y). It returns the smaller value of x and y. Create a C# application to do the following. Ask the user to enter three values. Compare them and identify the smallest one. For example, if the user enters 4.7, 8 and 2.25, then program will report that 2.25 is the smallest value. Do not use if, if…else statement or conditional operator to compare values. Use the Math.Min method. You may need to use it more than once.

Solution

Logic used to find the minimum of the three numbers using the Math.Min() method is as follows:

1. Find the minimum of the first two numbers by using the Min() method and store the value in one variable.

2. Find the miimum of the obtained value from step 1 and the third number and store the value in another variable.

So, by doing the following process the minimum value is obtained with out using any conditional statements or conditional operators.

Thus, the minimum value is obtained.

Program code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace FindingMinValue

{

    class Program

    {

        static void Main(string[] args)

        {

            //declare the required variables

            double x, y, z, min1, min2;

            //prompt the user to enter three variable values

            Console.WriteLine(\"Enter the value of x = \");

            x = Double.Parse(Console.ReadLine());

            Console.WriteLine(\"Enter the value of y = \");

            y = Double.Parse(Console.ReadLine());

            Console.WriteLine(\"Enter the value of z = \");

            z = Double.Parse(Console.ReadLine());

            //first find the minimum of first two numbers

           min1 = Math.Min(x, y);

            //then find the minimum of the obtained min1 and remaining

            //value

            min2 = Math.Min(min1, z);

            //print the value

            Console.WriteLine(\"The minimum of (\" + x + \", \" + y +\", \"+ z + \") is \"+min2 );

            Console.Read();

        }

    }

}

Sample output:

Enter the value of x = 4.7

Enter the value of y = 8

Enter the value of z = 2.25

The minimum of <4.7, 8, 2.25> is 2.25

Class Math (from the System namespace) of Microsoft .NET Framework Class Library has a static method Min(x, y). It returns the smaller value of x and y. Create
Class Math (from the System namespace) of Microsoft .NET Framework Class Library has a static method Min(x, y). It returns the smaller value of x and y. Create

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site