User gives 3 numbers, program gives you the bigger one out of the three

Lsnso

New member
Joined
Aug 10, 2019
Messages
1
Programming Experience
Beginner
Hey!
So I've been following the freeCodeCamp.org C# tutorial (Youtube). As I'm learning about if statements, the host says he'll create a little program that, given 3 numbers, gives you the bigger one out of the three. As the clearly superior individual I am, I try to do it by myself before seeing his suggestion/walkthrough. My program was a bit different as I used user input instead of simply choosing my 3 numbers as I write the code and then running the program. So my idea was that the user would enter three numbers (this way: 123 and not 1 2 3). Then I'd identify them using their index numbers (for example 1 would be arr[0]). Then I'd use them for my function ( MaxNum(arr[0] , arr[1] , arr[2]) ) and I'd see the biggest in the console. Problem is, if I Console.Write them they everything works fine but as soon as I use the function the numbers change. For instance, for the numbers 123, 1 now becomes 49, 2 becomes 50, 3 becomes 51 and so 51 is printed. I have "Console.Written" all of them ( arr[0], arr[1], arr[2] ) and they come up as 1 2 and 3. My code looks something like this: If anyone could help I'd appreciate, thank you :D Yes, im a complete noob at this.

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpLearning
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter three numbers: ");
            string arr = Console.ReadLine();

            Console.WriteLine("The biggest number is: " + MaxNum(arr[0], arr[1], arr[2]));
          
            Console.ReadLine();
        }

        static int MaxNum(int num1, int num2, int num3)
        {
            int result;

            if (num1 >= num2 && num1 >= num3)
            {
                result = num1;
            }
            else if (num2 >= num1 && num2 >= num3)
            {
                result = num2;
            }
            else if (num3 >= num1 && num3 >= num1)
            {
                result = num3;
            }
            else
            {
                result = num1;
            }

            return result;
        }
    }
}
 
Last edited by a moderator:
The problem is that you are playing fast and loose with data types. Your MaxNum method expects three int values but that's not what you're passing here:
C#:
string arr = Console.ReadLine();

Console.WriteLine("The biggest number is: " + MaxNum(arr[0], arr[1], arr[2]));
Firstly, why would you name a string variable arr? That name implies that it's a array but it's not. The problem is that you are treating it like it's an int array and assuming that arr[0], etc, are the numbers the user entered as int values. They are not. arr is type string so arr[0] is type char. In order to convert that to the required int, the system is getting the Unicode point value of that character. Guess what the Unicode point value of the character '1' is.

What needs to happen is that you need to convert the char to the int value you expect explicitly. To do that, you would convert the char to a string and then parse that. There are various ways to perform the parsing, many of which will implicitly convert a char to a string but, if you use Convert.ToInt32, you'll need to perform the conversion explicitly because that method will convert a char to its Unicode point value.
 
After you have converted the strings to ints you can use Math.Max Method (System) or Enumerable.Max Method (System.Linq) to get the max value. The first is used for two numbers, the latter for a list or array.
If you insist on implementing max yourself that can also be writter much simpler with terary conditional operator like this:
C#:
var result = first > second ? first : second;
result = result > third ? result : third;
 
Back
Top Bottom