Assignment! chap4
Assignment! chap4
Subject: Computer Science Class: XII (Computer) Developed by: Muhammad Farhan Ramzan
Worksheet PBA Chapter # 4
1. 90-100: A+
2. 80-89: A
3. 70-79: B
4. 60-69: C
5. 50-59: D
6. Below 50: Fail
5. Write a program to calculate the electricity bill based on the following conditions:
Switch-Case Applications
1. Write a program to take a number (1-7) as input and print the corresponding day of the week.
2. Write a program to take a character input (+, -, *, /) and perform the corresponding arithmetic operation on two
numbers.
3. Write a program to check whether a given month number (1-12) has 30 or 31 days using a switch statement.
4. Write a program to take a user’s choice (1-3) and print a message accordingly (e.g., 1 → Start, 2 → Stop, 3 →
Exit).
5. Write a program to take a grade (A, B, C, D, F) and display the corresponding GPA value using switch.
1. Write a program to take three sides of a triangle and check if it is an equilateral, isosceles, or scalene triangle.
2. Write a program to check whether a given number is a three-digit number or not.
3. Write a program to calculate the roots of a quadratic equation using the quadratic formula.
4. Write a program to take a 4-digit number and check if it is a palindrome.
5. Write a program to take an employee's salary and apply a tax deduction based on the following:
Less than 30,000: No tax
30,000 - 50,000: 5% tax
50,001 - 100,000: 10% tax
Above 100,000: 15% tax
1. Write a program to determine if a shop offers a discount based on the total purchase amount.
2. Write a program to take the age of a person and determine if they are eligible to vote (age ≥ 18).
3. Write a program to take the temperature in Celsius and classify it as cold, warm, or hot.
4. Write a program to determine if a person is eligible for a driving license based on their age.
5. Write a program to categorize a given number as a single-digit, two-digit, or three-digit number.
6. Write a program to determine if a person is underweight, normal, or overweight based on BMI.
7. Write a program to take the time in 24-hour format and determine if it is morning, afternoon, evening, or night.
8. Write a program to calculate bonus salary for employees based on years of service.
9. Write a program to take a product code and determine the department it belongs to (e.g., 101-200 → Electronics, 201-
300 → Clothing).
10. Write a program to determine the ticket price for a movie based on age (child, adult, senior citiz
Write a program to print the first 10 Fibonacci numbers using a for loop.
Write a program to find the greatest common divisor (GCD) of two numbers using a while loop.
Write a program to print the pattern below using nested for loops:
1
12
123
1234
Write a program to calculate the power of a number (base^exponent) using a for loop.
Write a program to find the sum of the digits of a number using a do-while loop.
Write a program to determine if a number is prime using a for loop.
Write a program to print numbers from 100 to 1 in reverse order using a while loop.
Write a program to print the ASCII values of characters from A to Z using a for loop.
Write a program to find the average of N numbers entered by the user using a while loop.
Write a program to display the following pattern using nested for loops:
* ****
* ***
* **
* *
*
Write a program to count how many times a specific digit appears in a number using a while loop.
Write a program to calculate the sum of the first N natural numbers using a do-while loop
2. Find the Output
int y = 10;if (y % 2 == 0) {
cout << "Even\n";
cout << "Number";
} else {
cout << "Odd";
}
int s = 12;
switch(s/4) {
case 2:
cout << "Two\n";
break;
case 3:
cout << "Three\n";
break;
default:
cout << "Other\n";
}
int v = 20;do {
if (v > 15) {
cout << v << " ";
}
v -= 5;
} while (v > 0);cout << endl;
int n1 = 9, n2 = 3;
cout << (n1 / n2 == 3 ? "Equal\nValues\n" : "Not\nEqual\n");
char ch = 'a';if (ch >= 'a' && ch <= 'z') {
cout << "Lowercase\nChar\n";
}
int x = 2;do {
cout << x << " ";
x *= 2;
} while(x <= 8);
int n = 3;do {
cout << n * n << " ";
n--;
} while(n >= 1);
int x = 5;
if (x = 10) {
cout << "A";
}
int s = 12;switch(s/4) {
case 2:
cout << "Two";
case 3:
cout << "Three";
default:
cout << "Other";
}
int v = 20;do {
if (v > 15)
cout << v << " ";
v -= 5;
} while (v > 0);
int n = 5;do
cout << n;while(n > 0)
int x = 3;do {
cout << x;
x--;
} while x > 0;
1`
4. Differences Between For, While, and Do-While Loops (With
Conceptual Focus)
Feature For Loop While Loop Do-While Loop
Repeats a block for a
Repeats as long as a Executes first, checks the
Definition known number of
condition is true condition later
times
Waiting for a
Best Use Counting, fixed Menus, input validation
condition to become
Case iterations (runs at least once)
false
Condition
Before loop starts Before each iteration After each iteration
Check
Guarantee of May not run if the May not run if the
Always runs at least once
Execution condition is false condition is false
Infinite loops if Logic errors if not
Common Forgetting to update
condition never considering the first
Mistake the loop variable
changes execution