C# Conditional Statements Examples

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

C# Programming C-Net, Koel Nagar

1. C# Program to Check whether the Entered Number is Even or Odd


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace check1
{
class Program
{
static void Main(string[] args)
{
int i;
Console.Write("Enter a Number : ");
i = int.Parse(Console.ReadLine());
if (i % 2 == 0)
{
Console.Write("Entered Number is an Even Number");
Console.Read();
}
else
{
Console.Write("Entered Number is an Odd Number");
Console.Read();
}
}
}
}

2. C# Program to Accept the Height of a Person & Categorize as Tall, Dwarf


or Average
using System;
class program
{
public static void Main()
{
float height;
Console.WriteLine("Enter the Height (in centimeters) \n");
height = int.Parse(Console.ReadLine());
if (height < 150.0)
Console.WriteLine("Dwarf \n");
else if ((height >= 150.0) && (height <= 165.0))
Console.WriteLine(" Average Height \n");
else if ((height >= 165.0) && (height <= 195.0))
Console.WriteLine("Taller \n");

Subrat Kumar Dash (7978718584) 1|P a ge


C# Programming C-Net, Koel Nagar
else
Console.WriteLine("Abnormal height \n");
}
}

3. C# Program to Find Greatest among 2 numbers


using System;
class prog
{
public static void Main()
{
int a, b;
Console.WriteLine("Enter the Two Numbers : ");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
if (a > b)
{
Console.WriteLine("{0} is the Greatest Number", a);
}
else
{
Console.WriteLine("{0} is the Greatest Number ", b);
}
Console.ReadLine();
}
}

4. C# Program to Generate Random Numbers


using System;
class Program
{
static void Main()
{
Console.WriteLine("Some Random Numbers that are generated are : ");
for (int i = 1; i < 10; i++)
{
Randfunc();
}
}
static Random r = new Random();
static void Randfunc()
{
int n = r.Next();
Console.WriteLine(n);
Subrat Kumar Dash (7978718584) 2|P a ge
C# Programming C-Net, Koel Nagar
Console.ReadLine();
}
}

5. C# Program to Check Whether the Entered Year is a Leap Year or Not


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
class leapyear
{
static void Main(string[] args)
{
leapyear obj = new leapyear();
obj.readdata();
obj.leap();
}
int y;
public void readdata()
{
Console.WriteLine("Enter the Year in Four Digits : ");
y = Convert.ToInt32(Console.ReadLine());
}
public void leap()
{
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
{
Console.WriteLine("{0} is a Leap Year", y);
}
else
{
Console.WriteLine("{0} is not a Leap Year", y);
}
Console.ReadLine();
}
}
}

Subrat Kumar Dash (7978718584) 3|P a ge


C# Programming C-Net, Koel Nagar

6. Write a C# Sharp program to accept two integers and check whether they
are equal or not.
using System;
public class Exercise1
{
public static void Main()
{
int int1,int2;
Console.Write("\n\n");
Console.Write("Check whether two integers are equal or not:\n");
Console.Write("-------------------------------------------");
Console.Write("\n\n");
Console.Write("Input 1st number: ");
int1= Convert.ToInt32(Console.ReadLine());
Console.Write("Input 2nd number: ");
int2= Convert.ToInt32(Console.ReadLine());
if (int1 == int2)
Console.WriteLine("{0} and {1} are equal.\n",int1,int2);
else
Console.WriteLine("{0} and {1} are not equal.\n",int1,int2);
}
}

7. C# Sharp program to check whether a given number is even or odd.


using System;
public class Exercise2
{
public static void Main()
{
int num1, rem1;
Console.Write("\n\n");
Console.Write("Check whether a number is even or odd :\n");
Console.Write("---------------------------------------");
Console.Write("\n\n");
Console.Write("Input an integer : ");
num1= Convert.ToInt32(Console.ReadLine());
rem1 = num1 % 2;
if (rem1 == 0)
Console.WriteLine("{0} is an even integer.\n",num1);
else
Console.WriteLine("{0} is an odd integer.\n",num1);
}
}

Subrat Kumar Dash (7978718584) 4|P a ge


C# Programming C-Net, Koel Nagar

8. Write a C# Sharp program to check whether a given number is positive or


