2.3 – C procedural programs to practice Part-1

by subbu on August 13, 2013

C procedural programs to practice part-1:

Specification: Accept the marks of a student in 3 subjects and print the total and average.

Program:

#include<stdio.h>
int main()
{
int mat,phy,che,tot;
float avg;
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);
return 0;
}

Execution:
Enter the marks:
Mathematics: 62
Physics: 86
Chemistry: 64
Total 212
Average 70.000000

Rectification and need of type casting:
In the above execution actual average must be 70.666667, but has printed 70.000000. Because tot/3 is an integer expression yields an integer value
We need to inform the compiler to change integer type to float type using typecast operator (float).
It can be rectified by changing the statement as avg=tot/3.0; or avg=(float)tot/3;

Rectified program:

#include<stdio.h>
int main()
{
int mat,phy,che,tot;
float avg;
printf("Enter the marks:\nMathematics:");
scanf("%d",&mat);
printf("Physics:");
scanf("%d",&phy);
printf("Chemistry:");
scanf("%d",&che);
tot=mat+phy+che;
avg=(float)tot/3;
printf("Total %d",tot);
printf("\nAverage %f",avg);
return 0;
}

Execution:
Enter the marks:
Mathematics: 62
Physics: 86
Chemistry: 64
Total 212
Average 70.666667

specification: Accept the salary of an employee and print the HRA (House Rent allowance) which is 12% of salary, DA (Dearness allowance) which is 15% of salary and print the gross.

Program:

#include<stdio.h>
int main()
{
int sal;
float hra,da,gross;
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);
return 0;
}

Execution:
Enter the salary: 3500
Salary details of employee
Salary 3500
House Rent Allowance 420.000000
Dearness Allowances 525.000000
Gross Salary 4445.000000

Specification: Accept the salary, sales of a sales person and print the commission which is 3% of sales and gross salary.
Program:


#include<stdio.h>
int main()
{
long int sal,sales;
float comm,gross;
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);
return 0;
}

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.
 

specification: Accept the temperature in degrees centigrade and print the temperature in Fahrenheit.

Formula:
C=(5/9)*(F-32)
F=C*(5/9)+32

Program:

#include<stdio.h>
int main()
{
int c;
float f;
printf("Enter the temperature in degrees centigrade:");
scanf("%d",&c);
f=c*((float)9/5)+32;
printf("Temperature in Fahrenheit %f",f);
return 0;
}

Execution 1:
Enter the temperature in degrees centigrade:0
Temperature in Fahrenheit 32.000000

Execution 2:
Enter the temperature in degrees centigrade:100
Temperature in Fahrenheit 212.000000

specification: 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>
int main()
{
int len,breadth,rad;
int rect_area,rect_peri;
float c_area,c_cir;
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);
return 0;
}

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

specification: Accept the distance between two cities in Kilo meters and print in meters, centimeters, miles, feet and inches.

Distance conversion table:

Distance conversion table
Program:

#include<stdio.h>
int main()
{
float kms,mts,cms,inch,fts,mile;
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);
return 0;
}

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

Previous post:

Next post: