Examples for practice:
Specification1:
Accept the price, quantity of a product from the keyboard
and print the bill, discount which is 5% of bill and the net bill.
Program:
#include<stdio.h>
void main()
{
int qty;
float price,dis,bill,net_bill;
clrscr();
printf(“Give the price:”);
scanf(“%f”,&price);
printf(“Give the quantity:”);
scanf(“%d”,&qty);
bill=price*qty;
dis=bill*5/100;
net_bill=bill-dis;
printf(“Total bill %f”,bill);
printf(“\nDiscount %f”,dis);
printf(“\nNet bill %f”,net_bill);
getch();
}
Execution:
Give the price: 25.25
Give the quantity: 3
Total bill 75.750000
Discount 3.787500
specification2:
Accept the present month meter reading of energy meter,
previous month meter reading and print the number of units consumed and the
total bill, if the unit charge is 3.75 per unit.
Program:
#include<stdio.h>
void main()
{
int cmr,pmr,nu;
float bill;
clrscr();
printf(“Enter the present month meter reading:”);
scanf(“%d”,&cmr);
scanf(“Enter the previous month meter reading:”);
scanf(“%d”,&pmr);
nu=cmr-pmr;
bill=nu*3.75;
printf(“Number of units consumed %d”,nu);
printf(“\nTotal bill %f”,bill);
getch();
}
Execution:
Enter the present month meter reading: 450
Enter the previous month reading: 300
Number of units consumed 150
Total bill 562.500000
specification3:
Accept the marks of a student 3 subjects and print the total
and average.
Program:
#include<stdio.h>
void main()
{
int mat,phy,che,tot;
float avg;
clrscr();
printf(“Enter the marks\nMathematics:”);
scanf(“%d”,&mat);
printf(“Physics:”);
scanf(“%d”,&phy);
printf(“Chemistry:”);
scanf(“%d”,&che);
tot=mat+phy+che;
avg=tot/3;
printf(“Total %d”,tot);
printf(“\nAverage %f”,avg);
getch();
}
Execution1:
Enter the marks
Mathematics: 62
Physics: 86
Chemistry: 64
Total 212
Average 70.000000
Rectification and need of type casting:
In the above example actual average must be 70.666667, but the above program printed only 70.000000.
There is a problem in typecasting tot/3 (integer expression) into avg (float type).
We need to inform the compiler to cast from integer type to float type using typecast operator (float).
It can be rectified by changing the statement as avg=(float)tot/3;
Examples:
Expression
Value of “avg”
Reason
avg=tot/3;
70.000000
The result of int expression is int
avg=tot/3.0;
70.666667
The result of float expression is float
avg=(float)tot/3;
70.666667
tot of int type is changed into float type.
Note: Auto casting and Typecasting will be discussed later.
specification4:
Accept the salary of an employee and print the HRA (House Rent allowance) which is 12% of salary, DA (Dearness allowance) which is 15% salary and print the gross.
Program:
#include<stdio.h>
void main()
{
int sal;
float hra,da,gross;
clrscr();
printf(“Enter the salary:”);
scanf(“%d”,&sal);
hra=(float)sal*12/100;
da=(float)sal*15/100;
gross=sal+hra+da;
printf(“Salary details of employee\n”);
printf(“Salary %d”,sal);
printf(“\nHouse Rent Allowances %f”,hra);
printf(“\n Dearness Allowances %f”,da);
printf(“\n Gross Salay %f”,gross);
getch();
}
Execution:
Enter the salary: 3500
Salary details of employee
Salary 3500
House Rent Allowance 420.000000
Dearness Allowances 525.000000
Gross Salary 4445.000000
Specification5:
Accept the salary, sales of a sales person and print the
commission which is 3% of sales and gross salary.
Program:
#include<stdio.h>
void main()
{
long int sal,sales;
float comm,gross;
clrscr();
printf(“Enter the salary:”);
scanf(“%ld”,&sal);
printf(“Enter the sales:”);
scanf(“%ld”,&sales);
comm.=(float)sales*3/100;
gross=sal+comm.;
printf(“Payment details\n”);
printf(“\nSalary %ld”,sal);
printf(“\nCommission %f”,comm.);
printf(“\nGross salary %f”,gross);
getch();
}
Execution:
Enter the salary: 12500
Enter the sales: 125750
Commission: 3772.5
Gross salary: 16272.5
Note:
The range of integer is between 32768 to 32767, but the salary,
sales may exceed to this range. long int is another type takes more space and
stores integers of higher ranges. %ld is the representation character for long
int.
specification6:
Accept the temperature in degrees Celsius and print the
temperature in foreign heat.
Formula: F= (9/5)*c+32
Program:
#include<stdio.h>
void main()
{
int c;
float f;
clrscr();
printf(“Enter the temperature in degrees Celsius:”);
scanf(“%d”,&c);
f=(float)9/5*c+32;
printf(“Temparature in foreign heat %f”,f);
getch();
}
Execution:
Enter the temperature in degrees Celsius: 45
Temparature in foreign heat: 113.000000
specification7:
Accept the length, breadth of a rectangle and radius of circle;
print the area, perimeter of rectangle and area, circumference of circle.
Program:
#include<stdio.h>
void main()
{
int len,breadth,rad;
int rect_area,rect_peri;
float c_area,c_cir;
clrscr();
printf(“Enter the length of rectangle:”);
scanf(“%d”,&len);
printf(“Enter the breadth of rectangle:”);
scanf(“%d”,&breadth);
printf(“Enter the radios of a circle:”);
scanf(“%d”,&rad);
rect_area=len*breadth;
rect_peri=2*(len+breadth);
c_area=3.14*rad*rad;
c_cir=2*3.14*rad;
printf(“Rectanle:\n”);
printf(“Area %d”,rect_area);
printf(“\nPerimeter %d”,rect_peri);
printf(“\nCircle:\n”);
printf(“Area %f”,c_area);
printf(“\n Circumference %f”,c_cir);
getch();
}
Execution:
Enter the length of rectangle: 12
Enter the breadth of rectangle: 17
Enter the radios of circle: 15
Rectangle
Area 204
Perimeter 58
Circle
Area 706.500000
Circumference 94.200000
specification8:
Accept the distance between two cities in Kilo meters and
print in meters, centimeters, miles, feet and inches.
Conversion table:
100 cms |
1 Meter |
1000 Meters |
1 Kilo meter |
1 inch |
2.54 cms |
1 cms |
0.36 inches |
1 feet |
12 inches |
1 Mile |
1.6 Kilometers |
Program:
#include<stdio.h>
void main()
{
float kms,mts,cms,inch,fts,mile;
clrscr();
printf("Enter the distance in kilometers:");
scanf("%f",&kms);
mts=kms*1000; /* 1 Kilometer=1000 Meters */
cms=mts*100; /* 1 Meter=100 Cms */
inch=cms*0.39; /* 1 cms=0.39 inch */
fts=inch/12; /* 1 feet=12 incehs */
mile=kms*0.6; /* 1 Mile=1.6 Kilometers*/
printf("Kilometers %f",kms);
printf("\nMiles %f",mile);
printf("\nMeters %f",mts);
printf ("\nCentimeters %f",cms);
printf("\nFeets %f",fts);
printf("\nInches %f",inch);
getch();
}
Execution:
Enter the distance in Kilometers: 1
Kilometers 1.000000
Miles 0.600000
Meters 100.000000
Centimeters 10000.000000
Feets 325.000000
Inches 3900.000000
0 comments:
Post a Comment