Skip to content

C programs on Switch-case

Practice programs on Switch case

1. C program to find all roots of a quadratic equation using switch case:

#include <stdio.h>
#include <math.h> /* Used for sqrt() */
 
int main()
{
    float a, b, c;
    float root1, root2, imaginary;
    float discriminant;
 
    printf("Enter values of a, b, c of quadratic equation (aX^2 + bX + c): ");
    scanf("%f%f%f", &a, &b, &c);
 
    /* Calculate discriminant */
    discriminant = (b * b) - (4 * a * c);
 
    /* Compute roots of quadratic equation based on the nature of discriminant */
    switch(discriminant > 0)
    {
    case 1:
        /* If discriminant is positive */
        root1 = (-b + sqrt(discriminant)) / (2 * a);
        root2 = (-b - sqrt(discriminant)) / (2 * a);
 
        printf("Two distinct and real roots exists: %.2f and %.2f",root1, root2);
        break;
 
    case 0:
        /* If discriminant is not positive */
        switch(discriminant < 0)
        {
            case 1:
                /* If discriminant is negative */
                root1 = root2 = -b / (2 * a);
                imaginary = sqrt(-discriminant) / (2 * a);
 
                printf("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f",root1, imaginary, root2, imaginary);
                break;
 
            case 0:
            /* If discriminant is zero */
                root1 = root2 = -b / (2 * a);
                printf("Two equal and real roots exists: %.2f and %.2f", root1, root2);
                break;
        }
    }
 
    return 0;
}

******************************************************************

2. C program to create calculator using switch case:

#include <stdio.h>
 
int main()
{
    char op;
    float num1, num2, result=0.0f;
 
    /* Print welcome message */
    printf("WELCOME TO SIMPLE CALCULATOR\n");
    printf("----------------------------\n");
    printf("Enter [number 1] [+ - * /] [number 2]\n");
 
    /* Input two number and operator from user */
    scanf("%f %c %f", &num1, &op, &num2);
 
    /* Switch the value and perform action based on operator*/
    switch(op)
    {
        case '+': 
            result = num1 + num2;
            break;
 
        case '-': 
            result = num1 - num2;
            break;
 
        case '*': 
            result = num1 * num2;
            break;
 
        case '/': 
            result = num1 / num2;
            break;
 
        default: 
            printf("Invalid operator");
    }
 
    /* Prints the result */
    printf("%.2f %c %.2f = %.2f", num1, op, num2, result);
 
    return 0;
}

*******************************************************************

3. Write a Program that does the following:

User to type his own ID, if the ID is valid it will ask him to enter his password,
if the password is correct the program will print the name of the user,

otherwise ,the program will print Incorrect Password and if the ID does not exist, the program will print Incorrect ID

#include <stdio.h>
int main() 
{
        int ID = 500;
        int password = 000;
        printf("Plese Enter Your ID:\n ");
        scanf("%d", & ID);
        switch (ID) 
        {
            case 500:
                printf("Enter your password:\n ");
                scanf("%d", &password);
                switch (password) 
                {
                    case 000:
                        printf("Welcome Dear Programmer\n");
                        break;
                    default:
                        printf("incorrect password");
                        break;
                }
                break;
            default:
                printf("incorrect ID");
                break;
        }
}

********************************************************************

For more practice questions, go to  Questionnare.

Leave a Reply

Your email address will not be published. Required fields are marked *