Home C Lab Course – C

Lab Course – C

1633
0
Lab Course – C

Lab Course – C

Lab Course Manual
Exercise 1 Set A

Apply all the three program development steps for the following examples.
1. Accept dimensions of a cylinder and print the surface area and volume
(Hint: surface area = 2πr2  + 2πrh, volume = πr2h)

#include <stdio.h>
#include <math.h>
int main()
{
    float radius, height;
    float surface_area, volume;
    printf("Enter  value for  radius and height of a cylinder:");
    scanf("%f%f", &radius, &height);
    surface_area = 2 * (22 / 7) * radius * (radius + height);
    volume = (22 / 7) * radius * radius * height;
    printf("\nSurface area of cylinder is: %f", surface_area);
    printf("\nVolume of cylinder is : %f", volume);
} 
$ gcc vol.c
$ ./a.out
Enter value for radius and height of a cylinder: 3 3
Surface area of cylinder is: 108.000000

Volume of cylinder is : 81.000000
$

 


2. Accept temperatures in Fahrenheit (F) and print it in Celsius(C) and Kelvin (K) (Hint: C=5/9(F32), K = C + 273.15)

#include<stdio.h>
int main()
{
	float cel,fer,kel;
	 
	printf("Enter Temperature in Fahrenheit :");
	scanf("%f",&fer);
	 
	cel= (fer-32)/1.8 ;
	printf("Celsius = %f \n",cel);
	 
	kel = (fer-32)/1.8 + 273.15 ; 
	printf("Kelvin  = %f \n",kel);
	 
	return (0) ;
}  

Enter value for radius and height of a cylinder :
4
6
Surface area of cylinder is: 240.000000
Volume of cylinder is : 288.000000

3. Accept initial velocity (u), acceleration (a) and time (t). Print the final velocity (v) and the distance (s) travelled. (Hint: v = u + at, s = u + at2)

include<stdio.h>
void main()                    
{
	int sec;
	float distance , initial_velocity,velocity, acceleration;
	printf("Enter the time in seconds \n");
	scanf("%d",&sec);
	printf("Enter the initial velocity \n");
	scanf("%f", &initial_velocity);
	printf("Enter the acceleration \n");
	scanf("%f", &acceleration);
	velocity = (initial_velocity + (acceleration * sec));
	distance = (initial_velocity + (acceleration * (sec*sec)));\
	printf("Total velocity is %f",velocity);
	printf("Total distance travelled is  %f", distance);

}

Enter the time in seconds
60
Enter the initial velocity
5
Enter the acceleration
7
Total velocity is 425.000000
Total distance travelled is 25205.000000

 


4. Accept inner and outer radius of a ring and print the perimeter and area of the ring
(Hint:
perimeter = 2 π (a+b) , area = π (a2-b2) )

#include <stdio.h>
int main()
{
    float out_radius,in_radius,area, perimeter;
    printf("Enter inner radius of ring: ");
    scanf("%f",&in_radius);
    printf("Enter outer radius of ring: ");
    scanf("%f",&out_radius);
    perimeter= (2 * 3.14) * (in_radius + out_radius);
    area= 3.14 * ((in_radius * in_radius) + (out_radius * out_radius));
    printf("Area of circle: %f \nPerimeter of circle: %f\n",area,perimeter);
}
Enter inner radius of ring: 5
Enter outer radius of ring: 6
Area of circle: 191.539993
Perimeter of circle: 69.080002

5. Accept two numbers and print arithmetic and harmonic mean of the two numbers
(Hint: AM=
(a+b)/2 , HM = ab/(a+b) )

#include <stdio.h>
void main()
{
   int a,b;
   float a_m,h_m;
   printf("Enter two number A and B : \n");
   scanf("%d%d",&a,&b);
   a_m = (a+b) /2;
   h_m = (a*b) / (a+b);
   printf("Arithmetic Mean = %f \nHarmonic Mean = %f",a_m,h_m);
}
Enter two number A and B : 2 6
Arithmetic Mean = 4.000000
Harmonic Mean = 1.000000

6. Accept three dimensions length (l), breadth(b) and height(h) of a cuboid and print surface area and volume
(Hint : surface area=2(lb+lh+bh ), volume = lbh )


#include <stdio.h>
void main()
{
  float l, w, h;
  float SA, V;
 
  printf("Enter the Length, Width and Height of a Cuboid : \n");
  scanf("%f %f %f",&l, &w, &h);
  SA = 2 * (l * w + l * h + w * h);
  V = l * w * h;
  printf("\n The Surface Area of a Cuboid : %f\n",SA);
  printf("\n The Volume of a Cuboid : %f\n",V);
}
Enter the Length, Width and Height of a Cuboid : 2 4 6
The Surface Area of a Cuboid : 88.000000
The Volume of a Cuboid : 48.000000

7. Accept a character from the keyboard and display its previous and next character in order. Ex. If the character entered is ‘d’, display “The previous character is c”, “The next character is e”.

#include <stdio.h>
int main()
{
	char ch;
	printf("Enter character:\t");
	scanf("%c", &ch);
	printf("You entered: %c\n", ch);
	printf("Previous character: %c\n", ch - 1);
	printf("Next character: %c\n", ch + 1);
}

 

Enter character: f
You entered: f
Previous character: e
Next character: g

8. Accept a character from the user and display its ASCII value.

#include <stdio.h>
int main() {
    char c;
    printf("Enter a Character\n");
    scanf("%c",&c);
    printf("ASCII value of %c = %d",c,c);     
    return 0;
}

Enter a Character
A
ASCII value of A = 65

If you like the post Lab Course – C , please share your feedback!
also see

also see

C Programming language
Go Programming language
Linked List Array
Simplification Queue
DBMS Reasoning
Aptitude HTML
Previous articleNumber Series Set II
Next articleHTML Text Formatting

LEAVE A REPLY

Please enter your comment!
Please enter your name here