negative.
using System;
public class Exercise3
{
public static void Main()
{
int num;
Console.Write("\n\n");
Console.Write("Check whether a number is positive or negative:\n");
Console.Write("----------------------------------------------");
Console.Write("\n\n");
Console.Write("Input an integer : ");
num= Convert.ToInt32(Console.ReadLine());
if (num >= 0)
Console.WriteLine("{0} is a positive number.\n",num);
else
Console.WriteLine("{0} is a negative number. \n", num);
}
}

9. C# Sharp program to read the age of a candidate and determine whether it


is eligible for casting his/her own vote
using System;
public class Exercise5
{
public static void Main()
{
int vote_age;
Console.Write("\n\n");
Console.Write("Detrermine a specific age is eligible for casting the vote:\n");
Console.Write("----------------------------------------------------------");
Console.Write("\n\n");
Console.Write("Input the age of the candidate : ");
vote_age= Convert.ToInt32(Console.ReadLine());
if (vote_age<18)
{
Console.Write("Sorry, You are not eligible to caste your vote.\n");
Console.Write("You would be able to caste your vote after {0} year.\n\n",18-vote_age);
}
else
Console.Write("Congratulation! You are eligible for casting your vote.\n\n");
}
}
Subrat Kumar Dash (7978718584) 5|P a ge
C# Programming C-Net, Koel Nagar

10. C# Sharp program to read the value of an integer m and display the value
of n is 1 when m is larger than 0, 0 when m is 0 and -1 when m is less than 0.
using System;
public class Exercise6
{
public static void Main()
{
int m,n;
Console.Write("\n\n");
Console.Write("Display the value of n is 1,0 and -1 for the value of er m:\n");
Console.Write("----------------------------------------------------------");
Console.Write("\n\n");
Console.Write("Input the value of m :");
m= Convert.ToInt32(Console.ReadLine());
if(m!=0)
if(m>0)
n=1;
else
n=-1;
else
n=0;
Console.Write("The value of m = {0} \n",m);
Console.Write("The value of n = {0} \n\n",n);
}
}

11. Write a C# Sharp program to find the largest of three numbers


using System;
public class Exercise8
{
public static void Main()
{
int num1, num2, num3;
Console.Write("\n\n");
Console.Write("Find the largest of three numbers:\n");
Console.Write("------------------------------------");
Console.Write("\n\n");
Console.Write("Input the 1st number :");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the 2nd number :");
num2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the 3rd number :");
num3 = Convert.ToInt32(Console.ReadLine());

Subrat Kumar Dash (7978718584) 6|P a ge


C# Programming C-Net, Koel Nagar
if (num1 > num2)
{
if (num1 > num3)
{
Console.Write("The 1st Number is the greatest among three. \n\n");
}
else
{
Console.Write("The 3rd Number is the greatest among three. \n\n");
}
}
else if (num2 > num3)
Console.Write("The 2nd Number is the greatest among three \n\n");
else
Console.Write("The 3rd Number is the greatest among three \n\n");
}
}

12. C# Sharp program to accept a coordinate point in an XY coordinate


