Skip to content
Solved Examples · Example 26

Q.Write a C program to illustrate the switch statement for computing the area of different geometrical figures such as a circle, a square, a triangle, a rectangle, etc. The program should display the menu of figure codes of different figures. On choosing a particular figure code, the corresponding parameters required by that figure are accepted and processed. Finally, the area should be printed. The textbook presents this worked program, transcribed exactly as printed (including its printed errors):
#include<stdio.h>  
void main()  
{  
in fig_code;  
float side, base, length, breadth, height, area, radius;  
printf("--------\n");  
printf("1 Circle\n");  
printf("2 Rectangle\n");  
printf("3 Square\n");  
printf("4 Triangle\n");  
printf("--------\n");  
printf("Enter the figure code\n");  
scanf("%d, &fig_code);  
switch(fig_code)  
{  
    case 1: printf("Enter the radius\n");  
            scanf("%f", &radius);  
            area=3.142*radius*radius;  
            printf("Area of a Circle=%f\n", area);  
            break;  
    case 2: printf("Enter the breadth and length\n");  
            scanf("%f%f", &breadth, &length\n");  
            area=breadth*length;  
            printf("Area of a Rectangle=%f\n", area);  
            break;  
    case 3: printf("Enter the side\n");  
            scanf("%f%, &side\n");  
            area=side*side;  
            printf("Area of a Square=%f\n", area);  
            break;  
    case 4: printf("Enter the base and height\n");  
            scanf("%f%f", &base, &height\n);  
            area=0.5*base*height;  
            printf("Area of a Triangle=%f\n", area);  
            break;  
}  
/*End of switch()*/  
}  
/*End of main()*/  
Sample output printed in the textbook:
Run1                     Run2  
1. Circle                1. Circle  
2. Rectangle             2. Rectangle  
3. Square                3. Square  
4. Triangle              4. Triangle  
Enter the figure code    Enter the figure code  
3                        6  
Enter the side           Error in figure code  
5.0  
Area of a Square=25.000000  

Karnataka PUCTextbookLongImportance★★★★★est
78% · 120/154 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

[!TLDR]

A switch on the figure code selects the area formula for circle/rectangle/square/triangle; side 5.0 (code 3) gives 25.0, and an unknown code hits default.

The switch statement is multi-way branching driven by an integer (or char) value. Control jumps to the case label equal to that value, runs its statements, and the break ends the switch so execution does not fall through into the next case; the default label handles any value that matches no case - here it produces the "Error in figure code" message the book's own run shows. …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.