Conditional Statement in C-Sharp:-
It is used to solve a condition-based program, It provides a separate section for true conditions and false conditions.
Ternary operator can contain only a single statement on the true section and false section. that's why we mostly prefer if...else statements to solve condition based program.
Condition Statements have two different statements first is IF and another one is Else
Syntax of IF Statements:-
if(condition)
{
Statements;
}
It will execute when the condition will be true.
Syntax of ELSE Statements:-
if(condition)
{
Statements;
}
It will execute when the condition will be true.
Syntax of ELSE Statements:-
else
{
}
It will not execute separately, It is a dependent statement under if, when IF condition will be false then Else statement will be executed.
Combination of else and IF:-
else if(condition)
{
}
It is a combined statement of Else and IF that will work similarly to else but we can provide the condition, it is also a dependent statement under the IF statement.
Type of Conditional Statement:-
1) Simple If:-
It will execute when the condition will be true.
if(condition)
{
}
Program:- WAP to increase the load amount on electricity bill when consumption will be above 2500.load amount will be 5% of total bill?
Solution:-
namespace Csharpfundamental
{
internal class Program
{
static void Main()
{
float bill = 3500;
if (bill>2500)
{
bill = bill + bill * .05F;
}
Console.WriteLine(bill);
}
}
}
WAP to apply grace mark if entered mark is <33 and > 28.
otherwise print normal mark. and print also total applied grace mark in case of grace mark
Solution of this program:-
namespace Csharpfundamental
{
internal class Program
{
static void Main()
{
int mark = 40;
if (mark >=28 && mark<33)
{
Console.WriteLine("Apply grace is " + (33 - mark));
mark = mark + (33 - mark);
}
Console.WriteLine(mark);
}
}
}
It will execute when the condition will be true or false.
if(condition)
{
}
else
{
}
if(condition)
{
}
else
{
}
Program:-
WAP to increase the load amount on electricity bill when consumption will be above 2500.load amount will be 5% of total bill otherwise provide 50% discount on electricity bill.
namespace Csharpfundamental
{
internal class Program
{
static void Main()
{
float bill = 2501;
if (bill>2500)
{
bill = bill + bill * .05F;
}
else
{
bill = bill / 2;
}
Console.WriteLine(bill);
}
}
}
It will execute with the combination of IF statement, Else IF statement,... and Else statement. Ladder means Step by Step when the first condition will be false then the second will be cheeked.
It is also called Ladder if-else because it will work step by step.
Syntax of Ladder if else:-
if(condition)
{
Statements;
}
else if(condition)
{
Statements;
}
else if(condition)
{
Statements;
}
else
{
Statement;
}
if(condition)
{
Statements;
}
else if(condition)
{
Statements;
}
else if(condition)
{
Statements;
}
else
{
Statement;
}
Example of Ladder if-else to calculate program of the greatest number?
class Ladder
{
static void Main()
{
int a = 50, b = 200, c = 300;
if (a > b && a > c)
{
Console.WriteLine("a is greatest");
}
else if (b > c)
{
Console.WriteLine("b is greatest");
}
else
{
Console.WriteLine("c is greatest");
}
Console.ReadKey();
}
Another Example of Nested If-else
class SimpleIf
{
static void Main()
{
int num = -5;
if (num > 0 && num < 10)
{
Console.WriteLine("One Digit Positive Number" + num);
}
else if(num>= 10)
{
Console.WriteLine("More Then One Digit Positive Number" + num);
}
else if (num > -9 && num < -1)
{
Console.WriteLine("One Digit Negative Number" + num);
}
else
{
Console.WriteLine("More than One Digit Negative Number" + num);
}
Console.ReadKey();
}
}
4) Nested If--Else:- If we write more than one if-else statement using nested sequence then it is called nested if-else.
Syntax of Nested If Else:-
if(condition)
{
if(condition)
{
}
else
{
}
}
else
{
if(condition)
{
}
else
{
}
}
Example of Nested If--else to calculate the greatest number?
class NestedExample
{
static void Main()
{
int a = 50, b = 200, c = 300;
if (a > b)
{
if(a>c)
Console.WriteLine("a is greatest");
else
Console.WriteLine("c is greatest");
}
else
{
if (b > c)
Console.WriteLine("b is greatest");
else
Console.WriteLine("c is greatest");
}
Console.ReadKey();
}
}
Another Example of Nested If-else
#Program to check number is one digit positive, negative or above this.
class SimpleIf
{
static void Main()
{
int num = 5;
if (num > 0)
{
if (num < 10)
{
Console.WriteLine("One Digit Positive Number" + num);
}
else
{
Console.WriteLine("More than One Digit Positive Number" + num);
}
}
else
{
if (num > -9 && num < -1)
{
Console.WriteLine("One Digit Negative Number" + num);
}
else
{
Console.WriteLine("More than One Digit Negative Number" + num);
}
}
Console.ReadKey();
}
}
Create Nested If--Else:-
namespace Csharpfundamental
{
internal class Program
{
static void Main()
{
int a = 4500, b = 670, c = 89,d=889;
if(a>b )
{
if (a > c)
{
if(a>d)
{
Console.WriteLine("a is greatest");
}
else
{
Console.WriteLine("d is greatest");
}
}
else
{
if(c>d)
{
Console.WriteLine("c is greatest");
}
else
{
Console.WriteLine("d is greatest");
}
}
}
else
{
if (b > c)
{
if (b > d)
{
Console.WriteLine("b is greatest");
}
else
{
Console.WriteLine("d is greatest");
}
}
else
{
if (c > d)
{
Console.WriteLine("c is greatest");
}
else
{
Console.WriteLine("d is greatest");
}
}
}
}
}
}
Syntax of Multiple IF:-
if(condition)
{
}
if(condition)
{
}
Example of Multiple if to check divisibility from 3, 5, and 9 in combinations and individually.
class MultipleIf
{
static void Main()
{
int num;
Console.WriteLine("Enter Number");
num = int.Parse(Console.ReadLine());
if (num % 3 != 0 && num % 5 != 0 && num % 9 != 0)
{
Console.WriteLine("Not divisible by 3, 5 and 9");
}
if (num % 3 == 0)
{
Console.WriteLine("Number is divisible by 3");
}
if (num % 5 == 0)
{
Console.WriteLine("Number is divisible by 5");
}
if (num % 9 == 0)
{
Console.WriteLine("Number is divisible by 9");
}
Console.ReadKey();
}
ASSIGNMENT OF IF-ELSE:-
Q Check Userid and Password with all combinations?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
String uid, pass;
Console.WriteLine("Enter userid");
uid = Console.ReadLine();
Console.WriteLine("Enter password");
pass = Console.ReadLine();
if (uid == "abc" && pass == "1234")
{
Console.WriteLine("Valid Userid and Password");
}
else if (uid != "abc" && pass != "1234")
{
Console.WriteLine("Invalid Userid and password");
}
else if (pass != "1234")
{
Console.WriteLine("Invalid Password");
}
else
{
Console.WriteLine("Invalid Userid ");
}
Console.ReadKey();
}
}
}
Q WAP to provide grace marks to the student if the mark will be less then <33 otherwise the same marks will be displayed, grace marks will be up-to five. if pass with grace then displays grace + grace mark otherwise mark.
Q WAP to increase the salary of an employee from 500, if entered salary will be less then 20000,1000 if entered salary will be > 20000 but <30000 if the salary will be above 30000 then increase 2000.
Q WAP to declare income tax slab according to entered income?
Q WAP to check that temperature is normal, cold, or heat according to entered temperature, the temperature should be in Celsius but the comparison will be in Fahrenheit?
The solution of this program:-
class TempratureConvertor
{
static void Main()
{
float f, c;
Console.WriteLine("Enter temprature in celsius");
c = float.Parse(Console.ReadLine());
f = (9 * c + 160) / 5;
if (f >= 0 && f <= 62.6)
{
Console.WriteLine("COLD and Tempratute is "+c+ "c");
}
else if (f <= 77)
{
Console.WriteLine("Normal");
}
else
{
Console.WriteLine("Heat");
}
Console.ReadKey();
}
}
Q WAP to check divisibility of number that number is divisible by 3,5 and 9 with all combinations?
Q WAP to create a mark sheet of Students using five different subjects with the following condition?
1) All Subject Marks Should be 0 to 100.
2) If only Subject Mark is <33 Then Student will Suppl
3) If Minimum Two Subject Marks is <33 Then Student Will Fail
4) IF all Subject Marks is > 33 then percentage and division should be calculated.
5) IF only subject Mark is >28 and <33 then 5 grace marks will be applied and the student will pass by grace.
6) Display Grace Subject Name, Distinction Subject name, Supp Subject name, and Failed Subject name.
The solution of This Program:-
using System;
namespace ConsoleApplication2
{
class MarksheetProgram
{
static void Main()
{
int m1, m2, m3, m4, m5,grace=0;
String sub1, sub2, sub3, sub4, sub5,sub="",dist="";
int count = 0;
Console.WriteLine("Enter First Subject Name and Mark");
sub1 = Console.ReadLine();
m1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Second Subject Name and Mark");
sub2 = Console.ReadLine();
m2 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Third Subject Name and Mark");
sub3 = Console.ReadLine();
m3 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Fourth Subject Name and Mark");
sub4 = Console.ReadLine();
m4 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Fifth Subject Name and Mark");
sub5 = Console.ReadLine();
m5 = int.Parse(Console.ReadLine());
if ((m1 >= 0 && m1 <= 100) && (m2 >= 0 && m2 <= 100) && (m3 >= 0 && m3 <= 100) && (m4 >= 0 && m4 <= 100) && (m5 >= 0 && m5 <= 100))
{
if (m1 >=75)
{
dist+= sub1 + " ";
}
if (m2 >= 75)
{
dist += sub2 + " ";
}
if (m4 >= 75)
{
dist += sub4 + " ";
}
if (m5 >= 75)
{
dist += sub5 + " ";
}
if (m3 >= 75)
{
dist += sub3 + " ";
}
if (m1 < 33)
{
grace = m1;
sub += sub1 + " ";
count++;
}
if (m2 < 33)
{
grace = m2;
sub += sub2 + " ";
count++;
}
if (m3 < 33)
{
grace = m3;
sub += sub3 + " ";
count++;
}
if (m4 < 33)
{
grace = m4;
count++;
sub += sub4 + " ";
}
if (m5 < 33)
{
grace = m5;
count++;
sub += sub5 + " ";
}
if (count == 0 || (count==1 && grace>=28))
{
float per = (m1 + m2 + m3 + m4 + m5) / 5;
if(per>=33 && per<45)
Console.WriteLine("Congrats you are pass with third division with "+per +"%");
else if(per<60)
Console.WriteLine("Congrats you are pass with second division with " + per + "%");
else
Console.WriteLine("Congrats you are pass with first division with " + per + "%");
if (grace >= 28)
Console.WriteLine("You are pass by grace and grace marks is " + (33 - grace));
if (dist != "")
Console.WriteLine("Distinction Subject name is " + dist);
}
else if (count == 1)
{
Console.WriteLine("Try again you are suppl in "+sub);
}
else
{
Console.WriteLine("You are fail in "+sub);
}
}
else
{
Console.WriteLine("All Subject Marks Should be 0 to 100");
}
Console.ReadKey();
}
}
}
Q WAP to increase the salary of an employee from 500, if entered salary will be less then 20000,1000 if entered salary will be > 20000 but <30000 if the salary will be above 30000 then increase 2000.
not use any logical operator
Q WAP to find the middle in three different where the number should not be equal?
a=1
b=7
c=2
Ans1:- Passed by Grace+
ReplyDeleteusing System;
public class Program
{
public static void Main()
{
int a ;
Console.WriteLine("Enter Marks: ");
a= int.Parse(Console.ReadLine());
if (a >0 && a<=100)
{
if (a==28)
{
Console.WriteLine("Grace+ = {0}",(a + 5));
}
else if (a==29)
{
Console.WriteLine("Grace+ = {0}",(a + 4));
}
else if (a==30)
{
Console.WriteLine("Grace+ = {0}",(a + 3));
}
else if (a==31)
{
Console.WriteLine("Grace+ = {0}",(a + 2));
}
else if (a==32)
{
Console.WriteLine("Grace+ = {0}",(a + 1));
}
else
{
Console.WriteLine("Mark is {0}",a);
}
}
}
}
Ans2 :- Salary Increment(500,1000,2000) Program.
ReplyDeletepublic static void Main()
{
int a;
Console.WriteLine("Enter Salary : ");
a = int.Parse(Console.ReadLine());
if(a>=0 && a<=19999)
{
Console.WriteLine("Salary is :{0} \nSalary with Increment :{1} ",a,a+500);
}
else if(a>=20000 && a<=29999)
{
Console.WriteLine("Salary is :{0} \nSalary with Increment :{1} ",a,a+1000);
}
else
{
Console.WriteLine("Your Salary is :{0} \nSalary With Increment :{1}",a,a+2000);
}
}
using System;
ReplyDeletepublic class Program
{
public static void Main()
{
float a,b,c,d,e,f,g,h;
Console.Write("Enter Annual Salary Lakh: ");
a= float.Parse(Console.ReadLine());
b=((a*5)/100);
c=((a*10)/100);
d=((a*15)/100);
e=((a*20)/100);
f=((a*25)/100);
g=((a*30)/100);
if (a>=0 && a<=250000)
{
Console.WriteLine("Your are not under Income-Tax");
}
else if(a>=250001.00 && a<=500000.00)
Console.WriteLine("You will Pay 5% Income-Tax : {0}",b+" " +"Thousand");
else if(a>=500001.00 && a<=750000.00)
Console.WriteLine("You will Pay 10% Income-Tax : {0}",c+" " +"Thousand");
else if (a>=750001.00 && a<=1000000.00)
Console.WriteLine("You will Pay 15% Income-Tax : {0}",d+" " +"Lakh");
else if(a>=1000001 && a<=1250000)
Console.WriteLine("You will Pay 20% Income-Tax : {0}",e+" " +"Lakh");
else if(a>=1250001 && a<=1500000)
Console.WriteLine("You will Pay 25% Income-Tax : {0}",f+" " +"Lakh");
else
Console.WriteLine("You will Pay 30% Income-Tax : {0}",g+" " +"Lakh");
}
}
int a;
ReplyDeleteConsole.WriteLine("Enter Number : ");
a =int.Parse(Console.ReadLine());
if (a%3==0 && a%5==0 && a%9==0)
{
Console.WriteLine("Divisible Number :{0}",a);
}else
Console.WriteLine("Not Divisible Number :{0}",a);
using System;
ReplyDeleteusing System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace graceMarks
{
internal class Program
{
static void Main(string[] args)
{
int marks, grace,graceMarks;
Console.WriteLine("Enter the marks");
marks= Convert.ToInt32(Console.ReadLine());
grace = (33 - marks);
if (marks<33 && grace<=5)
{
graceMarks = marks + grace;
Console.WriteLine("Grace {0} and Grace Marks {1}",grace,graceMarks);
}
else
{
Console.WriteLine("The total marks obtained are {0}",marks);
}
Console.ReadKey();
}
}
}
using System;
ReplyDeleteusing System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace salaryCondition
{
internal class Program
{
static void Main(string[] args)
{
int s;
Console.WriteLine("Enter the salary");
s= Convert.ToInt32(Console.ReadLine());
if(s<=20000)
{
s = s + 500;
Console.WriteLine(s);
}
if(s>20000 && s<=30000)
{
s = s + 1000;
Console.WriteLine(s);
}
if(s>30000)
{
s = s + 2000;
Console.WriteLine(s);
}
Console.ReadKey();
}
}
}
class incomeTaxSlab
ReplyDelete{
public static void Main()
{
int salary;
double tax;
Console.WriteLine("Enter your Salary");
salary = int.Parse(Console.ReadLine());
if (salary < 250000)
{
Console.WriteLine("No tax Payable");
}
else if (salary > 250000 && salary < 500000)
{
Console.WriteLine("You have to Pay 5% Tax");
}
else if (salary > 500000 && salary < 750000)
{
Console.WriteLine("You have to Pay 10% Tax");
}
else if (salary > 750000 && salary < 1000000)
{
Console.WriteLine("You have to Pay 15% Tax");
}
else if (salary > 1000000 && salary < 1250000)
{
Console.WriteLine("You have to Pay 20% Tax");
}
else if (salary > 1250000 && salary < 1500000)
{
Console.WriteLine("You have to Pay 25% Tax");
}
else if (salary > 1500000)
{
Console.WriteLine("You have to Pay 30% Tax");
}
Console.ReadLine();
}
}
greatest no. in among 4
ReplyDeletenamespace First_project
{
internal class FourDigGreatest
{
public void fourdiggreatest()
{
int a = 1105, b = 110, c = 920, d = 984;
if (a > b && a > c && a > d)
{
Console.WriteLine("a is greatest");
}
else if (b > c && b > d)
{
Console.WriteLine("b is greatest");
}
else if (c > d)
{
Console.WriteLine("c is greatest");
}
else Console.WriteLine("d is greatest");
}
}
}
Grace Marks
ReplyDeletenamespace First_project
{
internal class GraceMark
{
public void gracemark()
{
float graceMarks, marks;
Console.WriteLine("please enter marks");
marks=float.Parse(Console.ReadLine());
if (marks < 33 && marks>22)
{
graceMarks = 33 - marks;
}
else
{
graceMarks = 0;
}
Console.WriteLine("Grace Marks is "+graceMarks);
Console.WriteLine("Total Marks are " + (marks + graceMarks));
}
}
}