0% found this document useful (0 votes)
12 views10 pages

Assignment! chap4

Uploaded by

fehminat19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

Assignment! chap4

Uploaded by

fehminat19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

ARMY PUBLIC SCHOOL AND COLLEGE, NOOR MAHAL CAMPUS BAHAWALPUR

SYLLABUS BREAKUP PUNJAB HSSC II (SESSION 2024-2025)

Subject: Computer Science Class: XII (Computer) Developed by: Muhammad Farhan Ramzan
Worksheet PBA Chapter # 4

Write C++ programs for the following problems:

Basic If-Else Conditions

1. Write a program to check whether a number is positive, negative, or zero.


2. Write a program to check whether a number is even or odd using an if-else statement.
3. Write a program to take three numbers as input and find the largest among them.
4. Write a program to check whether a given character is a vowel or a consonant.
5. Write a program to check whether a number is divisible by both 5 and 11.

Nested If & Multiple Conditions

1. Write a program to check whether a given year is a leap year or not.


2. Write a program to determine whether a triangle is valid based on its three angles.
3. Write a program to check whether a given character is uppercase, lowercase, a digit, or a special symbol.
4. Write a program to take a student’s marks in five subjects and determine their grade using the following criteria:

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:

 Up to 100 units: Rs. 5 per unit


 101-300 units: Rs. 7 per unit
 Above 300 units: Rs. 10 per unit

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.

Advanced If-Else & Nested If Applications

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

Practical Applications of Selection Structures

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

1. Programming Practice Questions


1) Write a program to print all odd numbers between 1 and 50 using a for loop.
2) Write a program to calculate the factorial of a number using a while loop.
3) Write a program to reverse a given number using a do-while loop.
4) Write a program to print the sum of all even numbers from 1 to 100 using a for loop.
5) Write a program to count the number of digits in a given number using a while loop.
6) Write a program to display a multiplication table from 1 to 10 using nested for loops.
Write a program that keeps accepting numbers from the user until they enter zero using a do-while loop.

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

1: Find the Output (20 Questions)

int x = 5;if (x > 3) {


cout << "A";
cout << "B";
} else {
cout << "C";
}

int y = 10;if (y % 2 == 0) {
cout << "Even\n";
cout << "Number";
} else {
cout << "Odd";
}

int z = 15;cout << (z > 10 ? "Big\nValue" : "Small");

int age = 16;if (age >= 18) {


cout << "Adult\n";
}cout << "Not Adult"; // This will always print

char grade = 'B';switch (grade) {


case 'A':
cout << "Excellent\n";
break;
case 'B':
cout << "Good\n";
break;
default:
cout << "Invalid\n";
}

int num = 7;if (num > 5 && num < 10) {


cout << "In\n";
cout << "Range\n";
}

int a = 2, b = 5;if (a > b || a < 0) {


cout << "True\n";
} else {
cout << "False\n";
}

int p = 8;if (! (p > 10)) {


cout << "Not\n";
cout << "Greater\n";
}

int q = 3;if (q > 0) {


cout << "Positive ";
if (q % 2 != 0) {
cout << "Odd\n";
}
}

int r = -4;if (r > 0) {


cout << "Positive\n";
} else if (r < 0) {
cout << "Negative\n";
} else {
cout << "Zero\n";
}

int s = 12;
switch(s/4) {
case 2:
cout << "Two\n";
break;
case 3:
cout << "Three\n";
break;
default:
cout << "Other\n";
}

int t = 5;for (int i = 0; i < 3; i++) {


if (t % 2 != 0) {
cout << t << " ";
}
t++;
}cout << endl; // Add newline for clarity

int u = 10;while (u > 5) {


if (u % 2 == 0) {
cout << u << " ";
}
u--;
}cout << endl;

int v = 20;do {
if (v > 15) {
cout << v << " ";
}
v -= 5;
} while (v > 0);cout << endl;

int w = 7;cout << (w % 3 == 0 ? "Divisible\n" : "Not\nDivisible\n");

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";
}

