Lab Activity2 Vinay Kapoor
Lab Activity2 Vinay Kapoor
1. Write a C program to declare variables of different data types (int, float, char, double) and
print their values.
Solution:
#include <stdio.h>
int main() {
return 0;
Output:
2. Write a program to perform basic arithmetic operations (+, -, *, /, %) on two numbers
provided by the user.
Solution:
#include <stdio.h>
int main() {
float num1, num2;
if (num2 != 0) {
printf("Division: %.2f / %.2f = %.2f\n", num1, num2, num1 / num2);
printf("Modulo: %.0f %% %.0f = %.0f\n", num1, num2, (int)num1 % (int)num2);
} else {
printf("Division and Modulo are not possible with zero as divisor.\n");
}
return 0;
}
Output:
3. Write a program that demonstrates explicit type casting by converting a float to an int and
printing both values.
Solution:
#include <stdio.h>
int main() {
int intNum;
intNum = (int)floatNum;
return 0;
Output:
4. Write a program to swap the values of two variables using a third temporary variable.
Solution:
#include <stdio.h>
int main() {
printf("before swapping\n");
temp = a;
a = b;
b = temp;
printf("after swapping\n");
return 0;
Output:
5. Write a program to swap the values of two variables without using a third variable, using
arithmetic or bitwise operators.
Solution:
#include <stdio.h>
int main() {
int a = 5, b = 10;
a = a ^ b;
b = a ^ b;
a = a ^ b;
return 0;
Output:
6. Write a program to take two integers as input and demonstrate the use of relational
operators (==, !=, <, >, <=, >=).
Solution:
#include <stdio.h>
int main() {
scanf("%d", &num1);
scanf("%d", &num2);
return 0;
Output:
7. Write a program to demonstrate the use of logical operators (&&, ||, !) in different
conditions.
Solution:
#include <stdio.h>
int main() {
} else {
} else {
} else {
return 0;
}
Output:
8. Write a program that demonstrates the difference between pre-increment and post-
increment operators.
Solutions:
#include <stdio.h>
int main() {
int a = 5, b = 5;
return 0;
Output:
9. Write a program to demonstrate the use of assignment operators (=, +=, -=, *=, /=, %=).
Solution:
#include <stdio.h>
int main() {
int a = 10, b = 5;
a = b;
a += b;
a -= b;
a *= b;
a /= b;
a %= b;
return 0;
Output:
10. Write a program to print the size of different data types (int, float, double, char) using the
sizeof() operator.
Solution:
#include <stdio.h>
int main() {
return 0;
Output:
11. Write a program that takes an integer as input and checks if it is even or odd using the
modulus operator (%).
Solution:
#include <stdio.h>
int main() {
int num;
scanf("%d", &num);
if (num % 2 == 0) {
} else {
return 0;
Output:
12. Write a program to calculate simple interest using the formula SI = (P * R * T) / 100, where P,
R, and T are provided by the user.
Solution:
#include <stdio.h>
int main() {
float P, R, T, SI;
scanf("%f", &P);
scanf("%f", &R);
scanf("%f", &T);
SI = (P * R * T) / 100;
return 0;
Output:
13. Write a program to find the roots of a quadratic equation using the formula (-b ± √(b² -
4ac)) / 2a.
Solution:
#include <stdio.h>
#include <math.h>
int main() {
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
} else if (discriminant == 0) {
root1 = -b / (2 * a);
} else {
return 0;
}
Output:
14. Write a program to find the sum of digits of a given integer using the modulus (%) and
division (/) operators.
Solution:
#include <stdio.h>
int main() {
scanf("%d", &num);
while (num != 0) {
num /= 10;
return 0;
Output:
15. Write a program to convert temperature from Celsius to Fahrenheit using the formula F = (C
* 9/5) + 32.
Solution:
#include <stdio.h>
int main() {
scanf("%f", &celsius);
return 0;
Output:
16. Write a program to calculate the perimeter and area of a rectangle, taking the length and
width as input.
Solution:
#include <stdio.h>
int main() {
scanf("%f", &length);
scanf("%f", &width);
return 0;
Output:
17. Write a program to demonstrate compound assignment operators (+=, -=, *=, /=) by
performing operations on a variable.
Solution:
#include <stdio.h>
int main() {
num += 5;
num -= 3;
num *= 2;
num /= 4;
return 0;
Output:
18. Write a program to calculate the Body Mass Index (BMI) of a person given their weight (in kg)
and height (in meters) using the formula BMI = weight / (height * height).
Solution:
#include <stdio.h>
int main() {
scanf("%f", &weight);
scanf("%f", &height);
return 0;
Output:
19. Write a program to take a character as input and print its corresponding ASCII value using
type conversion.
Solution:
#include <stdio.h>
int main() {
char ch;
scanf("%c", &ch);
return 0;
Output: