Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an interactive program that computes the area of a square or a triangle. T

ID: 3537459 • Letter: W

Question

Write an interactive program that computes the area of a square or a triangle. The user is first

prompted to enter the first character of the figure type (S or T). Then the user is prompted to enter the side length in the case of a square or the base and height in the case of a triangle. The program outputs the area of the figure on the screen.

Sample program execution: Please enter the first letter of the shape type>TEnter the height and the base of the triangle>6 9

The area of the triangle is 30. Do you want to continue (y/n)?

Explanation / Answer

#include<stdio.h>
int main()
{
float a,b,h,area;
int i;
char type,check;
void clear(void);
while(1)
{     
     
      printf("Please enter the first letter of the shape type>");
     
     scanf("%c",&type);
     while(type!='S' && type!='T')
     {
      printf("wrong Choice ");
      printf("Please enter the first letter of the shape type>");
      clear();   
      scanf("%c",&type);
     }
     if(type=='S')
     {
      printf("enter the side length>");
      clear();
     
      scanf("%f",&a);
      area=a*a;
      printf("The area of the square is %f ",area);
     }
     else
     {
      
      printf("Enter the height and the base of the triangle>");
      clear();
      scanf("%f%f",&h,&b);
      area=(b*h)/2;
      printf("The area of the triangle is %f ",area);
     }
    
     printf("Do you want to continue(y/n)");
     clear();
     scanf("%c",&check);
     while(check!='y' && check!='n')
     {
     
      printf("wrong Choice ");
      printf("Do you want to continue(y/n)>");
      clear();
      scanf("%c",&check);
     }
     if(check=='n')
     break;
     clear();
}
}
// to avoid
void clear (void)
{   
while ( getchar() != ' ' );
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote