Qno2: Use If-Else to write C++ code for the following
problem.
Loan Approval System
#include <iostream>
using namespace std;
int main()
double salary;
int creditScore;
// Taking input from user
cout << "Enter your salary: ";
cin >> salary;
cout << "Enter your credit score: ";
cin >> creditScore;
// Checking loan approval conditions
if (salary > 50000 && creditScore > 700) {
cout << "Congratulations! Your loan is approved." << endl;
} else {
cout << "Sorry, your loan is rejected due to insufficient salary or low
credit score." << endl;
return 0;
}
Online Shopping Discount
#include <iostream>
using namespace std;
int main() {
double totalAmount, discount, finalPrice;
// Taking input from user
cout << "Enter your total purchase amount (in PKR): ";
cin >> totalAmount;
// Applying discount based on total purchase amount
if (totalAmount > 10000) {
discount = 0.20 * totalAmount; // 20% discount
} else {
discount = 0.05 * totalAmount; // 5% discount
// Calculating final price
finalPrice = totalAmount - discount;
// Displaying the final price after discount
cout << "Your total discount is: " << discount << " PKR" << endl;
cout << "Final price after discount: " << finalPrice << " PKR" << endl;
return 0;
}
Weather Suggestion System
#include <iostream>
using namespace std;
int main() {
double temperature;
// Taking temperature input from the user
cout << "Enter the temperature (°C): ";
cin >> temperature;
// Suggesting clothing based on temperature
if (temperature > 30) {
cout << "Wear light clothes." << endl;
} else if (temperature >= 20 && temperature <= 30) {
cout << "Wear normal clothes." << endl;
} else {
cout << "Wear warm clothes." << endl;
return 0;
}
Bus Fare Calculation
#include <iostream>
using namespace std;
int main() {
int age;
int fare;
// Taking age input from user
cout << "Enter your age: ";
cin >> age;
// Determining bus fare based on age
if (age < 5) {
fare = 0; // Free for children below 5 years
} else if (age >= 5 && age <= 12) {
fare = 50; // 50 PKR for ages 5-12
} else if (age >= 13 && age <= 60) {
fare = 100; // 100 PKR for ages 13-60
} else {
fare = 70; // 70 PKR for above 60 years
// Displaying the bus fare
cout << "Your bus fare is: " << fare << " PKR" << endl;
return 0;
}
Qno:03 Use Nested if to write C++ code for the
following problem.
Password Strength Checker
#include <iostream>
#include <cctype> // For isupper() and isdigit()
using namespace std;
int main() {
string password;
bool hasUpper = false, hasDigit = false;
// Taking password input
cout << "Enter your password: ";
cin >> password;
// Checking password length
if (password.length() >= 8) {
// Checking for at least one uppercase letter and one digit
for (char ch : password) {
if (isupper(ch)) {
hasUpper = true;
if (isdigit(ch)) {
hasDigit = true;
// Checking if both conditions are met
if (hasUpper) {
if (hasDigit) {
cout << "Strong Password" << endl;
} else {
cout << "Weak Password (Must contain at least one digit)" <<
endl;
} else {
cout << "Weak Password (Must contain at least one uppercase
letter)" << endl;
} else {
cout << "Weak Password (Must be at least 8 characters long)" << endl;
return 0;
}
Scholarship Eligibility
#include <iostream>
using namespace std;
int main() {
double marks, income;
// Taking input from the user
cout << "Enter your marks (in %): ";
cin >> marks;
cout << "Enter your family income (in PKR): ";
cin >> income;
// Checking scholarship eligibility
if (marks >= 90) {
if (income < 50000) {
cout << "Congratulations! You are awarded a 100% scholarship." <<
endl;
} else {
cout << "Sorry, you are not eligible for a scholarship." << endl;
} else if (marks >= 85) {
if (income < 100000) {
cout << "Congratulations! You are awarded a 50% scholarship." <<
endl;
} else {
cout << "Sorry, you are not eligible for a scholarship." << endl;
} else {
cout << "Sorry, you are not eligible for a scholarship." << endl;
return 0;
}
Library Fine System
#include <iostream>
using namespace std;
int main() {
int overdueDays;
int fine = 0;
// Taking input from the user
cout << "Enter the number of days the book is overdue: ";
cin >> overdueDays;
// Fine calculation based on overdue days
if (overdueDays >= 1 && overdueDays <= 5) {
fine = overdueDays * 10; // 10 PKR per day
else if (overdueDays >= 6 && overdueDays <= 10) {
fine = overdueDays * 20; // 20 PKR per day
else if (overdueDays > 10) {
fine = overdueDays * 50; // 50 PKR per day
cout << "Your membership has been suspended due to excessive
overdue days." << endl;
// Displaying the fine amount
cout << "Total fine: " << fine << " PKR" << endl;
return 0;
}
Qno4: Use Switch Statement to write C++ code for the
following problem.
#include <iostream>
using namespace std;
int main() {
int choice;
// Displaying the menu
cout << "Welcome to Our Restaurant!" << endl;
cout << "Menu:" << endl;
cout << "1. Burger - 300 PKR" << endl;
cout << "2. Pizza - 500 PKR" << endl;
cout << "3. Pasta - 400 PKR" << endl;
cout << "4. Biryani - 250 PKR" << endl;
cout << "5. Exit" << endl;
// Taking user choice
cout << "Enter your choice (1-5): ";
cin >> choice;
// Processing user selection
switch (choice) {
case 1:
cout << "You selected Burger. Price: 300 PKR" << endl;
break;
case 2:
cout << "You selected Pizza. Price: 500 PKR" << endl;
break;
case 3:
cout << "You selected Pasta. Price: 400 PKR" << endl;
break;
case 4:
cout << "You selected Biryani. Price: 250 PKR" << endl;
break;
case 5:
cout << "Exiting... Thank you for visiting!" << endl;
break;
default:
cout << "Invalid choice! Please select a valid option (1-5)." << endl;
return 0;
}
Calculator
#include <iostream>
using namespace std;
int main() {
double num1, num2;
char op;
// Taking input from the user
cout << "Enter first number: ";
cin >> num1;
cout << "Enter an operator (+, -, *, /, %): ";
cin >> op;
cout << "Enter second number: ";
cin >> num2;
// Performing the operation using switch
switch (op) {
case '+':
cout << "Result: " << num1 + num2 << endl;
break;
case '-':
cout << "Result: " << num1 - num2 << endl;
break;
case '*':
cout << "Result: " << num1 * num2 << endl;
break;
case '/':
if (num2 != 0)
cout << "Result: " << num1 / num2 << endl;
else
cout << "Error! Division by zero is not allowed." << endl;
break;
case '%':
if (static_cast<int>(num2) != 0)
cout << "Result: " << static_cast<int>(num1) %
static_cast<int>(num2) << endl;
else
cout << "Error! Modulus by zero is not allowed." << endl;
break;
default:
cout << "Invalid operator! Please enter a valid operator (+, -, *, /,
%)." << endl;
return 0;
}
Car Gear System
#include <iostream>
using namespace std;
int main() {
int gear;
// Taking user input for gear selection
cout << "Enter gear number (0-5): ";
cin >> gear;
// Gear selection using switch
switch (gear) {
case 0:
cout << "Car in Neutral" << endl;
break;
case 1:
cout << "First Gear" << endl;
break;
case 2:
cout << "Second Gear" << endl;
break;
case 3:
cout << "Third Gear" << endl;
break;
case 4:
cout << "Fourth Gear" << endl;
break;
case 5:
cout << "Fifth Gear" << endl;
break;
default:
cout << "Invalid gear! Please enter a number between 0 and 5." <<
endl;
return 0;
}
Qno5: Use For loop to write C++ code for the following problem.
Sum of Even Numbers
#include <iostream>
using namespace std;
int main() {
int sum = 0;
// Loop to find the sum of even numbers from 2 to 20
for (int i = 2; i <= 20; i += 2) {
sum += i;
// Display the result
cout << "The sum of even numbers between 2 and 20 is: " << sum <<
endl;
return 0;
}
Star Pyramid Pattern
#include <iostream>
using namespace std;
int main() {
int rows;
// Taking input from the user
cout << "Enter the number of rows: ";
cin >> rows;
// Loop for printing the pyramid
for (int i = 1; i <= rows; i++) {
// Print spaces for alignment
for (int j = 1; j <= rows - i; j++) {
cout << " ";
// Print stars
for (int k = 1; k <= (2 * i - 1); k++) {
cout << "*";
cout << endl; // Move to next line
return 0;
}
Fibonacci Series
#include <iostream>
using namespace std;
int main() {
int n = 20; // Number of terms
long long first = 0, second = 1, next;
cout << "First 20 terms of the Fibonacci series:" << endl;
// Print first two terms
cout << first << ", " << second;
// Loop to generate next terms
for (int i = 3; i <= n; i++) {
next = first + second; // Compute next term
cout << ", " << next;
first = second; // Update values for next iteration
second = next;
cout << endl; // Move to new line
return 0;