system and determine in which quadrant the coordinate point lies.
using System;
public class Exercise9
{
public static void Main()
{
int co1,co2;
Console.Write("\n\n");
Console.Write("Find the quadrant in which the coordinate point lies:\n");
Console.Write("------------------------------------------------------");
Console.Write("\n\n");
Console.Write("Input the value for X coordinate :");
co1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the value for Y coordinate :");
co2 = Convert.ToInt32(Console.ReadLine());

if( co1 > 0 && co2 > 0)


Console.Write("The coordinate point ({0} {1}) lies in the First
quandrant.\n\n",co1,co2);
else if( co1 < 0 && co2 > 0)
Console.Write("The coordinate point ({0} {1}) lies in the Second
quandrant.\n\n",co1,co2);
else if( co1 < 0 && co2 < 0)
Console.Write("The coordinate point ({0} {1}) lies in the Third
quandrant.\n\n",co1,co2);
else if( co1 > 0 && co2 < 0)
Console.Write("The coordinate point ({0} {1}) lies in the Fourth
quandrant.\n\n",co1,co2);
Subrat Kumar Dash (7978718584) 7|P a ge
C# Programming C-Net, Koel Nagar
else if( co1 == 0 && co2 == 0)
Console.Write("The coordinate point ({0} {1}) lies at the origin.\n\n",co1,co2);

}
}

13. Write a C# Sharp program to find the eligibility of admission for a


professional course based on the following criteria:
Marks in Maths >=65
Marks in Phy >=55
Marks in Chem>=50
Total in all three subject >=180
or
Total in Math and Subjects >=140

using System;
public class Exercise10
{
public static void Main()
{
int p,c,m;

Console.Write("\n\n");
Console.Write("Find eligibility for admission :\n");
Console.Write("----------------------------------");
Console.Write("\n\n");
Console.Write("Eligibility Criteria :\n");
Console.Write("Marks in Maths >=65\n");
Console.Write("and Marks in Phy >=55\n");
Console.Write("and Marks in Chem>=50\n");
Console.Write("and Total in all three subject >=180\n");
Console.Write("or Total in Maths and Physics >=140\n");
Console.Write("-------------------------------------\n");
Console.Write("Input the marks obtained in Physics :");
p = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the marks obtained in Chemistry :");
c = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the marks obtained in Mathematics :");
m = Convert.ToInt32(Console.ReadLine());
Console.Write("Total marks of Maths, Physics and Chemistry : {0}\n",m+p+c);
Console.Write("Total marks of Maths and Physics : {0}\n",m+p);

if (m>=65)
if(p>=55)
if(c>=50)
if((m+p+c)>=180||(m+p)>=140)
Subrat Kumar Dash (7978718584) 8|P a ge
C# Programming C-Net, Koel Nagar
Console.Write("The candidate is eligible for admission.\n");
else
Console.Write("The candidate is not eligible.\n\n");
else
Console.Write("The candidate is not eligible.\n\n");
else
Console.Write("The candidate is not eligible.\n\n");
else
Console.Write("The candidate is not eligible.\n\n");
}
}

14. Write a C# Sharp program to calculate root of Quadratic Equation.


using System;
public class Exercise11
{
public static void Main()
{
int a,b,c;
double d, x1,x2;
Console.Write("\n\n");
Console.Write("Calculate root of Quadratic Equation :\n");
Console.Write("----------------------------------------");
Console.Write("\n\n");
Console.Write("Input the value of a : ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the value of b : ");
b = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the value of c : ");
c = Convert.ToInt32(Console.ReadLine());
d=b*b-4*a*c;
if(d==0)
{
Console.Write("Both roots are equal.\n");
x1=-b/(2.0*a);
x2=x1;
Console.Write("First Root Root1= {0}\n",x1);
Console.Write("Second Root Root2= {0}\n",x2);
}
else if(d>0)
{
Console.Write("Both roots are real and diff-2\n");

x1=(-b+Math.Sqrt(d))/(2*a);
x2=(-b-Math.Sqrt(d))/(2*a);

Subrat Kumar Dash (7978718584) 9|P a ge


C# Programming C-Net, Koel Nagar
Console.Write("First Root Root1= {0}\n",x1);
Console.Write("Second Root root2= {0}\n",x2);
}
else
Console.Write("Root are imeainary;\nNo Solution. \n\n");
}
}

15. Write a C# Sharp program to read roll no, name and marks of three
subjects and calculate the total, percentage and division.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Exercise12
{
static void Main(string[] args)
{
double rl,phy,che,ca,total;
double per;
string nm,div;
Console.Write("\n\n");
Console.Write("Calculate the total, percentage and division to take marks of three
subjects:\n");
Console.Write("-------------------------------------------------------------------------------");
Console.Write("\n\n");
Console.Write("Input the Roll Number of the student :");
rl = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the Name of the Student :");
nm = Console.ReadLine();
Console.Write("Input the marks of Physics : ");
phy= Convert.ToInt32(Console.ReadLine());
Console.Write("Input the marks of Chemistry : ");
che = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the marks of Computer Application : ");
ca = Convert.ToInt32(Console.ReadLine());
total = phy+che+ca;
per = total/3.0;
if (per>=60)
div="First";
else
if (per<60&&per>=48)
div="Second";
else
if (per<48&&per>=36)
div="Pass";
Subrat Kumar Dash (7978718584) 10 | P a g e
C# Programming C-Net, Koel Nagar
else
div="Fail";

Console.Write("\nRoll No : {0}\nName of Student : {1}\n",rl,nm);


Console.Write("Marks in Physics : {0}\nMarks in Chemistry : {1}\nMarks in Computer
Application : {2}\n",phy,che,ca);
Console.Write("Total Marks = {0}\nPercentage = {1}\nDivision = {2}\n",total,per,div);
}
}

16. Write a C# Sharp program to read temperature in centigrade and display


a suitable message according to temperature state below :
Temp < 0 then Freezing weather
Temp 0-10 then Very Cold weather
Temp 10-20 then Cold weather
Temp 20-30 then Normal in Temp
Temp 30-40 then Its Hot
Temp >=40 then Its Very Hot
using System;
public class Exercise13
{
public static void Main()
{
int tmp;
Console.Write("\n\n");
Console.Write("Accept a temperature in centigrade and display a suitable message:\n");
Console.Write("--------------------------------------------------------------------");
Console.Write("\n\n");
Console.Write("Input days temperature : ");
tmp= Convert.ToInt32(Console.ReadLine());
if(tmp<0)
Console.Write("Freezing weather.\n");
else if(tmp<10)
Console.Write("Very cold weather.\n");
else if(tmp<20)
Console.Write("Cold weather.\n");
else if(tmp<30)
Console.Write("Normal in temp.\n");
else if(tmp<40)
Console.Write("Its Hot.\n");
else
Console.Write("Its very hot.\n");

}
}

Subrat Kumar Dash (7978718584) 11 | P a g e


C# Programming C-Net, Koel Nagar
17. Write a C# Sharp program to check whether an alphabet is a vowel or
consonant.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class exercise16
{
static void Main(string[] args)
{
char ch;
Console.Write("\n\n");
Console.Write("check whether the input alphabet is a vowel or not:\n");
Console.Write("-----------------------------------------------------");
Console.Write("\n\n");
Console.Write("Input an Alphabet (A-Z or a-z) : ");
ch = Convert.ToChar(Console.ReadLine().ToLower());
int i=ch;
if(i>=48 && i<=57)
{
Console.Write("You entered a number, Please enter an alpahbet.");
}
else
{
switch (ch)
{
case 'a':
Console.WriteLine("The Alphabet is vowel");
break;
case 'i':
Console.WriteLine("The Alphabet is vowel");
break;
case 'o':
Console.WriteLine("The Alphabet is vowel");
break;
case 'u':
Console.WriteLine("The Alphabet is vowel");
break;
case 'e':
Console.WriteLine("The Alphabet is vowel");
break;
default:
Console.WriteLine("The Alphabet is not a vowel");
break;
}
}
Console.ReadKey();
Subrat Kumar Dash (7978718584) 12 | P a g e
C# Programming C-Net, Koel Nagar
}
}

18. Write a C# Sharp program to calculate profit and loss on a transaction.


using System;
public class Exercise17
{
public static void Main()
{
int cprice,sprice, plamt;

Console.Write("\n\n");
Console.Write("Calculate profit and loss:\n");
Console.Write("----------------------------");
Console.Write("\n\n");
Console.Write("Input Cost Price: ");
cprice= Convert.ToInt32(Console.ReadLine());
Console.Write("Input Selling Price: ");
sprice= Convert.ToInt32(Console.ReadLine());
if(sprice>cprice)
{
plamt = sprice-cprice;
Console.Write("\nYou can booked your profit amount : {0}\n", plamt);
}
else if(cprice>sprice)
{
plamt = cprice-sprice;
Console.Write("\nYou got a loss of amount : {0}\n", plamt);
}
else
{
Console.Write("\nYou are running in no profit no loss condition.\n");
}
}
}

19. Write a program in C# Sharp to calculate and print the Electricity bill of
a given customer. The customer id., name and unit consumed by the user
should be taken from the keyboard and display the total amount to pay to
the customer. The charge are as follow :
Unit Charge/unit
upto 199 @1.20

Subrat Kumar Dash (7978718584) 13 | P a g e


C# Programming C-Net, Koel Nagar
200 and above but less than 400 @1.50
400 and above but less than 600 @1.80
600 and above @2.00

If bill exceeds Rs. 400 then a surcharge of 15% will be charged and the minimum bill
should be of Rs. 100/-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class exercise18


{
static void Main(string[] args)
{
int custid, conu;
double chg, surchg=0, gramt,netamt;
string connm;

Console.Write("\n\n");
Console.Write("Calculate Electricity Bill:\n");
Console.Write("----------------------------");
Console.Write("\n\n");

Console.Write("Input Customer ID :");


custid= Convert.ToInt32(Console.ReadLine());
Console.Write("Input the name of the customer :");
connm= Console.ReadLine();
Console.Write("Input the unit consumed by the customer : ");
conu= Convert.ToInt32(Console.ReadLine());
if (conu <200 )
chg = 1.20;
else if (conu>=200 && conu<400)
chg = 1.50;
else if (conu>=400 && conu<600)
chg = 1.80;
else
chg = 2.00;
gramt = conu*chg;
if (gramt>300)
surchg = gramt*15/100.0;
netamt = gramt+surchg;
if (netamt < 100)
netamt =100;
Console.Write("\nElectricity Bill\n");
Console.Write("Customer IDNO :{0}\n",custid);
Console.Write("Customer Name :{0}\n",connm);
Subrat Kumar Dash (7978718584) 14 | P a g e
C# Programming C-Net, Koel Nagar
Console.Write("unit Consumed :{0}\n",conu);
Console.Write("Amount Charges @Rs. {0} per unit :{1}\n",chg,gramt);
Console.Write("Surchage Amount :{0}\n",surchg);
Console.Write("Net Amount Paid By the Customer :{0}\n",netamt);
}
}

20. Write a program in C# Sharp to accept a grade and declare the


equivalent description :
Grade Description
E Excellent
V Very Good
G Good
A Average
F Fail
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class exercise19


{
static void Main(string[] args)
{
string notes;
char grd;
Console.Write("\n\n");
Console.Write("Accept a grade and display equivalent description:\n");
Console.Write("---------------------------------------------------");
Console.Write("\n\n");

Console.Write("Input the grade :");


grd = Convert.ToChar(Console.ReadLine().ToUpper());

switch(grd)
{
case 'E':
notes= " Excellent";
break;
case 'V':
notes= " Very Good";
break;
case 'G':
notes= " Good ";
break;
Subrat Kumar Dash (7978718584) 15 | P a g e
C# Programming C-Net, Koel Nagar
case 'A':
notes= " Average";
break;
case 'F':
notes= " Fails";
break;
default :
notes= "Invalid Grade Found.";
break;
}
Console.Write("You have chosen : {0}\n", notes);
}
}

21. Write a program in C# Sharp to read any day number in integer and
display day name in the word.

using System;
public class Exercise20
{
public static void Main()
{
int dayno;

Console.Write("\n\n");
Console.Write("Accept day number and display its equivalent day name in word:\n");
Console.Write("----------------------------------------------------------------");
Console.Write("\n\n");

Console.Write("Input Day No : ");


dayno = Convert.ToInt32(Console.ReadLine());

switch(dayno)
{
case 1:
Console.Write("Monday \n");
break;
case 2:
Console.Write("Tuesday \n");
break;
case 3:
Console.Write("Wednesday \n");
break;
case 4:
Subrat Kumar Dash (7978718584) 16 | P a g e
C# Programming C-Net, Koel Nagar
Console.Write("Thursday \n");
break;
case 5:
Console.Write("Friday \n");
break;
case 6:
Console.Write("Saturday \n");
break;
case 7:
Console.Write("Sunday \n");
break;
default:
Console.Write("Invalid day number. \nPlease try again ....\n");
break;
}
}
}

22. Write a program in C# Sharp to read any digit, display in the word.
using System;
public class Exercise21
{
public static void Main()
{
int cdigit;

Console.Write("\n\n");
Console.Write("Accept digit and display in word:\n");
Console.Write("-----------------------------------");
Console.Write("\n\n");

Console.Write("Input Digit(0-9) : ");


cdigit = Convert.ToInt32(Console.ReadLine());

switch(cdigit)
{
case 0:
Console.Write("Zero\n");
break;

case 1:
Console.Write("one\n");
break;
case 2:

Subrat Kumar Dash (7978718584) 17 | P a g e


C# Programming C-Net, Koel Nagar
Console.Write("Two\n");
break;
case 3:
Console.Write("Three\n");
break;
case 4:
Console.Write("Four\n");
break;
case 5:
Console.Write("Five\n");
break;
case 6:
Console.Write("Six\n");
break;
case 7:
Console.Write("Seven\n");
break;
case 8:
Console.Write("Eight\n");
break;
case 9:
Console.Write("Nine\n");
break;
default:
Console.Write("invalid digit. \nPlease try again ....\n");
break;
}
}
}

23. Write a program in C# Sharp to read any Month Number in integer and
display Month name in the word.
using System;
public class Exercise22
{
public static void Main()
{
int monno;

Console.Write("\n\n");
Console.Write("Read month number and display month name:\n");
Console.Write("-------------------------------------------");
Console.Write("\n\n");

Subrat Kumar Dash (7978718584) 18 | P a g e


C# Programming C-Net, Koel Nagar

Console.Write("Input Month No : ");


monno = Convert.ToInt32(Console.ReadLine());

switch(monno)
{
case 1:
Console.Write("January\n");
break;
case 2:
Console.Write("February\n");
break;
case 3:
Console.Write("March\n");
break;
case 4:
Console.Write("April\n");
break;
case 5:
Console.Write("May\n");
break;
case 6:
Console.Write("June\n");
break;
case 7:
Console.Write("July\n");
break;
case 8:
Console.Write("August\n");
break;
case 9:
Console.Write("September\n");
break;
case 10:
Console.Write("October\n");
break;
case 11:
Console.Write("November\n");
break;
case 12:
Console.Write("December\n");
break;
default:
Console.Write("invalid Month number. \nPlease try again ....\n");
break;
}
}
}
Subrat Kumar Dash (7978718584) 19 | P a g e
C# Programming C-Net, Koel Nagar

24. Write a program in C# Sharp to read any Month Number in integer and
display the number of days for this month.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class exercise23


{
static void Main(string[] args)
{
int monno;

Console.Write("\n\n");
Console.Write("Read month number and display number of days for that
month:\n");
Console.Write("--------------------------------------------------------------");
Console.Write("\n\n");

Console.Write("Input Month No : ");


monno = Convert.ToInt32(Console.ReadLine());
switch(monno)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
Console.Write("Month have 31 days. \n");
break;
case 2:
Console.Write("The 2nd month is a February and have 28 days. \n");
Console.Write("in leap year The February month Have 29 days.\n");
break;
case 4:
case 6:
case 9:
case 11:
Console.Write("Month have 30 days. \n");
break;
default:
Console.Write("invalid Month number.\nPlease try again ....\n");
Subrat Kumar Dash (7978718584) 20 | P a g e
C# Programming C-Net, Koel Nagar
break;
}
}
}

25. Write a program in C# Sharp which is a Menu-Driven Program to


compute the area of the various geometrical shape.

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 :");
Subrat Kumar Dash (7978718584) 21 | P a g e
C# Programming C-Net, Koel Nagar
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);
}
}

26. Write a program in C# Sharp which is a Menu-Driven Program to


perform a simple calculation.

using System;
public class Exercise25
{
public static void Main()
{
int num1,num2,opt;

Console.Write("\n\n");
Console.Write("A menu driven program for a simple calculator:\n");
Console.Write("------------------------------------------------");
Console.Write("\n\n");

Console.Write("Enter the first Integer :");


num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the second Integer :");
num2 = Convert.ToInt32(Console.ReadLine());

Console.Write("\nHere are the options :\n");


Console.Write("1-Addition.\n2-Substraction.\n3-Multiplication.\n4-Division.\n5-
Exit.\n");
Console.Write("\nInput your choice :");
opt = Convert.ToInt32(Console.ReadLine());

switch(opt) {
case 1:
Console.Write("The Addition of {0} and {1} is: {2}\n",num1,num2,num1+num2);
break;

case 2:
Console.Write("The Substraction of {0} and {1} is: {2}\n",num1,num2,num1-num2);
Subrat Kumar Dash (7978718584) 22 | P a g e
C# Programming C-Net, Koel Nagar
break;

case 3:
Console.Write("The Multiplication of {0} and {1} is: {2}\n",num1,num2,num1*num2);
break;

case 4:
if(num2==0) {
Console.Write("The second integer is zero. Devide by zero.\n");
} else {
Console.Write("The Division of {0} and {1} is : {2}\n",num1,num2,num1/num2);
}
break;

case 5:
break;

default:
Console.Write("Input correct option\n");
break;
}
}
}

Subrat Kumar Dash (7978718584) 23 | P a g e

You might also like