w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: A menu-driven program to compute the area of the various geometrical shape


C# Sharp Conditional Statement: Exercise-24 with Solution

Write a program in C# Sharp which is a Menu-Driven Program to compute the area of the various geometrical shape.

C# Sharp: A menu-driven program to compute the area of the various geometrical shape

Sample Solution:-

C# Sharp Code:

using System;  
public class Exercise24  
{  
    public static void Main()
{
      int choice,r,l,w,b,h;
      double area=0;

           Console.Write("\n\n");
           Console.Write("A menu driven program to compute the area of various geometrical shape:\n");
           Console.Write("-------------------------------------------------------------------------");
           Console.Write("\n\n");


      Console.Write("Input 1 for area of circle\n");
      Console.Write("Input 2 for area of rectangle\n");
      Console.Write("Input 3 for area of triangle\n");
      Console.Write("Input your choice : ");
      choice = Convert.ToInt32(Console.ReadLine());

      switch(choice)
      {
           case 1:
                 Console.Write("Input radius of the circle : ");
                 r = Convert.ToInt32(Console.ReadLine());
                    area=3.14*r*r;
                 break;
            case 2:
                  Console.Write("Input length  of the rectangle : ");
                  l = Convert.ToInt32(Console.ReadLine());
                  Console.Write("Input  width of the rectangle : ");
                  w = Convert.ToInt32(Console.ReadLine());
                  area=l*w;
                  break;
            case 3:
                  Console.Write("Input the base of the triangle :");
                  b = Convert.ToInt32(Console.ReadLine());
                  Console.Write("Input the hight of the triangle :");
                  h = Convert.ToInt32(Console.ReadLine());
                         area=.5*b*h;
                  break;
          }
          Console.Write("The area is : {0}\n",area);
    }
}

Sample Output:

A menu driven program to compute the area of various geometrical shape:                                       
-------------------------------------------------------------------------                      
Input 1 for area of circle                                                                                    
Input 2 for area of rectangle                                                                                 
Input 3 for area of triangle                                                                                  
Input your choice : 2                                                                                         
Input length  of the rectangle : 20                                                                           
Input  width of the rectangle : 30                                                                            
The area is : 600 

Flowchart:

Flowchart: A menu driven program to compute the area of various geometrical shape

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to read any Month Number in integer and display the number of days for this month.
Next: Write a program in C# Sharp which is a Menu-Driven Program to perform a simple calculation.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming