Programming for Problem Solving
Unit-2 One Shot + PYQ Solutions
PYQs
Q.1 Differentiate between implicit and explicit type conversion.
Q.2 Explain various bitwise operators in C Language with help of an example.
When precedence of two operators in an expression is same, how associativity
helps in identifying which operator will be evaluated first. Illustrate it with an
example.
Q.3 Write the output of following code:
#include <stdio.h>
int main()
{
int a = -10, b = 20;
if(a > 0 && b < 0)
a++;
else if(a < 0 && b < 0)
a--;
else if(a < 0 && b > 0)
b--;
else
b--;
printf("%d\n",a + b);
return 0;
}
Q.4 Write a program in C to print grades as per following criteria for obtained
percentage of marks M out 100.
Obtained Percent Marks (M) Grade
90 <M≤ 100 A+
80 <M≤ 90 A
70 <M≤ 80 B+
60 <M≤ 70 B
50 <M≤ 60 C
M≤ 50 F
Q.5 Explain different types of bitwise operators used in C with suitable examples. Find
the value of following expressions:
i) 10 >>2 ii) 20 << 2 iii) 25 & 30 iv) 25 | 30
Q.6 Illustrate the Concept of Operator Precedence and Associativity with
Example.
Q.7 Write a Program to discuss the use of break in Switch Statement.
Q.8 Discuss the Concept of Type Casting and Type Conversion with the
Program.
Q.9 Differentiate Between Operator and Operands.
Q.10 Define Conditional Operator with an example.
Q.11 Define Explicit type conversion with suitable example.
Q.12 Find the value of variable max in the following code:
int a=-10, b=20;
int max= (a>b)? a: b;
Q.13 What is the importance of Precedence order and associativity? Explain Bitwise and Ternary
operators in detail.
Q.14 Why break statement is required? Write a C program in C language to check character is a vowel
or consonant
Q.15 Write a program in C language to calculate energy bill. Read the starting and ending meter
readings. The charges are as follows: -
No. of Units Consumed rates in Rs.
0<units<=200 Rs 5.50 per Unit
200<units<=400 Rs 700 + Rs 6.0 per unit excess of 200
400<units<= 600 Rs 1400 + Rs 7.50 per unit excess of 400
600<units Rs 1850+ Rs 9.0 per Unit excess of 600
Topic 1: Operators & Expressions in C
✅ What are Operators?
It is a symbol that performs a specific operation on one or more values (operands). In simple
terms, operators help us manipulate data and perform calculations in a program.
✅ What are Expressions?
An expression is a combination of operators and operands that produces a value.
✅ Types of Operators in C
✅ Arithmetic Operators (+, -, *, /, %)
✅ Relational Operators (>, <, >=, <=, ==, !=)
✅ Logical Operators (&&, ||, !)
✅ Bitwise Operators (&, |, ^, ~, <<, >>)
✅ Assignment Operators (=, +=, -=, *=, /=, %=)
✅ Conditional (Ternary) Operator ( ? : )
1️⃣ Arithmetic Operators: These operators perform basic mathematical operations such as
addition, subtraction, multiplication, and division.
#include <stdio.h>
int main() {
int a = 10, b = 3;
Output:
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
Addition: 13
printf("Multiplication: %d\n", a * b); Subtraction: 7
printf("Division: %d\n", a / b); // 10/3 = 3 (Integer division) Multiplication: 30
printf("Modulus: %d\n", a % b); // 10%3 = 1 (Remainder)
Division: 3
return 0; Modulus: 1
}
2️⃣ Relational Operators: Used to compare two values. They always return true (1) or false
(0).
Output:
#include <stdio.h>
int main() {
1
int a = 10, b = 5;
0
printf("%d\n", a > b); // 1 (True)
0
printf("%d\n", a < b); // 0 (False)
1
printf("%d\n", a == b); // 0 (False)
printf("%d\n", a != b); // 1 (True)
return 0;
}
3️⃣ Logical Operators: Used for combining multiple conditions.
Output:
#include <stdio.h>
int main() { 1
int a = 10, b = 5; 1
printf("%d\n", (a > 5) && (b > 2)); // 1 (Both True) 0
printf("%d\n", (a > 5) || (b < 2)); // 1 (At least one True)
printf("%d\n", !(a == 10)); // 0 (Reverses the condition.)
return 0;
}
4️⃣ Assignment Operators: Used to assign values to variables.
Output:
#include <stdio.h>
int main() {
int a = 10; 15
a += 5; // Same as a = a + 5
printf("%d\n", a); // Output: 15
return 0;
}
5️⃣ Increment & Decrement Operators
#include <stdio.h> Output:
int main() {
6
int x = 5;
printf("%d\n", ++x); // Pre-increment (First increase, then print) 6
5
printf("%d\n", x--); // Post-decrement (First print, then decrease)
printf("%d\n", x);
return 0;
}
PYQ: What is the difference between Operator and Operand? Explain with an
example.
Operator: A symbol that performs operations on variables/values.
Operand: The values on which the operator acts.
Here, + is an operator.
int result = 10 + 5;
10 and 5 are operands.
Bitwise Operators in C
✅ What are Bitwise Operators?
Bitwise operators perform operations at the bit level (0s and 1s).
These operators work only on integers (not on float/double).
📌 Example: 5 → 00000101 (Binary Representation)
1️⃣ Bitwise AND (&): Both bits must be 1 for the result to be 1.
Example: Output:
#include <stdio.h> a&b=1
int main() {
int a = 5, b = 3;
printf("a & b = %d\n", a & b);
return 0;
}
Binary Calculation:
5 → 00000101
3 → 00000011
& → 00000001 (1 in decimal)
2️⃣ Bitwise OR (|): If any bit is 1, the result is 1.
Example: Output:
#include <stdio.h> a|b=7
int main() {
int a = 5, b = 3;
printf("a | b = %d\n", a | b);
return 0;
}
Binary Calculation:
5 → 00000101
3 → 00000011
| → 00000111 (7 in decimal)
3️⃣ Bitwise XOR (^): Returns 1 if bits are different.
Example:
Output:
#include <stdio.h> a^b=6
int main() {
int a = 5, b = 3;
printf("a ^ b = %d\n", a ^ b);
return 0;
}
Binary Calculation:
5 → 00000101
3 → 00000011
^ → 00000110 (6 in decimal)
4️⃣ Bitwise NOT (~): Flips all bits (1 → 0, 0 → 1).
Example:
Output:
#include <stdio.h> ~a = -6
int main() {
int a = 5;
printf("~a = %d\n", ~a);
return 0;
}
Binary Calculation:
5 → 00000101
~5 → 11111010 (-6 in decimal)
5️⃣ Left Shift (<<): Shifts bits to the left.
Example:
Output:
#include <stdio.h> a << 1 = 10
int main() {
int a = 5;
printf("a << 1 = %d\n", a << 1);
return 0;
}
Binary Calculation:
5 → 00000101
5<<1 → 00001010 (10 in decimal)
6️⃣ Right Shift (>>): Shifts bits to the right.
Example:
Output:
#include <stdio.h> a >> 1 = 2
int main() {
int a = 5;
printf("a >> 1 = %d\n", a >> 1);
return 0;
}
Binary Calculation:
5 → 00000101
5>>1 → 00000010 (2 in decimal)
i) 10 >> 2 (Right Shift)
Binary of 10: 00001010
Shift right by 2: 00000010 (binary) = 2 (decimal).
Answer: 10 >> 2 = 2
ii) 20 << 2 (Left Shift)
Binary of 20: 00010100
Shift left by 2: 01010000 (binary) = 80 (decimal).
Answer: 20 << 2 = 80
iii) 25 & 30 (Bitwise AND)
Binary of 25: 00011001
Binary of 30: 00011110
Bitwise AND: 00011000 (binary) = 24 (decimal).
Answer: 25 & 30 = 24
iv) 25 | 30 (Bitwise OR)
Binary of 25: 00011001
Binary of 30: 00011110
Bitwise OR: 00011111 (binary) = 31 (decimal).
Answer: 25 | 30 = 31
Q. Write a C program to check whether a number is EVEN or ODD using Bitwise
Operators.
✔ A number is EVEN if the last bit is 0.
✔ A number is ODD if the last bit is 1.
✔ To check, use num & 1 (if result is 1 → Odd, else Even).
Output:
#include <stdio.h>
int main() { Enter a number: 7
int num; 7 is Odd
printf("Enter a number: ");
scanf("%d", &num);
if (num & 1)
printf("%d is Odd\n", num);
else
printf("%d is Even\n", num);
return 0;
}
Conditional Operator (Ternary Operator)
The conditional operator (? :) is a ternary operator that evaluates a condition and
returns one of two values based on whether the condition is true or false.
The conditional operator (? :) is a shorthand way of writing an if-else statement. It
takes three operands:
1. A condition (evaluates to true or false).
2. A value if true.
3. A value if false.
condition ? expression1 : expression2;
If the condition is true, expression1 is evaluated.
If the condition is false, expression2 is evaluated.
int a = 10, b = 20; Here, (a > b) is the condition.
int max = (a > b) ? a : b; If a > b is true, max = a.
If a > b is false, max = b.
Output: max = 20 (since a > b is false).
Precedence & Associativity of Operators in C
Precedence defines the order in which operators are evaluated in an expression.
Higher precedence operators are evaluated first.
For example, in 10 + 5 * 2,
* has higher precedence than +, so 5 * 2 is evaluated first, then 10 + 10.
Associativity determines the direction of execution when operators have the
Associativity determines the direction of execution when operators have the
same precedence. It can be Left-to-Right or Right-to-Left.
1. Left-to-Right Associativity: Operators are evaluated from left to right.
Example: 10 - 5 - 2 is evaluated as (10 - 5) - 2.
2. Right-to-Left Associativity: Operators are evaluated from right to left.
Example: a = b = 10 is evaluated as a = (b = 10).
Topic 2: Type Conversion & Type Casting in C
✅ What is Type Conversion?
It is the process of converting one data type into another in C.
There are two types of type conversion in C:
1️⃣ Implicit Type Conversion (Automatic Type Conversion)
2️⃣ Explicit Type Conversion (Type Casting)
1️⃣ Implicit Type Conversion (Automatic Type Conversion)
Done automatically by the compiler.
Occurs when a smaller data type is converted into a larger data type to prevent
data loss.
No need for manual intervention.
#include <stdio.h>
int main() {
int a = 10;
float b = a; // int is automatically converted to float Output:
printf("Value of b: %f\n", b); Value of b: 10.000000
return 0;
}
2️⃣ Explicit Type Conversion (Type Casting)
Manually converting one data type into another using type casting.
(data_type) value;
#include <stdio.h>
Output:
int main() {
float num = 10.75; Value after type casting: 10
int result = (int) num; // Type casting float to int
printf("Value after type casting: %d\n", result);
return 0;
}
Topic 3: Conditional Branching (if-else & switch-case) in C
✅ What is Conditional Branching?
Conditional branching in C allows us to execute different blocks of code based on
conditions.
We use:
1️⃣ if statement
2️⃣ if-else statement
3️⃣ Nested if-else
4️⃣ if-else-if ladder
5️⃣ switch statement
1️⃣ if Statement
Used to execute a block of code only if a condition is true.
Syntax:
if (condition) {
// Code executes if condition is true
}
#include <stdio.h> Output:
int main() {
int num = 10; The number is positive.
if (num > 0) { // Condition is true
printf("The number is positive.\n");
}
return 0;
}
2️⃣ if-else Statement
Used when we have two possible outcomes.
Syntax:
if (condition) {
// Code executes if condition is true
} else {
// Code executes if condition is false
}
#include <stdio.h> Output:
int main() {
The number is negative.
int num = -5;
if (num > 0) {
printf("The number is positive.\n");
} else {
printf("The number is negative.\n");
}
return 0;
}
3️⃣ Nested if-else Statement
We can write if-else statements inside another if-else for multiple conditions.
Syntax:
if (condition1) {
if (condition2) {
// Code executes if both conditions are true
} else {
// Code executes if condition1 is true but condition2 is false
}
} else {
// Code executes if condition1 is false
}
Output:
#include <stdio.h>
int main() { The number is zero.
int num = 0;
if (num > 0) {
printf("The number is positive.\n");
} else {
if (num < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
}
return 0;
}
4️⃣ if-else-if Ladder
Used when we have multiple conditions to check.
Syntax:
if (condition1) {
// Executes if condition1 is true
} else if (condition2) {
// Executes if condition2 is true
} else if (condition3) {
// Executes if condition3 is true
} else {
// Executes if none of the above conditions are true
}
#include <stdio.h>
int main() {
int M;
Output:
// Input marks
Enter your marks: 85
printf("Enter obtained marks (out of 100): ");
Grade: A
scanf("%d", &M);
Enter your marks: 45
Grade: F (Fail)
// Determine and print the grade
if (M > 90 && M <= 100) {
printf("Grade: A+\n");
} else if (M > 80 && M <= 90) {
printf("Grade: A\n");
} else if (M > 70 && M <= 80) {
printf("Grade: B+\n");
} else if (M > 60 && M <= 70) {
printf("Grade: B\n");
} else if (M > 50 && M <= 60) {
printf("Grade: C\n");
} else {
printf("Grade: F\n");
}
return 0;
}
1. User Input:
Takes starting and ending meter readings.
Calculates total units consumed as end_reading - start_reading.
2. Bill Calculation Using Slabs:
If units ≤ 200, charge Rs. 5.50 per unit.
If units between 200-400, base charge Rs. 700 + Rs. 6 per extra unit.
If units between 400-600, base charge Rs. 1400 + Rs. 7.50 per extra unit.
If units > 600, base charge Rs. 1850 + Rs. 9 per extra unit.
3. Final Output:
Displays total units consumed and final bill amount.
#include <stdio.h>
int main() {
int start_reading, end_reading, units;
float bill = 0.0;
// Taking user input for meter readings
printf("Enter starting meter reading: ");
scanf("%d", &start_reading);
printf("Enter ending meter reading: ");
scanf("%d", &end_reading);
// Calculating the total units consumed
units = end_reading - start_reading;
// Bill calculation based on slabs
if (units <= 200) {
bill = units * 5.50;
}
else if (units <= 400) {
bill = 700 + (units - 200) * 6.00;
}
else if (units <= 600) {
bill = 1400 + (units - 400) * 7.50;
}
else {
bill = 1850 + (units - 600) * 9.00;
}
// Displaying the final bill amount
printf("Total units consumed: %d\n", units);
printf("Total energy bill: Rs. %.2f\n", bill);
return 0;
}
Input
Enter starting meter reading: 1200
Enter ending meter reading: 1450
Output
Total units consumed: 250
Total energy bill: Rs. 1000.00
✅ Solution:
✔ a = -10, b = 20
✔ if (-10 > 0 && 20 < 0) → false
✔ else if (-10 < 0 && 20 < 0) → false
✔ else if (-10 < 0 && 20 > 0) → true → b-- (so b = 19)
✔ printf("%d\n", a + b); → -10 + 19 = 9
5️⃣ switch-case Statement
Used when we need to match a variable against multiple values.
Syntax:
switch (variable) {
case value1:
// Code for case 1
break;
case value2:
// Code for case 2
break;
default:
// Code if none of the cases match
}
#include <stdio.h>
int main() {
Output:
int day = 3;
Wednesday
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
default:
printf("Weekend\n");
}
return 0;
}
✔ break; is used to stop execution after a match is found.
✔ If break; is missing, all cases after the match will execute (fall-through).
✔ default: runs if no cases match.
#include <stdio.h> Output:
int main() { Enter a character: E
E is a vowel.
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
switch (ch) {
case 'a': case 'A':
case 'e': case 'E':
case 'i': case 'I':
case 'o': case 'O':
case 'u': case 'U':
printf("%c is a vowel.\n", ch);
break;
default:
printf("%c is a consonant.\n", ch);
}
return 0;
}