C Program to find the Maximum of Two Numbers using Switch Case

program-to-find-maximum-of-two-numbers-using-switch-case

Maximum of two numbers using switch Case Program Description:

We have looked at the Program to find the Maximum of two numbers using the if else statement and conditional statements. In today’s article, We are going to write a C Program to find the maximum of two numbers using Switch Case.

The program should accept two numbers from the user and calculate the Maximum between them. If the user enters the same number, Then the program should output the numbers that are Equal

Expected Input and Output:

Example 1:

Program Input:

Enter two Numbers : 600 345

Program Output:

600 is Maximum

Example 2:

Program Input:

Enter two Numbers : 10 10

Program Output:

Numbers are Equal

Related Programs:

Pre Requisites:

It is recommended to know the basics of the switch statement to better understand the following program. Please go through the following article, which gives you the desired info about the switch statement.

Maximum of Two Numbers using Switch Case Program Explanation / Algorithm:

  1. Take the two numbers as Input from the user and store them in variables num1 and num2
  2. Then we are going to use the Switch statement to check the maximum. We are using switch(num1 >= num2) condition.
  3. This condition returns either 1 or . If the condition is True, Then it returns 1, If not then returns
  4. If the above condition is True, Then num1 is greater than or num1 and num2 are equal.
    • Now we need to further check if the num1 == num2 using another switch statement or switch case.
    • If the num1 == num2 returns True, Then num1 and num2 are Equal
    • If the above condition is False, Then num1 is Maximum.
  5. If the above condition is False, Then num2 is Maximum. So we can proceed further and print the result onto the Console.

Let’s convert the above algorithm to C code.

Maximum of Two Numbers using Switch Case Program:

Program Output:

Let’s compile and run the above program. We are using the GCC compiler to compile the program.

$ gcc maximum-of-two-switch.c

The above command produces the executable file which is a.out, Let’s run the program.

Test 1: When num1 is greater than num2:

Test 2: When num1 and num2 are Equal:

Test 3: With Negative Numbers

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] C Program to find the maximum of two numbers using a switch case […]

  2. […] C Program to find the maximum of two numbers using a switch case […]

Leave a Reply