Example 1: While purchasing certain items, a discount of 10% is offered if the quantity
purchased is more than 1000. If quantity and price per item are input through the
keyboard, write a program to calculate the total expenses.
Code:
/* Calculation of total expenses */
main( )
{
int qty, dis = 0 ;
float rate, tot ;
printf ( "Enter quantity and rate " ) ;
scanf ( "%d %f", &qty, &rate) ;
if ( qty > 1000 )
dis = 10 ;
tot = ( qty * rate ) - ( qty * rate * dis / 100 ) ;
printf ( "Total expenses = Rs. %f", tot ) ;
}
Example 2: The current year and the year in which the employee joined the
organization are entered through the keyboard. If the number of years for which the
employee has served the organization is greater than 3 then a bonus of Rs. 2500/- is
given to the employee. If the years of service are not greater than 3, then the program
should do nothing.
Code: /* Calculation of bonus */
main( )
{
int bonus, cy, yoj, yr_of_ser ;
printf ( "Enter current year and year of joining " ) ;
scanf ( "%d %d", &cy, &yoj ) ;
yr_of_ser = cy - yoj ;
if ( yr_of_ser > 3 )
{
bonus = 2500 ;
printf ( "Bonus = Rs. %d", bonus ) ;
}
}
Example 3: In a company an employee is paid as under:
If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and DA =
90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs.
500 and DA = 98% of basic salary. If the employee's salary is input through the
keyboard write a program to find his gross salary.
Code:
/* Calculation of gross salary */
main( )
{
float bs, gs, da, hra ;
printf ( "Enter basic salary " ) ;
scanf ( "%f", &bs ) ;
if ( bs < 1500 )
{
hra = bs * 10 / 100 ;
da = bs * 90 / 100 ;
}
else
{
hra = 500 ;
da = bs * 98 / 100 ;
}
gs = bs + hra + da ;
printf ( "gross salary = Rs. %f", gs ) ;
}
Example 4: The marks obtained by a student in 5 different subjects are input through
the keyboard. The student gets a division as per the following rules:
Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 - Fail
Write a program to calculate the division obtained by the student by using logical
operator
Code:
main( )
{
int m1, m2, m3, m4, m5, per ;
printf ( "Enter marks in five subjects " ) ;
scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ;
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
if ( per >= 60 )
printf ( "First division" ) ;
if ( ( per >= 50 ) && ( per < 60 ) )
printf ( "Second division" ) ;
if ( ( per >= 40 ) && ( per < 50 ) )
printf ( "Third division" ) ;
if ( per < 40 )
printf ( "Fail" ) ;
}
A company insures its drivers in the following cases:
− If the driver is married.
− If the driver is unmarried, male & above 30 years of age.
− If the driver is unmarried, female & above 25 years of age.
In all other cases the driver is not insured. If the marital status, gender and age of the
driver are the inputs, write a program to determine whether the driver is to be insured
or not.
Code:
main( )
{
char gender, ms ;
int age ;
printf ( "Enter age, gender, marital status " ) ;
scanf ( "%d %c %c", &age, &gender, &ms ) ;
if ( ms == 'M' )
printf ( "Driver is insured" ) ;
else
{
if ( gender == 'M' )
{
if ( age > 30 )
printf ( "Driver is insured" ) ;
else
printf ( "Driver is not insured" ) ;
}
else
{
if ( age > 25 )
printf ( "Driver is insured" ) ;
else
printf ( "Driver is not insured" ) ;
}
}
}
If we list down all those cases in which the driver is insured, then they would be:
Driver is married.
Driver is an unmarried male above 30 years of age.
Driver is an unmarried female above 25 years of age.
Since all these cases lead to the driver being insured, they can be combined together
using && and || as shown in the program below:
/* Insurance of driver - using logical operators */
main( )
{
char sex, ms;
int age;
printf ( "Enter age, sex, marital status " ) ;
scanf ( "%d %c %c" &age, &sex, &ms ) ;
if ( ( ms == 'M') || ( ms == 'U' && sex == 'M' && age > 30 ) || ( ms == 'U' && sex == 'F'
&& age > 25 ) )
printf ( "Driver is insured" ) ;
else
printf(“Driver is not insured”)’
}
Write a program to calculate the salary as per the following table: