Kianmehr K.
Kianmehr K.

Reputation: 17

A program that gets 3 digits and outputs the smallest one in C# Console App

How can I make a program in C# that gets 3 digits from the user and outputs the smallest one? It's gonna be as a Console App.

I tried this and gave me an error (I may be stupid):

if (a<b<c)
{
min=a;
Console.WriteLine("Min: " + min);

I don't now what else should I do, I'm new to C#.

Upvotes: 0

Views: 71

Answers (3)

Here's a solution that should work:

int a, b, c, min;

Console.WriteLine("Please enter three digits:");
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
c = int.Parse(Console.ReadLine());

min = Math.Min(Math.Min(a, b), c);

Console.WriteLine("Min: " + min);

Upvotes: 0

Sergey
Sergey

Reputation: 640

Try this.

if (a < b && a < c)
{
    Console.WriteLine("Min: " + a);
}
else if (b < c)
{
    Console.WriteLine("Min: " + b);
}
else
{
    Console.WriteLine("Min: " + c);
}

Upvotes: 0

Austin
Austin

Reputation: 2265

There's nothing wrong with being new, and you aren't stupid just because you aren't sure how something works.

Think of it like this:

We need to have a variable to hold this minimum value:

int min;

First, you need to compare two values to get the smallest between them:

if (a < b)
    min = a;
else
    min = b;

Now that you have the minimum between those two, compare that value to your third input:

if (c < min)
    min = c;

If c is less than the current min value, you adjust to c, otherwise you already had your minimum value in the first comparison.

Here is a full example for you to play with as well:

int a = 4;
int b = 2;
int c = 1;

int min;

if (a < b)
    min = a;
else
    min = b;

if (c < min)
    min = c;

Console.WriteLine("Lowest value is {0}", min);

Upvotes: 3

Related Questions