Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
Switch case for comparing which number is higher given by user C#

I want to write a code in C# that asks a user to input 2 numbers to compare which is higher but cant seem to get an output from this.

{ class Program {

    public static int Max { get; private set; }

    static void Main(string[] args)
    {
        Console.WriteLine("Please enter 2 (two) integer numbers on a separate line: ");

        int num1 = int.Parse(Console.ReadLine());
        int num2 = int.Parse(Console.ReadLine());


        switch (Max)
        {
            case 1:
                if (num1 < num2)
                {
                    Console.WriteLine(num2 + "is Maximum");
                }
                break;


            case 2:
                if (num1 > num2)
                {
                    Console.WriteLine(num1 + "is Maximum");
                }
                break;



        }
    }
}

}

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You are using the "Max" variable on the condition of the switch, but you never actually declare "Max", and the switch is only expectating the numbers 1 or 2, because you put case 1: and case 2:

So the correct way to show output is this:

if(num1 > num2)
{
    Max = 1;
}
else
{
    Max = 2;
}

switch(Max)
{
    case 1:
        Console.WriteLine(num1 + "is Maximum");
        break;
    case 2:
        Console.WriteLine(num2 + "is Maximum");
        break;
}

This is just in case you need to use a switch case, because you can do it only with a simple if

over 4 years ago · Santiago Trujillo Report

0

case cannot be used to compare values of two numbers you might just want to use if-else statement or if you just want a application of case statement use this :

switch (input)
{
    case 1:
        Console.WriteLine("a");
        break;

    case 2:
        Console.WriteLine("b");
        break;
    case 3:
        Console.WriteLine("c");
        break;
    case 4:
        Console.WriteLine("d");
        break;
    case 5:
        Console.WriteLine("e");
        break;
    case 6:
        Console.WriteLine("f");
        break;
    case 7:
        Console.WriteLine("g");
        break;
    default:
        Console.WriteLine("Invalid input");
        break;

}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!