University of poonch
Rawalakot
Departement of cs&IT
ASSIGNMENT #2
C ourse title : Programming Fundamentals
S ubmitted by : Raza Ali,Abdul Moiz,Uzair Naseer
Roll No : 3 , 2 , 44
st
Class : BSCS(1 )
Submitted to : Mam Anum Naseer
Date of submission : 3 ,March, 2025
Code for problem 1:
#include <iostream>
#include <cmath>
using namespace std;
long long factorial(int n) {
if (n < 0)
return -1;
long long result = 1;
for (int i = 1; i <= n; i++)
result *= i;
return result;
}
int main() {
int operation;
double operand1 = 0, operand2 = 0, result = 0;
bool valid = true;
cout << "--------------------------------------------------------" <<
endl;
cout << "| C++ Calculator |" <<
endl;
cout << "--------------------------------------------------------" <<
endl;
cout << "| Select Operation: |" <<
endl;
cout << "--------------------------------------------------------" <<
endl;
cout << "| 1. Addition (+) | 2. Subtraction (-) |" <<
endl;
cout << "| 3. Multiplication (*) | 4. Division (/) |" <<
endl;
cout << "| 5. Modulus (%) | 6. Exponentiation (^) |" <<
endl;
cout << "| 7. Factorial (!) | 8. Square Root (sqrt) |" <<
endl;
cout << "| 9. Logarithm (log) | 10. Sine (sin) |" <<
endl;
cout << "--------------------------------------------------------" <<
endl;
cout << "Enter the operation number (1-10): ";
cin >> operation;
if ([Link]() || operation < 1 || operation > 10) {
cerr << "Invalid operation selection." << endl;
return 1;
}
[Link]();
if (operation >= 1 && operation <= 6) {
cout << "Enter Operand 1: ";
cin >> operand1;
if ([Link]()) {
cerr << "invalid operand." << endl;
return 1;
}
[Link]();
cout << "Enter Operand 2: ";
cin >> operand2;
if ([Link]()) {
cerr << "invalid operand." << endl;
return 1;
}
[Link]();
}
else {
cout << "Enter your number: ";
cin >> operand1;
if ([Link]()) {
cerr << "invalid operand." << endl;
return 1;
}
[Link]();
}
if (operation == 1) {
result = operand1 + operand2;
}
else if (operation == 2) {
result = operand1 - operand2;
}
else if (operation == 3) {
result = operand1 * operand2;
}
else if (operation == 4) {
if (operand2 == 0) {
cerr << "Error: Division by zero!" << endl;
valid = false;
}
else {
result = operand1 / operand2;
}
}
else if (operation == 5) {
int op1 = static_cast<int>(operand1);
int op2 = static_cast<int>(operand2);
if (op2 == 0) {
cerr << "Error: Modulus by zero!" << endl;
valid = false;
}
else {
result = op1 % op2;
}
}
else if (operation == 6) {
result = pow(operand1, operand2);
}
else if (operation == 7) {
int n = static_cast<int>(operand1);
if (n < 0) {
cerr << "Error: Factorial of a negative number is not defined." <<
endl;
valid = false;
}
else {
long long fact = factorial(n);
result = fact;
}
}
else if (operation == 8) {
if (operand1 < 0) {
cerr << "Error: Square root of a negative number is not real." <<
endl;
valid = false;
}
else {
result = sqrt(operand1);
}
}
else if (operation == 9) {
if (operand1 <= 0) {
cerr << "Error: Logarithm of non-positive number is not defined."
<< endl;
valid = false;
}
else {
result = log(operand1);
}
}
else if (operation == 10) {
result = sin(operand1);
}
if (valid)
cout << "Result: " << result << endl;
return 0;
}
Output for problem 1:
Code for problem 2:
#include <iostream>
using namespace std;
int main() {
const double DOLLAR_RATE = 278.0;
const double EURO_RATE = 290.0;
cout << "----------- Restaurant Menu -----------\n";
cout << "1. Chicken Handi (1800 PKR per kg)\n";
cout << "2. Chicken Karahi (1500 PKR per kg)\n";
cout << "3. Chicken Tikka (2000 PKR per kg)\n";
cout << "4. Chicken Haleem (2500 PKR per kg)\n";
cout << "5. Creamy Chicken (1800 PKR per kg)\n";
cout << "--------------------------------------\n\n";
int code;
cout << "Enter the code of the dish you want to order (1-5): ";
cin >> code;
if (code < 1 || code > 5) {
cout << "Error: Invalid dish code! Program will terminate.\n";
return 1;
}
double pricePerKg;
if (code == 1)
pricePerKg = 1800.0;
else if (code == 2)
pricePerKg = 1500.0;
else if (code == 3)
pricePerKg = 2000.0;
else if (code == 4)
pricePerKg = 2500.0;
else
pricePerKg = 1800.0;
double quantity;
cout << "Enter the quantity in kilograms (e.g., 0.5, 1, 2, etc.): ";
cin >> quantity;
if (quantity <= 0) {
cout << "Error: Invalid quantity! Program will terminate.\n";
return 1;
}
double mealPrice = pricePerKg * quantity;
double taxRate;
if (mealPrice <= 1000)
taxRate = 0.02;
else if (mealPrice <= 3000)
taxRate = 0.05;
else
taxRate = 0.10;
double salesTax = mealPrice * taxRate;
double totalPricePKR = mealPrice + salesTax;
int currencyChoice;
cout << "\nSelect payment currency:\n";
cout << "1. Pakistani Rupees (PKR)\n";
cout << "2. US Dollars (USD)\n";
cout << "3. Euros (EUR)\n";
cout << "Enter your choice (1-3): ";
cin >> currencyChoice;
if (currencyChoice < 1 || currencyChoice > 3) {
cout << "Error: Invalid currency choice! Program will terminate.\n";
return 1;
}
double finalPrice;
string currency;
if (currencyChoice == 1) {
finalPrice = totalPricePKR;
currency = "PKR";
}
else if (currencyChoice == 2) {
finalPrice = totalPricePKR / DOLLAR_RATE;
currency = "USD";
}
else {
finalPrice = totalPricePKR / EURO_RATE;
currency = "EUR0";
}
cout << "\n---------------- Bill Details ----------------------\n";
cout << "Meal Price (excluding tax) in PKR: " << mealPrice << "\n";
cout << "Sales Tax in PKR: " << salesTax << "\n";
cout << "Total Price in PKR (Meal + Tax): " << totalPricePKR << "\n";
cout << "Final Amount in " << currency << ": " << finalPrice << "\n";
cout << "----------------------------------------------------\n";
return 0;
}
Output for problem 2:
Code for problem 3:
#include <iostream>
using namespace std;
int main() {
string team1 = "Pakistan", team2 = "India", team3 = "Bangladesh";
string team4 = "New Zealand", team5 = "Sri Lanka", team6 = "West Indies";
int points1 = 99, points2 = 52, points3 = 80;
int points4 = 72, points5 = 45, points6 = 84;
string top1, top2, top3;
int max1, max2, max3;
if (points1 > points2 && points1 > points3 && points1 > points4 && points1
> points5 && points1 > points6) {
top1 = team1;
max1 = points1;
}
else if (points2 > points1 && points2 > points3 && points2 > points4 &&
points2 > points5 && points2 > points6) {
top1 = team2;
max1 = points2;
}
else if (points3 > points1 && points3 > points2 && points3 > points4 &&
points3 > points5 && points3 > points6) {
top1 = team3;
max1 = points3;
}
else if (points4 > points1 && points4 > points2 && points4 > points3 &&
points4 > points5 && points4 > points6) {
top1 = team4;
max1 = points4;
}
else if (points5 > points1 && points5 > points2 && points5 > points3 &&
points5 > points4 && points5 > points6) {
top1 = team5;
max1 = points5;
}
else {
top1 = team6;
max1 = points6;
}
if (points1 != max1 && points1 > points2 && points1 > points3 && points1 >
points4 && points1 > points5 && points1 > points6) {
top2 = team1;
max2 = points1;
}
else if (points2 != max1 && points2 > points1 && points2 > points3 &&
points2 > points4 && points2 > points5 && points2 > points6) {
top2 = team2;
max2 = points2;
}
else if (points3 != max1 && points3 > points1 && points3 > points2 &&
points3 > points4 && points3 > points5 && points3 > points6) {
top2 = team3;
max2 = points3;
}
else if (points4 != max1 && points4 > points1 && points4 > points2 &&
points4 > points3 && points4 > points5 && points4 > points6) {
top2 = team4;
max2 = points4;
}
else if (points5 != max1 && points5 > points1 && points5 > points2 &&
points5 > points3 && points5 > points4 && points5 > points6) {
top2 = team5;
max2 = points5;
}
else {
top2 = team6;
max2 = points6;
}
if (points1 != max1 && points1 != max2 && points1 > points2 && points1 >
points3 && points1 > points4 && points1 > points5 && points1 > points6) {
top3 = team1;
max3 = points1;
}
else if (points2 != max1 && points2 != max2 && points2 > points1 &&
points2 > points3 && points2 > points4 && points2 > points5 && points2 >
points6) {
top3 = team2;
max3 = points2;
}
else if (points3 != max1 && points3 != max2 && points3 > points1 &&
points3 > points2 && points3 > points4 && points3 > points5 && points3 >
points6) {
top3 = team3;
max3 = points3;
}
else if (points4 != max1 && points4 != max2 && points4 > points1 &&
points4 > points2 && points4 > points3 && points4 > points5 && points4 >
points6) {
top3 = team4;
max3 = points4;
}
else if (points5 != max1 && points5 != max2 && points5 > points1 &&
points5 > points2 && points5 > points3 && points5 > points4 && points5 >
points6) {
top3 = team5;
max3 = points5;
}
else {
top3 = team6;
max3 = points6;
}
cout << "Top 3 teams:\n";
cout << "1. " << top1 << " - " << max1 << " points\n";
cout << "2. " << top2 << " - " << max2 << " points\n";
cout << "3. " << top3 << " - " << max3 << " points\n";
return 0;
}
Output for problem 3:
Code for problem 4:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0));
char playAgain;
do {
int number = rand() % 100 + 1;
int guess;
int attempts = 0;
cout << "Guess the number (1-100):" << endl;
while (true) {
cin >> guess;
attempts++;
if (guess < number) {
cout << "Too low!" << endl;
}
else if (guess > number) {
cout << "Too high!" << endl;
}
else {
cout << "Correct! Attempts: " << attempts << endl;
break;
}
}
cout << "Play again? (y/n): ";
cin >> playAgain;
} while (playAgain == 'y' || playAgain == 'Y');
return 0;
}
Output for problem 4:
Code for problem 5:
#include <iostream>
using namespace std;
int main() {
double budgetGroceries, budgetUtilities, budgetEntertainment,
budgetMiscellaneous;
double spentGroceries, spentUtilities, spentEntertainment,
spentMiscellaneous;
cout << "Enter your budget for Groceries: $";
cin >> budgetGroceries;
cout << "Enter your budget for Utilities: $";
cin >> budgetUtilities;
cout << "Enter your budget for Entertainment: $";
cin >> budgetEntertainment;
cout << "Enter your budget for Miscellaneous: $";
cin >> budgetMiscellaneous;
cout << "Enter your actual expenses for Groceries: $";
cin >> spentGroceries;
cout << "Enter your actual expenses for Utilities: $";
cin >> spentUtilities;
cout << "Enter your actual expenses for Entertainment: $";
cin >> spentEntertainment;
cout << "Enter your actual expenses for Miscellaneous: $";
cin >> spentMiscellaneous;
cout << "\nBudget Summary:\n";
if (spentGroceries <= budgetGroceries) {
cout << "Groceries: Under budget by $" << budgetGroceries -
spentGroceries << endl;
}
else {
cout << "Groceries: Over budget by $" << spentGroceries -
budgetGroceries << endl;
}
if (spentUtilities <= budgetUtilities) {
cout << "Utilities: Under budget by $" << budgetUtilities -
spentUtilities << endl;
}
else {
cout << "Utilities: Over budget by $" << spentUtilities -
budgetUtilities << endl;
}
if (spentEntertainment <= budgetEntertainment) {
cout << "Entertainment: Under budget by $" << budgetEntertainment -
spentEntertainment << endl;
}
else {
cout << "Entertainment: Over budget by $" << spentEntertainment -
budgetEntertainment << endl;
}
if (spentMiscellaneous <= budgetMiscellaneous) {
cout << "Miscellaneous: Under budget by $" << budgetMiscellaneous -
spentMiscellaneous << endl;
}
else {
cout << "Miscellaneous: Over budget by $" << spentMiscellaneous -
budgetMiscellaneous << endl;
}
double totalBudget = budgetGroceries + budgetUtilities +
budgetEntertainment + budgetMiscellaneous;
double totalSpent = spentGroceries + spentUtilities + spentEntertainment +
spentMiscellaneous;
if (totalSpent <= totalBudget) {
cout << "\nOverall: You are within your total budget by $" <<
totalBudget - totalSpent << endl;
}
else {
cout << "\nOverall: You have overspent $" << totalSpent - totalBudget
<< endl;
}
return 0;
}
Output for problem 5:
Code for problem 6:
#include <iostream>
using namespace std;
int main() {
int missionChoice;
cout << "===== NASA Mission Planner =====\n";
cout << "Choose a mission:\n";
cout << "1. Moon Mission\n";
cout << "2. Mars Mission\n";
cout << "3. Jupiter Mission\n";
cout << "Enter your choice (1, 2, or 3): ";
cin >> missionChoice;
int astronauts;
double fuel;
double food;
int equipment;
int minAstronauts = 0;
double minFuel = 0.0;
double minFood = 0.0;
int minEquipment = 0;
double fuelFactor = 0.0;
double foodFactor = 0.0;
double equipmentFactor = 0.0;
double fuelDenominator = 1.0;
double foodDenominator = 1.0;
double equipmentDenominator = 1.0;
switch (missionChoice) {
case 1:
minAstronauts = 3;
minFuel = 500.0;
minFood = 100.0;
minEquipment = 3;
fuelFactor = 20.0;
foodFactor = 30.0;
equipmentFactor = 50.0;
fuelDenominator = 500.0;
foodDenominator = 100.0;
equipmentDenominator = 3.0;
break;
case 2:
minAstronauts = 5;
minFuel = 1000.0;
minFood = 300.0;
minEquipment = 10;
fuelFactor = 25.0;
foodFactor = 40.0;
equipmentFactor = 35.0;
fuelDenominator = 1000.0;
foodDenominator = 300.0;
equipmentDenominator = 10.0;
break;
case 3:
minAstronauts = 8;
minFuel = 3000.0;
minFood = 800.0;
minEquipment = 10;
fuelFactor = 30.0;
foodFactor = 40.0;
equipmentFactor = 30.0;
fuelDenominator = 3000.0;
foodDenominator = 800.0;
equipmentDenominator = 10.0;
break;
default:
cout << "Invalid choice. Program will exit.\n";
return 0;
}
cout << "\nEnter the number of astronauts: ";
cin >> astronauts;
cout << "Enter the amount of fuel (liters): ";
cin >> fuel;
cout << "Enter the amount of food (units): ";
cin >> food;
cout << "Enter the number of equipment kits: ";
cin >> equipment;
if (astronauts < minAstronauts || fuel < minFuel || food < minFood ||
equipment < minEquipment) {
cout << "\nMission not feasible: You do not meet the minimum
requirements.\n";
}
else {
double successProbability = 0.0;
successProbability += (fuel / fuelDenominator) * fuelFactor;
successProbability += (food / foodDenominator) * foodFactor;
successProbability += (equipment / equipmentDenominator) *
equipmentFactor;
cout << "\nResources are sufficient. Calculating success
probability...\n";
cout << "Success Probability : " << successProbability << "%\n ";
double minSuccess = 70.0;
if (successProbability >= minSuccess) {
cout << "Mission is likely to succeed!\n";
}
else {
cout << "Mission success probability is low. Proceed with
caution.\n";
}
}
cout << "\nThank you for using the NASA Mission Planner!\n";
return 0;
}
Output for problem 6:
Code for problem 7:
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a positive integer: ";
cin >> number;
int sumOfFactors = 0;
cout << "Input: " << number << endl;
cout << "Factors are: ";
for (int i = 1; i < number; i++) {
if (number % i == 0) {
cout << i << " ";
sumOfFactors += i;
}
}
cout << "\nSum is: " << sumOfFactors << endl;
if (sumOfFactors == number) {
cout << "Classification: Perfect" << endl;
}
else if (sumOfFactors > number) {
cout << "Classification: Abundant" << endl;
}
else {
cout << "Classification: Deficient" << endl;
}
return 0;
}
Output for problem 7:
Code for problem 8:
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter side 1:" << endl;
cin >> a;
cout << "Enter side 2:" << endl;
cin >> b;
cout << "Enter side 3:" << endl;
cin >> c;
int s1 = a, s2 = b, s3 = c;
if (s1 > s2) { int temp = s1; s1 = s2; s2 = temp; }
if (s2 > s3) { int temp = s2; s2 = s3; s3 = temp; }
if (s1 > s2) { int temp = s1; s1 = s2; s2 = temp; }
if (s1 + s2 <= s3) {
cout << "Not a triangle" << endl;
return 0;
}
if (s1 == s2 && s2 == s3)
cout << "Equilateral triangle" << endl;
else if (s1 * s1 + s2 * s2 == s3 * s3)
cout << "Right triangle" << endl;
else if (s1 == s2 || s2 == s3 || s1 == s3)
cout << "Isosceles triangle" << endl;
else
cout << "Regular triangle" << endl;
return 0;
}
Output for problem 8:
Code for problem 9:
//RAZA Ali,Abdul Moiz,Uzair Naseer
//problem No 9
#include <iostream>
using namespace std;
bool isPalindrome(int num) {
int original = num;
int reversed = 0;
while (num > 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num = num / 10;
}
return (original == reversed);
}
int main() {
int x, y;
cout << "Lower bound?" << endl;
cin >> x;
cout << "Upper bound?" << endl;
cin >> y;
if (x > y) {
int temp = x;
x = y;
y = temp;
}
int countPalindromes = 0;
for (int i = x; i <= y; i++) {
if (i >= 0 && isPalindrome(i)) {
countPalindromes++;
}
}
cout << "The number of palindromes is " << countPalindromes << endl;
return 0;
}
Output for Problem 9:
Code for problem 10:
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number that has minimum of two digits and at most of nine
digits : ";
cin >> number;
if (number < 10 || number > 999999999) {
cout << "Invalid input! The number must have between 2 and 9 digits."
<< endl;
return 1;
}
int temp = number;
int digits = 0;
while (temp > 0) {
digits++;
temp /= 10;
}
bool isMagic = true;
int divider = 1;
for (int i = 1; i <= digits; i++) {
divider *= 10;
int segment = number % divider;
if (segment % i != 0) {
isMagic = false;
break;
}
}
if (isMagic) {
cout << number << " is a magic number!" << endl;
}
else {
cout << number << " is not a magic number" << endl;
}
return 0;
}
Output for Problem 10:
Code for problem 11:
#include <iostream>
using namespace std;
int countEvenDivisors(int num) {
int count = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0 && i % 2 == 0) {
count++;
}
}
return count;
}
int main() {
int X, Y, N;
cout << "Enter value for X: ";
cin >> X;
cout << "Enter value for Y: ";
cin >> Y;
cout << "Enter value for N: ";
cin >> N;
if (X >= Y) {
cout << "Error: X must be less than Y." << endl;
return 1;
}
cout << "Numbers between " << X << " and " << Y << " with exactly " << N
<< " even divisors are: \n";
bool found = false;
for (int num = X; num <= Y; num++) {
int evenDivisors = countEvenDivisors(num);
if (evenDivisors == N) {
cout << num << endl;
found = true;
}
}
if (!found) {
cout << "None";
}
cout << endl;
return 0;
}
Output for problem 11:
Code for Problem 12:
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
int reverseNumber(int num) {
int reversed = 0;
while (num > 0) {
reversed = reversed * 10 + (num % 10);
num /= 10;
}
return reversed;
}
bool isPalindrome(int num) {
return num == reverseNumber(num);
}
int main() {
int X, Y;
cout << "Enter X: ";
cin >> X;
cout << "Enter Y: ";
cin >> Y ;
cout << "\n\nTHese numbers and their Palindrome are Prime in the given
range " << endl;
for (int i = X; i <= Y; i++) {
if (isPrime(i) && isPalindrome(i) && isPrime(reverseNumber(i))) {
cout << i << " ";
}
}
return 0;
}
Output for problem 12
Code for problem 13:
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int k = 1; k <= 2 * i - 1; k++) {
cout << "* ";
}
cout << endl;
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int k = 1; k <= 2 * i - 1; k++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Output for problem 13:
==========================================
The End