0% found this document useful (0 votes)
40 views14 pages

Practical Questions Set A

Uploaded by

shaikhirfant.0
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
40 views14 pages

Practical Questions Set A

Uploaded by

shaikhirfant.0
Copyright
© © All Rights Reserved
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/ 14

Q .

1) Write a C program to declare and initialize Variable of


different data types and display there sizes

#include <stdio.h>

int main() {
int int_var = 10;
float float_var = 3.14;
double double_var = 2.71828;
char char_var = 'A';

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


printf("Size of float: %zu bytes\n", sizeof(float_var));
printf("Size of double: %zu bytes\n", sizeof(double_var));
printf("Size of char: %zu bytes\n", sizeof(char_var));

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;

// Conditional expression to find the maximum of a and b


max = (a > b) ? a : b;

printf("The maximum of %d and %d is: %d\n", a, b, 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.

Q. 4 ) Create a C program to perform bitwise operations (and or )on


integer variable and print the result

#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

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


printf("num2: %d (Binary: %d)\n", num2, num2);
printf("Bitwise AND: %d (Binary: %d)\n", and_result, and_result);
printf("Bitwise OR: %d (Binary: %d)\n", or_result, or_result);

return 0;
}

Output –

num1: 15 (Binary: 15)


num2: 9 (Binary: 9)
Bitwise AND: 9 (Binary: 9)
Bitwise OR: 15 (Binary: 15)

Q . 5 ) Develop a c program to demonstrate the use of assignment


operators and evaluate expressions involving them
#include <stdio.h>

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;

printf("a: %d\n", a);


printf("b: %d\n", b);
printf("c: %d\n", c);

return 0;
}

Output –

a: 7
b: 0
c: 30

Q. 6) Create a function in c to calculate the factorial of a given


number and display the result

#include <stdio.h>

// Function to calculate factorial


int factorial(int n) {
if (n == 0) {
return 1; // Factorial of 0 is 1
} else {
return n * factorial(n - 1);
}
}

int main() {
int num;

printf("Enter a non-negative integer: ");


scanf("%d", &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:

// Enter any number


Q. 8) Write a C program to initialize and display elements of a one –
dimensional array

#include <stdio.h>

int main() {
int arr[5] = {10, 20, 30, 40, 50}; // Initialize array with values

printf("Elements of the array:\n");


for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

Output –
Elements of the array:
10 20 30 40 50

You might also like