The document contains 5 code snippets that demonstrate different programming concepts in C language:
1) Assigning values to variables of different data types (int, float, char) and printing their sizes.
2) Swapping values of two variables with and without using a third temporary variable.
3) Calculating and printing the area and volume of a cylinder given radius and height.
4) Finding the minimum of two numbers using a conditional operator.
5) Determining if a number is even or odd using a conditional operator.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
7 views
C-Practical 2
The document contains 5 code snippets that demonstrate different programming concepts in C language:
1) Assigning values to variables of different data types (int, float, char) and printing their sizes.
2) Swapping values of two variables with and without using a third temporary variable.
3) Calculating and printing the area and volume of a cylinder given radius and height.
4) Finding the minimum of two numbers using a conditional operator.
5) Determining if a number is even or odd using a conditional operator.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2
1 1.
//assigning the values to variables and printing its size
2 3 #include <stdio.h> 4 int main() 5 { 6 int i = 18; 7 float f = 5.5; 8 char c = 's'; 9 printf("The value of i is %d and its size is %d bytes.\n", i, sizeof(i)); 10 printf("The value of f is %f and its size is %d bytes.\n", f, sizeof(f)); 11 printf("The value of c is %c and its size is %d bytes.\n", c, sizeof(c)); 12 13 return 0; 14 } 15 16 Output: 17 The value of i is 18 and its size is 4 bytes 18 The value of f is 5.500000 and its size is 4 bytes 19 The value of c is S and its size is 1 bytes 20 21 2.//swapping two variable using third variable 22 23 #include <stdio.h> 24 int main() 25 { 26 int x = 10, y = 20, temp; 27 printf("Before swapping, x = %d and y = %d\n", x, y); 28 temp = x; 29 x = y; 30 y = temp; 31 printf("After swapping, x = %d and y = %d\n", x, y); 32 return 0; 33 } 34 35 Output: 36 Before swapping,x=10 and y=20 37 After swapping,x=20 and y=10 38 39 40 //swapping two variable without using third variable 41 42 #include <stdio.h> 43 int main() 44 { 45 int a=5, b=3; 46 printf("Before swapping: a = %d, b = %d\n", a, b); 47 a = a + b; 48 b = a - b; 49 a = a - b; 50 printf("After swapping: a = %d, b = %d\n", a, b); 51 return 0; 52 } 53 54 Output: 55 Before swapping:a=5,b=3 56 After swapping:a=3,b=5 57 58 1 3.//Area and Volume of cylinder 2 3 #include<stdio.h> 4 #define pi 3.1415 5 int main() 6 { 7 float r,h,area,volume; 8 printf("enter the radius of cylinder:"); 9 scanf("%f",&r); 10 11 printf("enter the height of cylinder:"); 12 scanf("%f",&h); 13 14 area=2*pi*r*(r+h); 15 volume=pi*r*r*h; 16 17 printf("The area of cylinder:%.2f\n",area); 18 printf("The volume of cylinder:%.2f\n",volume); 19 20 return 0; 21 } 22 23 Outout: 24 enter the radius of cylinder:2.5 25 enter the height of cylinder:3.5 26 The area of cylinder:94.25 27 The volume of cylinder:68.72 28 29 30 4.//minimum using conditional operator 31 32 #include <stdio.h> 33 int main() 34 { 35 int num1, num2, min; 36 printf("Enter the first number: "); 37 scanf("%d", &num1); 38 39 printf("Enter the second number: "); 40 scanf("%d", &num2); 41 42 min = (num1 < num2) ? num1 : num2; 43 printf("The minimum of %d and %d is: %d\n", num1, num2, min); 44 return 0; 45 } 46 Output: 47 Enter the first number:7 48 Enter the second number:8 49 The minimum of 7 and 8 is:7 50 51 52 5.//odd and even using conditional operator 53 #include <stdio.h> 54 int main() 55 { 56 int num; 57 printf("Enter a number: "); 58 scanf("%d", &num); 59 60 num % 2 == 0 ? printf("%d is even", num) : printf("%d is odd", num); 61 62 return 0; 63 } 64 Output: 65 enter the number:8 66 8 is even 67 enter the number:9 68 9 is odd