Feb 26, 2020

C Program To Print Maximum Number Between Two Numbers Using Switch Case

Ex: write a C program to find maximum number between two numbers without using if-else using switch case. How to write a C program to find maximum number between two numbers without using if-else using switch case.
c program to print maximum out of two numbers without using if-else statement using Switch Case.


Input from user:

Enter value of a: 5

Enter value of b: 15

Expected Output:

Maximum is b=15.


In below code we will print max out of two numbers without using if-else statement's. Here we will use switch-case to find the max number of given two numbers.




  Step by Step Logic of the program:

1. First we will accept two numbers from user which say a & b.


2. After that we will store the output of a/b in the switch. Switch (a/b)

3. If output in zero then case 0 will work means max is b.


4. Otherwise in default- a is max.




  C Program To Print Maximum Number  Between Two Numbers  Using Switch Case:


#include<stdio.h>

int main()
{
  int a,b;


printf("enter value of a\n");

scanf("%d",&a);
printf("enter value of b\n");

scanf("%d",&b);

  switch(a/b)

  {
   case 0:

   printf("max is b=%d",b);
   break;

  default:

   printf("max is a=%d",a);

}

  return 0;


}


Above Program show's the following output:



C Program To Print Maximum Number  Between Two Numbers  Using Switch Case




No comments:

Post a Comment

If you have any doubts, please discuss here...