Practical Questions Set A
Practical Questions Set A
#include <stdio.h>
int main() {
int int_var = 10;
float float_var = 3.14;
double double_var = 2.71828;
char char_var = 'A';
return 0;
}
Output-
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 bytes
Note - The %zu format specifier in C is used to print the value of a size_t type variable.
Q. 2 ) Implement arithmetic , rational and logical operations in c
programme and display the results.
#include <stdio.h>
int main() {
int a = 10, b = 5;
float x = 3.5, y = 2.2;
// Arithmetic operations
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
printf("Arithmetic operations:\n");
printf("Sum: %d\n", sum);
printf("Difference: %d\n", difference);
printf("Product: %d\n", product);
printf("Quotient: %d\n", quotient);
printf("Remainder: %d\n\n", remainder);
// Rational operations
float sum_float = x + y;
float difference_float = x - y;
float product_float = x * y;
float quotient_float = x / y;
printf("Rational operations:\n");
printf("Sum: %.2f\n", sum_float);
printf("Difference: %.2f\n", difference_float);
printf("Product: %.2f\n", product_float);
printf("Quotient: %.2f\n\n", quotient_float);
// Logical operations
int result_and = a && b; // Both a and b must be non-zero
int result_or = a || b; // At least one of a or b must be non-zero
int result_not = !a; // Negation of a
printf("Logical operations:\n");
printf("AND (a && b): %d\n", result_and);
printf("OR (a || b): %d\n", result_or);
printf("NOT (!a): %d\n", result_not);
return 0;
}
Output –
Arithmetic operations:
Sum: 15
Difference: 5
Product: 50
Quotient: 2
Remainder: 0
Rational operations:
Sum: 5.70
Difference: 1.30
Product: 7.70
Quotient: 1.59
Logical operations:
AND (a && b): 1
OR (a || b): 1
NOT (!a): 0
Q . 3 ) Write a program to demonstrate the use of conditional
expressions and explain the order of evaluation.
#include <stdio.h>
int main() {
int a = 10, b = 5, max;
return 0;
}
Output –
The maximum of 10 and 5 is : 10
Explanation:
Conditional Expression:
• The line max = (a > b) ? a : b; is a conditional expression, also
known as the ternary operator.
• It has the following structure: condition ? expression1 :
expression2
• If the condition is true, expression1 is evaluated and its value
is assigned to the variable.
• If the condition is false, expression2 is evaluated and its value
is assigned to the variable.
Order of Evaluation:
1. Condition Evaluation: The condition a > b is evaluated first. In
this case, a is 10 and b is 5, so the condition is true.
2. Expression1 Evaluation: Since the condition is true, the value
of a (which is 10) is assigned to the variable max.
#include <stdio.h>
int main() {
int num1 = 15; // Binary: 00001111
int num2 = 9; // Binary: 00001001
// Bitwise AND
int and_result = num1 & num2; // Binary: 00001001
// Bitwise OR
int or_result = num1 | num2; // Binary: 00001111
return 0;
}
Output –
int main() {
int a = 10, b = 5;
// Simple assignment
int c = a;
// Addition assignment
a += b; // Equivalent to a = a + b;
// Subtraction assignment
b -= 2; // Equivalent to b = b - 2;
// Multiplication assignment
c *= 3; // Equivalent to c = c * 3;
// Division assignment
a /= 2; // Equivalent to a = a / 2;
// Modulus assignment
b %= 3; // Equivalent to b = b % 3;
return 0;
}
Output –
a: 7
b: 0
c: 30
#include <stdio.h>
int main() {
int num;
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
int result = factorial(num);
printf("Factorial of %d is: %d\n", num, result);
}
return 0;
}
Output –
Enter an integer: 5 Factorial of 5 is 120
Q. 7 ) implement a menu – driven program in c using switch – case
statement to perform arithmetic operations
#include <stdio.h>
int main() {
int num1, num2, choice;
float result;
printf("Menu:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
result = (float)num1 + num2;
printf("Sum: %.2f\n", result);
break;
case 2:
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
result = (float)num1 - num2;
printf("Difference: %.2f\n", result);
break;
case 3:
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
result = (float)num1 * num2;
printf("Product: %.2f\n", result);
break;
case 4:
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num2 == 0) {
printf("Error: Division by zero!\n");
} else {
result = (float)num1 / num2;
printf("Quotient: %.2f\n", result);
}
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
return 0;
}
Output –
Menu:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50}; // Initialize array with values
return 0;
}
Output –
Elements of the array:
10 20 30 40 50