1) Write a c program to demonstrate the working of arithmetic operators (Associativity and
precedence of arithmetic operators is expected)
#include <stdio.h>
int main() {
int a = 10, b = 5, c = 2, result;
// Demonstrating precedence and associativity
result = a + b * c; // '*' has higher precedence than '+'
printf("Result of a + b * c = %d\n", result);
result = (a + b) * c; // Parentheses override precedence
printf("Result of (a + b) * c = %d\n", result);
result = a - b / c; // '/' has higher precedence than '-'
printf("Result of a - b / c = %d\n", result);
result = (a - b) / c; // Parentheses change the order of evaluation
printf("Result of (a - b) / c = %d\n", result);
result = a + b - c; // '+' and '-' have the same precedence; evaluated left to right
printf("Result of a + b - c = %d\n", result);
result = a * b / c; // '*' and '/' have the same precedence; evaluated left to right
printf("Result of a * b / c = %d\n", result);
return 0;
}
2) Write a c Program to find maximum of two numbers using conditional operator.
#include <stdio.h>
int main() {
int num1, num2, max;
// Input two numbers from the user
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Using the conditional operator to find the maximum number
max = (num1 > num2) ? num1 : num2;
// Output the result
printf("The maximum of %d and %d is: %d\n", num1, num2, max);
return 0;
}
3) Write a c Program to find maximum of three numbers using logical operators.
#include <stdio.h>
int main() {
int num1, num2, num3;
// Input three numbers from the user
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
// Using logical operators to find the maximum number
if ((num1 >= num2) && (num1 >= num3)) {
printf("The maximum number is: %d\n", num1);
}
else if ((num2 >= num1) && (num2 >= num3)) {
printf("The maximum number is: %d\n", num2);
}
else {
printf("The maximum number is: %d\n", num3);
}
return 0;
} #include <stdio.h>
int main() {
// Default values for num1, num2, and num3
int num1 = 15, num2 = 25, num3 = 20;
// Using logical operators to find the maximum number
if ((num1 >= num2) && (num1 >= num3)) {
printf("The maximum number is: %d\n", num1);
}
else if ((num2 >= num1) && (num2 >= num3)) {
printf("The maximum number is: %d\n", num2);
}
else {
printf("The maximum number is: %d\n", num3);
}
return 0;
}
4) Write a c Program to display Quotient and Remainder of division of two variable.
#include <stdio.h>
int main() {
// Default values for num1 and num2
int num1 = 25, num2 = 4, quotient, remainder;
// Check if num2 is zero to avoid division by zero
if (num2 == 0) {
printf("Error! Division by zero is not allowed.\n");
} else {
// Calculating quotient and remainder
quotient = num1 / num2;
remainder = num1 % num2;
// Output the quotient and remainder
printf("Default values: num1 = %d, num2 = %d\n", num1, num2);
printf("Quotient of %d divided by %d is: %d\n", num1, num2, quotient);
printf("Remainder of %d divided by %d is: %d\n", num1, num2, remainder);
}
return 0;
}
5) Write a C Program which illustrate increment and decrement operators (use of pre and post
increment is expected)
#include <stdio.h>
int main() {
int num = 5;
// Pre-increment: Increment the value first, then use it
printf("Pre-increment: \n");
printf("Initial value of num: %d\n", num);
printf("Value after pre-increment: %d\n", ++num); // Increments num and then uses it
printf("Value of num after pre-increment operation: %d\n\n", num);
// Post-increment: Use the value first, then increment it
printf("Post-increment: \n");
printf("Initial value of num: %d\n", num);
printf("Value after post-increment: %d\n", num++); // Uses num, then increments it
printf("Value of num after post-increment operation: %d\n\n", num);
// Pre-decrement: Decrement the value first, then use it
printf("Pre-decrement: \n");
printf("Initial value of num: %d\n", num);
printf("Value after pre-decrement: %d\n", --num); // Decrements num and then uses it
printf("Value of num after pre-decrement operation: %d\n\n", num);
// Post-decrement: Use the value first, then decrement it
printf("Post-decrement: \n");
printf("Initial value of num: %d\n", num);
printf("Value after post-decrement: %d\n", num--); // Uses num, then decrements it
printf("Value of num after post-decrement operation: %d\n", num);
return 0;
}
6) Write a program to display the size of different data types.
#include <stdio.h>
int main() {
// Display size of various data types
printf("Size of char: %zu bytes\n", sizeof(char));
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 long: %zu bytes\n", sizeof(long));
printf("Size of long long: %zu bytes\n", sizeof(long long));
printf("Size of short: %zu bytes\n", sizeof(short));
printf("Size of unsigned int: %zu bytes\n", sizeof(unsigned int));
return 0;
}
7)Write a program to swap the values of two variables using bitwise operator(^).
#include <stdio.h>
int main() {
int a = 5, b = 9;
printf("Before swapping: a = %d, b = %d\n", a, b);
// Swapping using XOR
a = a ^ b; // Step 1: a now holds the result of a ^ b
b = a ^ b; // Step 2: b now holds the original value of a
a = a ^ b; // Step 3: a now holds the original value of b
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
8)Write a c program which illustrate the use of Bitwise And, Bitwise Or and Bitwise XOR operator.
#include <stdio.h>
int main() {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
printf("a & b = %d\n", a & b); // Bitwise AND
printf("a | b = %d\n", a | b); // Bitwise OR
printf("a ^ b = %d\n", a ^ b); // Bitwise XOR
return 0;
}
a & b: Performs bitwise AND. Example: 0101 & 0011 = 0001 (Decimal 1).
a | b: Performs bitwise OR. Example: 0101 | 0011 = 0111 (Decimal 7).
a ^ b: Performs bitwise XOR. Example: 0101 ^ 0011 = 0110 (Decimal 6).
9)Write a c program which illustrate biutwise left shift and right shift operators.
#include <stdio.h>
int main() {
int num = 8; // Binary: 1000
// Left shift by 1 position
int leftShift = num << 1; // Binary: 10000 (Decimal: 16)
// Right shift by 1 position
int rightShift = num >> 1; // Binary: 0100 (Decimal: 4)
printf("Original number: %d\n", num);
printf("After left shift (num << 1): %d\n", leftShift);
printf("After right shift (num >> 1): %d\n", rightShift);
return 0;
}
```
### Explanation:
1. **Left Shift (`<<`)**:
- Shifts all bits of the number to the left by the specified number of positions.
- Adds `0` bits on the right.
- Example: `8 << 1` shifts `1000` to `10000` (Decimal `16`).
2. **Right Shift (`>>`)**:
- Shifts all bits of the number to the right by the specified number of positions.
- Removes bits on the right and may add `0` or `1` on the left depending on the system and sign of the
number.
- Example: `8 >> 1` shifts `1000` to `0100` (Decimal `4`).
### Output:
```
Original number: 8
After left shift (num << 1): 16
After right shift (num >> 1): 4
10) Write a C program to check Least Significant Bit(LSB) of a number is set or not.
#include <stdio.h>
int main() {
int num = 10; // Default number, you can change this value
printf("The number is: %d\n", num);
// Check the Least Significant Bit
if (num & 1) {
printf("The Least Significant Bit (LSB) of %d is set (1).\n", num);
} else {
printf("The Least Significant Bit (LSB) of %d is not set (0).\n", num);
}
return 0;
}
Explanation:
The program uses num = 10 as the default value. You can modify this default value as needed.
It then checks whether the LSB is set (1) or not (0) using num & 1.
11) Write a c program to check most Significant Bit of a number is set or not.
#include <stdio.h>
#include <limits.h> // For CHAR_BIT
int main() {
int num = -1; // Default number (you can change this value)
// Calculate the MSB mask for a 32-bit integer
unsigned int msbMask = 1U << (sizeof(int) * CHAR_BIT - 1);
printf("The number is: %d\n", num);
// Check the Most Significant Bit
if (num & msbMask) {
printf("The Most Significant Bit (MSB) of %d is set (1).\n", num);
} else {
printf("The Most Significant Bit (MSB) of %d is not set (0).\n", num);
}
return 0;
}
12) Write a C program to flip bits of a binary number using bitwise operator.
#include <stdio.h>
int main() {
unsigned int num = 10; // Default number (Binary: 1010)
printf("Original number: %u\n", num);
// Flip the bits using ~ operator
unsigned int flipped = ~num;
printf("Number after flipping bits: %u\n", flipped);
return 0;
}
13)Write a C program to check whether a number is even or odd using bitwise operator.
#include <stdio.h>
int main() {
int num = 7; // Default number (you can change this value)
printf("The number is: %d\n", num);
// Check if the number is even or odd
if (num & 1) {
printf("%d is an odd number.\n", num);
} else {
printf("%d is an even number.\n", num);
}
return 0;
}