Blogger templates

Pages

Thursday, 14 June 2012

C Program: Make a Calculator that Calculates Area of different Figures by using switch Statement

Calculator

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int fig_code;

float radius ;                                            /* For Circle */
float length , breadth ;                              /* For Rectangle */
float base , height ;                                   /* For Triangle */
float side ;                                               /* For Square */
float base1 , height1 ;                               /* For parallelogram */
float base2 , base3 , height2 ;                    /* For Trapezoid */
                                            

printf("................../n");
printf("1 Circle\n");
printf("2 Rectangle\n");
printf("3 Triangle\n");
printf("4 Square\n");
printf("5 Parallelogram\n");
printf("6 Trapezoid\n");
printf(" Enter the Figure code\n");
scanf("%d" , &fig_code\n");
switch(fig_code)
{
  Area of Circle
case1:                                                                                  
printf("Enter the Radius\n");
scanf("%f" , &radius);
Area= 3.142*radius*radius;
printf("Area of Circle = %f\n" , Area);
break;
                                                                                            
Area of Rectangle
case 2:
printf("Enter the breadth and length\n");                           
scanf("%f%f" ,&breadth, &length);
Area= breadth*length;
printf("Area of Rectangle = %f\n" , Area);
break;

Area of Triangle
case 3:                                                              
printf("Enter the base and height\n");                     
scanf("%f%f" , &base , &height);                                  
area= 0.5*base*height;                                                           
printf("Area of Triangle = %f\n" , area);
break;

Area of Square
case 4:
printf("Enter the Side\n");
scanf("%f" , &side);                                                         
area= side*side;
printf("Area of Square = %f\n" , area);
break;
                                                                               
Area of Parallelogram
case 5:
printf("Enter the base1 and height1\n");                           
scanf("%f%f" , &base1 , &height1 );            
Area= base1 * height1;
printf("Area of Parallelogram = %f\n" , Area);
break;

Area of Trapezoid

case 6:
printf("Enter the base2 , base3 and height2\n");                           
scanf("%f%f" , &base1 , &base2 , &height1 );                         
Area=0.5*( base1 + base2 ) * height2;
printf("Area of Trapezoid = %f\n" , Area);
break;

default:
printf("Error in the figure code\n");
break;

}                                             /* End of switch */
}                                             /* End of main() */

Output:
....................
1 Circle
2 Rectangle
3 Triangle
4 Square
5 Parallelogram
6 Trapezoid
....................
Enter the Figure Code
4
Enter the side
2
Area of a square = 4



                                                   Did you check the following?                                                
                                         
                            C Program: Make a Calculator by using switch Statement


0 comments:

Post a Comment