Facebook Experiences Widespread Outages on March 5 2024

ERC First Semester C Programming LAB SHEET NO.2 [To be familiar Data types, Constants, Operators and Expressions]

Question

  1. WAP to declare integer, float, and character variables. Initialize them with certain values and print those values. Also, display the size of variables.
  2. WAP to swap the values of the variable with and without using a third variable.
  3. WAP to calculate the area and volume of a cylinder using pre-processor directive for the value of PI.
  4. WAP to input two numbers from the user and display the minimum using a conditional operator.
  5. WAP to display whether a number is even or odd using a conditional operator.

Complete Lab Sheet 2

Objective(s): To be familiar with Data types, Constants, Operators, and Expressions

Solution

1. WAP to declare integer, float, and character variables. Initialize them with certain values and print those values. Also, display the size of variables.

C Program: Code

#include<stdio.h>
int main()
{
int a=5;
float b=12.5;
char c='s';
printf("The values are %d, %f, %c\n", a,b,c);
printf("Size of int: %d bytes\n", sizeof (a));
printf("Size of float: %d bytes\n", sizeof (b));
printf("Size of character: %d bytes\n", sizeof (c));
return 0;
}

Output:

The values are 5, 12.500000, s
Size of int: 4 bytes
Size of float: 4 bytes
Size of character: 1 bytes

Discussion and Conclusion:

Discussion:
The given code is a simple C program that declares and initializes variables of different data types (int, float, char) and then prints their values using the printf function. It also prints the size of each variable using the sizeof operator.
Conclusion:
This code demonstrates the usage of variables and data types in C programming. It shows how to print values and determine the size of variables using the sizeof operator.

2. WAP to swap the values of the variable with and without using a third variable.

C Program: Code

#include<stdio.h>
int main()
{
int a,b;
printf("Enter the values of a and b: ");
scanf("%d%d",&a,&b);
printf("Before swap a=%d and b=%d\n", a,b);
a=a+b;
b=a-b;
a=a-b;
printf("After swap a=%d and b=%d", a,b);
return 0;
}

Output:

Enter the values of a and b:15
23
Before swap a=15 and b=23
After swap a=23 and b=15

Discussion and Conclusion:

Discussion:
The given code is a C program that takes input from the user for two integers (a and b) using the scanf function. It then performs swapping of the values of a and b without using a temporary variable. Finally, it prints the updated values of a and b using the printf function.
Conclusion:
This code demonstrates how to swap the values of two variables without using a temporary variable in C programming. It showcases the use of arithmetic operations (addition and subtraction) to achieve the swapping effect.

3. WAP to calculate the area and volume of a cylinder using pre-processor directive for the value of PI.

C Program: Code

#include<stdio.h>
#include<math.h>
#define PI 3.1415
int main()
{
int r,h;
float area, volume;
printf("Enter the values of r and h\n");
scanf("%d%d", &r,&h);
area=2*PI*r*(r+h);
volume=PI*(pow(r,2))*h;
printf("The area of Cylinder is %f\n", area);
printf("The volume of Cylinder is %f", volume);
return 0;
}

Output:

Enter the values of r and h
1
2
The area of Cylinder is 18.849001
The volume of Cylinder is 6.283000

Discussion and Conclusion:

Discussion:
The given code is a C program that calculates the area and volume of a cylinder based on user input for the values of radius (r) and height (h). It uses the math.h library for the pow() function to calculate the volume. The program defines a constant PI with the value 3.1415 using the #define directive.
Conclusion:
This code demonstrates the calculation of the area and volume of a cylinder using user-provided values for the radius and height. It showcases the use of mathematical formulas and the pow() function from the math.h library to perform the calculations.

4. WAP to input two numbers from the user and display the minimum using a conditional operator.

C Program: Code

#include<stdio.h>
int main ()
{
int a,b, minimum;
printf("Enter the value of a and b\n");
scanf("%d%d",&a,&b);
minimum=a<b? a:b;
printf("Minimum value is %d", minimum);
return 0;
}

Output:

Enter the value of a and b
1
2
Minimum value is 1

Discussion and Conclusion:

Discussion:
The given code is a C program that takes input from the user for two integers (a and b) using the scanf function. It then uses the conditional operator (?:) to compare the values of a and b and assigns the smaller value to the variable minimum. Finally, it prints the minimum value using the printf function.
Conclusion:
This code demonstrates how to find the minimum value between two integers using the conditional operator in C programming. It showcases the use of the ternary operator to perform a conditional assignment and determine the smaller value.

5. WAP to display whether a number is even or odd using a conditional operator.

C Program: Code

#include<stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
(num % 2 == 0) ? printf("%d is even", num): printf("%d is odd", num);
return 0;
}

Output:

Enter a number: 15
15 is odd

Discussion and Conclusion:

Discussion:
The given code is a C program that takes input from the user for a number using the scanf function. It then uses the conditional operator (?:) to check if the number is even or odd. If the number is divisible by 2 (i.e., num % 2 == 0), it prints that the number is even. Otherwise, it prints that the number is odd.
Conclusion:
This code demonstrates how to determine if a number is even or odd using the conditional operator in C programming. It shows the use of the modulo operator (%) to check the remainder of dividing the number by 2 and uses the conditional operator to print the appropriate message.

6. What is the output of the given code:

#include<stdio.h>
int main()
{
int a = 5, b = 9;
printf("a = %d, b = %d\n", a, b);
printf("a&b = %d\n", a & b);
printf("a|b = %d\n", a | b);
printf("a^b = %d\n", a ^ b);
printf("~a = %d\n", ~a);
printf("(b<<2)+(a<<1) = %d\n",(b<<2)+(a<<1));
printf("(b>>1)+(a>>1)=%d\n",(b>>1)+(a>>1));
return 0;
}

Output:

a = 5, b = 9
a&b = 1
a|b = 13
a^b = 12
~a = -6
(b<<2)+(a<<1) = 46
(b>>1)+(a>>1)=6

Discussion and Conclusion:

Discussion:
The given code is a C program that performs bitwise operations on two integers, a and b. It uses the printf function to display the values and results of the bitwise operations.
Conclusion:
This code demonstrates the usage of bitwise operators in C programming. It showcases the bitwise AND (&), bitwise OR (|), bitwise XOR (^), bitwise NOT (~), left shift (<<), and right shift (>>) operations. The program performs these operations on the variables a and b and displays the results using the printf function.
C Programming Lab
A free online educational resource provider.

Post a Comment

We get inspired from your single comment.

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing Lantro UI, you agreed to use cookies in agreement with the Lantro UI's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.