C Program to find the 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:
- Take the two numbers as Input from the user and store them in variables num1 and num2
- Then we are going to use the Switch statement to check the maximum. We are using switch(num1 >= num2) condition.
- This condition returns either 1 or . If the condition is True, Then it returns 1, If not then returns
- 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.
- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/* Program: Maximum of two number using switch case */ #include<stdio.h> int main() { // Take the input from the user int num1, num2; printf("Enter two Numbers : "); scanf("%d%d", &num1, &num2); // we have already covered max of two numbers program using if else // we are going to use switch case here. switch(num1 >= num2) { // If 'num1 >= num2', Then 'num2' is Maximum case 0: printf("%d is Maximum \n", num2); break; // We can also use default case here. case 1: // num1 is greater than or num1 and num2 are equal // let's check if num1 == num2 using switch case switch(num1 == num2) { case 1: printf("Numbers are Equal \n"); break; // We can also use default case here. case 0: printf("%d is Maximum \n", num1); break; } break; } return 0; } |
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:
1 2 3 |
$ ./a.out Enter two Numbers : 50 15 50 is Maximum |
Test 2: When num1 and num2 are Equal:
1 2 3 |
$ ./a.out Enter two Numbers : 10 10 Numbers are Equal |
Test 3: With Negative Numbers
1 2 3 4 |
$ ./a.out Enter two Numbers : -10 -20 -10 is Maximum $ |
2 Responses
[…] C Program to find the maximum of two numbers using a switch case […]
[…] C Program to find the maximum of two numbers using a switch case […]