# Tag Archives: Switch

## C program to check positive negative or zero using switch case

Write a C program to input a number and check positive negative or zero using switch case. Checking negative, positive or zero using switch case is little tricky. In this example, I will explain how to check positive negative or zero using switch case. However, it is not recommended way, it's just for learning.

## If…else…if vs switch…case, difference and usage?

`if...else...if` and `switch...case` both programming constructs has ability to take decision based on conditions. Both are almost similar in nature. However, there is always a debate among beginners which to use and when to use what?

In this post, I will compare both on various grounds. So let us begin.

## switch…case statement in C

`if...else` statement provides support to control program flow. if statement make decisions based on conditions. It selects an action, if some condition is met. However, there exits situations where you want to make a decision from available choices. For example - select a laptop from available models, select a menu from available menu list etc.

`switch...case` statement gives ability to make decisions from fixed available choices. Rather making decision based on conditions. Using `switch` we can write a more clean and optimal code, that take decisions from available choices.

## C program to find all roots of a quadratic equation using switch case

Write a C program to find all roots of a Quadratic equation using switch case. How to find all roots of a quadratic equation using switch case in C programming. Logic to calculate roots of quadratic equation in C program.

Example
Input

```Input a: 4
Input b: -2
Input c: -10```

Output

```Root1: 1.85
Root2: -1.35```

## C program to create calculator using switch case and functions

Write a C program to create menu driven calculator that performs basic arithmetic operations (add, subtract, multiply and divide) using switch case and functions. The calculator should input two numbers and an operator from user. It should perform operation according to the operator entered and must take input in given format.

`<number 1> <operator> <number 2>`

Example
Input

`5.2 - 3`

Output

`2.2`

## C program to check even or odd number using switch case

Write a C program to input number from user and check whether the number is even or odd using switch case. How to check even or odd using switch case in C programming. Logic to check whether a number is even or odd using switch case in C program.

Example
Input

`Input number: 12`

Output

`Even number`

## Switch case programming exercises and solutions in C

`switch case` is a branching statement used to perform action based on available choices, instead of making decisions based on conditions. Using `switch case` you can write more clean and optimal code than if else statement.

`switch case` only works with integer, character and enumeration constants.

In this exercises we will focus on the use of `switch case` statement. We will learn where to implement `switch case` statement other than `if else` statement.

## C program to find maximum between two numbers using switch case

Write a C program to input two numbers from user and find maximum between two numbers using switch case. How to find maximum or minimum between two numbers using switch case. Logic to find maximum between two numbers using switch case in C programming.

Example
Input

```Input first number: 12
Input second number: 40```

Output

`Maximum: 40`

## C program to print number of days in a month using switch case

Write a C program to input month number and print total number of days in month using `switch...case`. C program to find total number of days in a month using `switch...case`. Logic to print number of days in a month using `switch...case` in C programming.

Example
Input

`Input month number: 3`

Output

`Total number of days = 31`

## C program to print day of week name using switch case

Write a C program to input week number(1-7) and print day of week name using switch case. C program to find week day name using switch case. How to find day name of week using switch case in C programming.

Example
Input

`Input week number(1-7): 2`

Output

`Tuesday`