Home

A program to explain SWITCH CASE DEFAULT with break

C Programming Language 023

The following code shows how to write a program to explain SWITCH CASE DEFAULT with break. To find area of circle, square rectangle and triangle.

Program

#include <stdio.h>

void main()
{
    int choice;
    float a, b, area;

    printf("Enter your choice\n\
1 - Circle\n\
2 - Square\n\
3 - Rectangle\n\
4 - Triangle\n\
");

    scanf("%d", &choice);

    switch (choice)
    {
    case 1:
        printf("Enter radius: ");
        scanf("%f", &a);
        area = 3.14 * a * a;
        printf("Area = %f", area);
        break;
    case 2:
        printf("Enter side: ");
        scanf("%f", &a);
        area = a * a;
        printf("Area = %f", area);
        break;
    case 3:
        printf("Enter sides: ");
        scanf("%f %f", &a, &b);
        area = a * b;
        printf("Area = %f", area);
        break;
    case 4:
        printf("Enter base, height: ");
        scanf("%f %f", &a, &b);
        area = a * b / 2.0;
        printf("Area = %f", area);
        break;

    default:
        printf("Choice invalid");
    }

    printf("\n");
}

Result

Enter your choice
1 - Circle
2 - Square
3 - Rectangle
4 - Triangle
2
Enter side: 3
Area = 9.000000


Last Updated on

Next Post: Rules vs Guidelines →

Comments