About Lesson
If Else Programs
C
/*
Decision Control Instructions -
As we know a program is a solution of problem and a problem can not solve
without decision so to take decision in c programs there are following
decision control instructions
1. if-else
syntax-
if(condition)
{
..... if condition goes to true then this block executes
}
else
{
.... if condition goes to false then this else block executes
}
condition if block else block
true work not work
false not work work
Note - else block always optional
*/
/*
enter mrp of book give 10% discount if mrp>=500 otherwise give 5% discount
after discount print net price
*/
#include<stdio.h>
#include<conio.h>
void main()
{
float mrp,dis=0,net;
clrscr();
printf("\nenter mrp of book");
scanf("%f",&mrp); //600, 200
if(mrp>=500) // 600>=500=>true, 200>=500=>false
{
dis=mrp*10/100; // dis=600*10/100 =>60
}
else
{
dis=mrp*5/100; // dis=200*5/100=>10
}
net=mrp-dis; // 600-60=>540, 200-10=>190
printf("\nMRP=%.2f\nDis=%.2f\nNet=%.2f",mrp,dis,net);
getch();
}
/*
1. enter 2 nos and print which one is biggest
2. enter sale price and cost price of book print profit or loss
*/
C
/*
Nested if-else: when we use an if else statement into another if-else
syntax:
if(condition)
{
if(condition)
{
....
}
else
{
...
}
}
else
{
.....
if(condition)
{
...
}
else
{
...
}
}
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("\nenter an age of a person");
scanf("%d",&age); //10 ,25, 80
// lic eligibility criteria age must be between 18-45
if(age>=18)
{
if(age<=45)
{
printf("\nEligibile for LIC");
}
else
{
printf("\nNot Eligible for LIC");
}
}
else
{
printf("\n Not Eligible for LIC");
}
getch();
}
/*
Biggest between 3 Nos
a b c - ram , sham, mohan input age
input
*/
C
/*
if- else if - else
if(condition)
{
....
}
else if(condition)
{
.....
}
else if(condition)
{
....
}
.
.
.
else
{
.....
}
enter a day of the week and print name of the day
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int day;
clrscr();
printf("\nenter day of the week");
scanf("%d",&day);
if(day==1)
{
printf("\nToday is Monday");
}
else if(day==2)
{
printf("\nToday is Tuesday");
}
else if(day==3)
{
printf("\nToday is Wednesday");
}
else if(day==4)
{
printf("\nToday is Thursday");
}
else if(day==5)
{
printf("\nToday is Friday");
}
else if(day==6)
{
printf("\nToday is Saturday");
}
else if(day==7)
{
printf("\nToday is Sunday");
}
else
{
printf("\nenter a valid day of week it should be between 1 - 7");
}
getch();
}
/*
enter a digit and print it in word
0 Zero
1 One
2 Two
3 Three
....
enter a month of a year print name of the month
1 Jan
2 Feb
...
*/
C
/*
5+2
Logical Operator
combine 2 or more than 2 relations give result either true or false
5>6 && 7<9
0 && 1
0
&& Logical And gives true when all conditions goes to true
R1 R2 Result
1 1 1
1 0 0
0 1 0
0 0 0
---------------------------
5>6 || 7<9
0 || 1
1
|| Logical Or it gives true when anyone condition goes to true
R1 R2 Result
1 1 1
1 0 1
0 1 1
0 0 0
! Logical Not
Condition Result
1 0
0 1
!(5<3)=>1
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("\nenter age of a person");
scanf("%d",&age);
if(age>=18 && age<=45)
{
printf("\n Eligible for LIC");
}
else
{
printf("\nNot Eligible for LIC");
}
getch();
}
/*
Biggest between 3 Nos using logical operator and if - else if - else
h,e,m,sci,sst,total, per
divsion
merit
1st
2nd
3rd
fail
*/
C
/*
Ternery Operator - Operates on 3 Operand
? then
: otherwise
syntax T F
condition ? expr1-work : expr2-work;
Nested Ternery
condition ? (condition?expr1:expr2) : (condition?expr1:expr2);
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\nenter 2 nos");
scanf("%d%d",&a,&b);
a>b ? printf("\nA is Biggest") : printf("\nB is biggest");
c = a>b ? a : b;
printf("\nBiggest Number is %d",c);
getch();
}
//biggest between 3 nos using ternery operator
/*
enter working hour of an employee and calculate daily wage
wh wage
8 350
over time
wh wage per hour
>8 upto 10 50
>10 upto 12 75
>12 upto 14 100
other wise ivalid input
11
8 350
9 50
10 50
11 75
----------
525
*/
C
/*
Income Tax Slab
Min Max Tax%
0 3Lac 0
3Lac 7Lac 5
7Lac 10Lac 10
10Lac 12Lac 15
12Lac 20Lac 20
20Lac ~ 30
Question input income and calculate tax.
*/
#include<stdio.h>
int main()
{
double income,tax, taxableamnt;
printf("\nenter your income");
scanf("%lf",&income);
if(income<=300000)
{
tax=0;
}
else if(income>300000 && income<=700000)
{
taxableamnt=income-300000;
tax=taxableamnt*0.05;
}
else if(income>700000 && income<=1000000)
{
taxableamnt=income-700000;
tax=20000+taxableamnt*0.10;
}
else if(income>1000000 && income<=1200000)
{
taxableamnt=income-1000000;
tax=50000+taxableamnt*0.15;
}
//put rest of logic here
printf("Tax is %lf",tax);
}