C# Conditional Statements Examples
C# Conditional Statements Examples
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);
}
}
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);
}
}
}
}
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");
}
}
x1=(-b+Math.Sqrt(d))/(2*a);
x2=(-b-Math.Sqrt(d))/(2*a);
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("\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
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;
Console.Write("\n\n");
Console.Write("Calculate Electricity Bill:\n");
Console.Write("----------------------------");
Console.Write("\n\n");
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");
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");
switch(cdigit)
{
case 0:
Console.Write("Zero\n");
break;
case 1:
Console.Write("one\n");
break;
case 2:
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");
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;
Console.Write("\n\n");
Console.Write("Read month number and display number of days for that
month:\n");
Console.Write("--------------------------------------------------------------");
Console.Write("\n\n");
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");
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);
}
}
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");
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;
}
}
}