float f = 3.14;if (f > 3.0) {


cout << "Pi\nValue\n";
}

bool flag = true;if (flag) {


cout << "Yes\nIndeed\n";
}

int num_arr[] = {1, 2, 3};if (num_arr[1] == 2) {


cout << "Two\nHere\n";
}
for(int i = 1; i <= 3; i++) {
cout << i * i << " ";
}

int a = 5;while(a > 0) {


cout << a << " ";
a -= 2;
}

int x = 2;do {
cout << x << " ";
x *= 2;
} while(x <= 8);

for(int i = 0; i < 4; i++) {


for(int j = 0; j <= i; j++) {
cout << "*";
}
cout << endl;
}

int i = 1;while(i < 5) {


cout << i + 1 << " ";
i += 2;
}

int sum = 0;for(int i = 1; i <= 5; i++) {


sum += i;
}
cout << sum;

int num = 4;do {


cout << num << " ";
num--;
} while(num > 0);

for(int i = 1; i <= 3; i++) {


cout << i << " ";
if(i == 2)
break;
}

int count = 0;while(count < 3) {


cout << count << " ";
count++;
}

int n = 3;do {
cout << n * n << " ";
n--;
} while(n >= 1);

3. Find the Error

int x = 5;
if (x = 10) {
cout << "A";
}

int y = 10;if (y % 2 == 0); {


cout << "Even";
}

int z = 15;cout << (z > 10 ? "Big" : "Small")

int age = 16;if age >= 18 {


cout << "Adult";
}

char grade = 'B';switch (grade) {


case 'A':
cout << "Excellent";
case 'B':
cout << "Good";
default:
cout << "Invalid";
}

int num = 7;if (num > 5 and num < 10) {


cout << "In Range";
}

int a = 2, b = 5;if (a > b or a < 0) {


cout << "True";
}

int p = 8;if ! (p > 10) {


cout << "Not Greater";
}

int q = 3;if (q > 0) {


cout << "Positive ";
if (q % 2 != 0) {
cout << "Odd"
}
}

int r = -4;if (r > 0) {


cout << "Positive";
} else if (r < 0) {
cout << "Negative";
} else {
cout << "Zero";
}

int s = 12;switch(s/4) {
case 2:
cout << "Two";
case 3:
cout << "Three";
default:
cout << "Other";
}

int t = 5;for (int i = 0; i < 3; i++)


if (t % 2 != 0)
cout << t << " ";
t++;

int u = 10;while (u > 5) {


if (u % 2 == 0)
cout << u << " ";
u--;
}

int v = 20;do {
if (v > 15)
cout << v << " ";
v -= 5;
} while (v > 0);

int w = 7;cout << (w % 3 = 0 ? "Divisible" : "Not Divisible");

int n1 = 9, n2 = 3;cout << (n1 / n2 = 3 ? "Equal" : "Not Equal");


char ch = 'a';if (ch >= 'a' and ch <= 'z')

float f = 3.14;if (f > 3.0);


cout << "Pi";

bool flag = true;if (Flag)


cout << "Yes";

int num_arr[] = {1, 2, 3};if (num_arr[1] = 2)


cout << "Two";

for(int i = 0; i < 5; i++)


cout << i << endl

int a = 10;while(a > 0)


cout << a;
a--;

int n = 5;do
cout << n;while(n > 0)

for(int i = 1; i <= 5; i++) {


cout << i << " ";
}
cout << i;

int count = 0;while(count < 5) {


cout << count;
count++;

int x = 3;do {
cout << x;
x--;
} while x > 0;

for(int i = 0; i < 3; i++)


cout << i;
cout << "Done";

int a = 2;while(a < 5) {


cout << a;
a = a + 1;
}
cout << a;

int i = 0;for(; i < 5; i++)


cout << i;
i++;
cout << i;

int num = 4;do {


cout << num;
num--;
} until(num == 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

You might also like