0% found this document useful (0 votes)
16 views28 pages

Lab Activity2 Vinay Kapoor

Uploaded by

vinaykapoorin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views28 pages

Lab Activity2 Vinay Kapoor

Uploaded by

vinaykapoorin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

{Questions for Lab 2}

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() {

int intVar = 10;

float floatVar = 5.25;

char charVar = 'A';

double doubleVar = 123.456;

printf("Integer value: %d\n", intVar);

printf("Float value: %.2f\n", floatVar);

printf("Character value: %c\n", charVar);

printf("Double value: %.3f\n", doubleVar);

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;

printf("Enter first number: ");


scanf("%f", &num1);

printf("Enter second number: ");


scanf("%f", &num2);

printf("Addition: %.2f + %.2f = %.2f\n", num1, num2, num1 + num2);


printf("Subtraction: %.2f - %.2f = %.2f\n", num1, num2, num1 - num2);
printf("Multiplication: %.2f * %.2f = %.2f\n", num1, num2, 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() {

float floatNum = 9.75;

int intNum;

intNum = (int)floatNum;

printf("Original float value: %.2f\n", floatNum);

printf("Converted int value: %d\n", intNum);

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() {

int a = 5, b = 10, temp;

printf("before swapping\n");

printf("a = %d, b = %d\n", a, b);

temp = a;

a = b;

b = temp;

printf("after swapping\n");

printf("a = %d, b = %d\n", a, b);

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;

printf("Before Swap: a = %d, b = %d\n", a, b);

a = a ^ b;

b = a ^ b;

a = a ^ b;

printf("After Swap: a = %d, b = %d\n", 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() {

int num1, num2;

printf("Enter first integer: ");

scanf("%d", &num1);

printf("Enter second integer: ");

scanf("%d", &num2);

printf("%d == %d: %d\n", num1, num2, num1 == num2);

printf("%d != %d: %d\n", num1, num2, num1 != num2);

printf("%d < %d: %d\n", num1, num2, num1 < num2);

printf("%d > %d: %d\n", num1, num2, num1 > num2);

printf("%d <= %d: %d\n", num1, num2, num1 <= num2);

printf("%d >= %d: %d\n", num1, num2, num1 >= num2);

return 0;

Output:
7. Write a program to demonstrate the use of logical operators (&&, ||, !) in different
conditions.

Solution:

#include <stdio.h>

int main() {

int a = 10, b = 20;

if (a > 5 && b < 30) {

printf("Logical AND (&&): Both conditions are true\n");

} else {

printf("Logical AND (&&): One or both conditions are false\n");

if (a < 5 || b > 15) {

printf("Logical OR (||): At least one condition is true\n");

} else {

printf("Logical OR (||): Both conditions are false\n");

if (!(a > 15)) {

printf("Logical NOT (!): The condition a > 15 is false\n");

} else {

printf("Logical NOT (!): The condition a > 15 is true\n");

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;

printf("Pre-increment: %d\n", ++a);

printf("Value of a after pre-increment: %d\n", a);

printf("Post-increment: %d\n", b++);

printf("Value of b after post-increment: %d\n", b);

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;

printf("After assignment (=), a = %d\n", a);

a += b;

printf("After addition assignment (+=), a = %d\n", a);

a -= b;

printf("After subtraction assignment (-=), a = %d\n", a);

a *= b;

printf("After multiplication assignment (*=), a = %d\n", a);

a /= b;

printf("After division assignment (/=), a = %d\n", a);

a %= b;

printf("After modulus assignment (%%=), a = %d\n", a);

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() {

printf("Size of int: %zu bytes\n", sizeof(int));

printf("Size of float: %zu bytes\n", sizeof(float));

printf("Size of double: %zu bytes\n", sizeof(double));

printf("Size of char: %zu byte\n", sizeof(char));

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;

printf("Enter an integer: ");

scanf("%d", &num);

if (num % 2 == 0) {

printf("%d is even\n", num);

} else {

printf("%d is odd\n", num);

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;

printf("Enter principal amount (P): ");

scanf("%f", &P);

printf("Enter rate of interest (R): ");

scanf("%f", &R);

printf("Enter time period (T) in years: ");

scanf("%f", &T);

SI = (P * R * T) / 100;

printf("Simple Interest (SI) = %.2f\n", SI);

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() {

float a, b, c, discriminant, root1, root2;

printf("Enter coefficients a, b, and c: ");

scanf("%f %f %f", &a, &b, &c);

discriminant = b * b - 4 * a * c;

if (discriminant > 0) {

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("Roots are real and different.\n");

printf("Root 1 = %.2f\n", root1);

printf("Root 2 = %.2f\n", root2);

} else if (discriminant == 0) {

root1 = -b / (2 * a);

printf("Roots are real and the same.\n");

printf("Root 1 = Root 2 = %.2f\n", root1);

} else {

printf("Roots are complex and different.\n");

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() {

int num, sum = 0;

printf("Enter an integer: ");

scanf("%d", &num);

while (num != 0) {

sum += num % 10;

num /= 10;

printf("Sum of digits = %d\n", sum);

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() {

float celsius, fahrenheit;

printf("Enter temperature in Celsius: ");

scanf("%f", &celsius);

fahrenheit = (celsius * 9/5) + 32;

printf("Temperature in Fahrenheit = %.2f\n", fahrenheit);

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() {

float length, width, perimeter, area;

printf("Enter the length of the rectangle: ");

scanf("%f", &length);

printf("Enter the width of the rectangle: ");

scanf("%f", &width);

perimeter = 2 * (length + width);

area = length * width;

printf("Perimeter of the rectangle = %.2f\n", perimeter);

printf("Area of the rectangle = %.2f\n", area);

return 0;

Output:
17. Write a program to demonstrate compound assignment operators (+=, -=, *=, /=) by
performing operations on a variable.

Solution:

#include <stdio.h>

int main() {

int num = 10;

num += 5;

printf("After num += 5, num = %d\n", num);

num -= 3;

printf("After num -= 3, num = %d\n", num);

num *= 2;

printf("After num *= 2, num = %d\n", num);

num /= 4;

printf("After num /= 4, num = %d\n", num);

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() {

float weight, height, bmi;

printf("Enter weight in kg: ");

scanf("%f", &weight);

printf("Enter height in meters: ");

scanf("%f", &height);

bmi = weight / (height * height);

printf("Body Mass Index (BMI) = %.2f\n", bmi);

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;

printf("Enter a character: ");

scanf("%c", &ch);

printf("The ASCII value of '%c' is %d\n", ch, (int)ch);

return 0;

Output:

You might also like