Practical Based on C Language
Assignment No. 1
Set A. Apply all the three program development steps for the following examples.
1. Surface Area and Volume of a Cylinder
#include <stdio.h>
#define PI 3.14159
int main()
{
float r, h, surfaceArea, volume;
printf("Enter radius and height of the cylinder: ");
scanf("%f %f", &r, &h);
surfaceArea = 2 * PI * r * r + 2 * PI * r * h;
volume = PI * r * r * h;
printf("Surface Area = %.2f\n", surfaceArea);
printf("Volume = %.2f\n", volume);
return 0;
}
2. Fahrenheit to Celsius and Kelvin
#include <stdio.h>
int main()
{
float f, c, k;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &f);
c = (5.0 / 9) * (f - 32);
k = c + 273.15;
printf("Celsius = %.2f\n", c);
printf("Kelvin = %.2f\n", k);
return 0;
}
3. Final Velocity and Distance
#include <stdio.h>
int main()
{
float u, a, t, v, s;
printf("Enter initial velocity, acceleration and time: ");
scanf("%f %f %f", &u, &a, &t);
v = u + a * t;
s = u * t + 0.5 * a * t * t;
printf("Final Velocity = %.2f\n", v);
printf("Distance Travelled = %.2f\n", s);
return 0;
}
4. Area and Perimeter of a Ring
#include <stdio.h>
#define PI 3.14159
int main()
{
float a, b, perimeter, area;
printf("Enter inner radius and outer radius: ");
scanf("%f %f", &a, &b);
perimeter = 2 * PI * (a + b);
area = PI * (b * b - a * a);
printf("Perimeter = %.2f\n", perimeter);
printf("Area = %.2f\n", area);
return 0;
}
5. Arithmetic and Harmonic Mean
#include <stdio.h>
int main()
{
float a, b, am, hm;
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);
am = (a + b) / 2;
hm = (a * b) / (a + b);
printf("Arithmetic Mean = %.2f\n", am);
printf("Harmonic Mean = %.2f\n", hm);
return 0;
}
6. Surface Area and Volume of a Cuboid
#include <stdio.h>
int main()
{
float l, b, h, surfaceArea, volume;
printf("Enter length, breadth and height of the cuboid: ");
scanf("%f %f %f", &l, &b, &h);
surfaceArea = 2 * (l * b + l * h + b * h);
volume = l * b * h;
printf("Surface Area = %.2f\n", surfaceArea);
printf("Volume = %.2f\n", volume);
return 0;
}
7. Previous and Next Character
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
printf("Previous character: %c\n", ch - 1);
printf("Next character: %c\n", ch + 1);
return 0;
}
8. ASCII Value of a Character
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
printf("ASCII value of %c is %d\n", ch, ch);
return 0;
}
Set B. Apply all the three program development steps for the following examples.
1. Distance Between Two Points
#include <stdio.h>
#include <math.h>
int main()
{
float x1, y1, x2, y2, distance;
printf("Enter x and y coordinates of first point: ");
scanf("%f %f", &x1, &y1);
printf("Enter x and y coordinates of second point: ");
scanf("%f %f", &x2, &y2);
distance = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
printf("Distance between points = %.2f\n", distance);
return 0;
}
2. Interchange Two Integers
#include <stdio.h>
int main()
{
int a, b, temp;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("After interchanging: a = %d, b = %d\n", a, b);
return 0;
}
3. Cashier Currency Distribution (1, 5, 10 notes)
#include <stdio.h>
int main()
{
int amount, tens, fives, ones;
printf("Enter the amount to be withdrawn: ");
scanf("%d", &amount);
tens = amount / 10;
amount %= 10;
fives = amount / 5;
amount %= 5;
ones = amount;
printf("10s: %d\n", tens);
printf("5s: %d\n", fives);
printf("1s: %d\n", ones);
return 0;
}