KEMBAR78
Oop Assignment 2 | PDF | Integer (Computer Science) | Namespace
0% found this document useful (0 votes)
5 views15 pages

Oop Assignment 2

The document outlines a C++ coursework assignment consisting of 25 practical programming tasks. Each task requires writing a program to solve a specific problem, such as determining if a number is positive or negative, checking for leap years, or simulating an ATM. The tasks cover various programming concepts including conditionals, loops, and basic arithmetic operations.

Uploaded by

ywkvtkf9jx
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)
5 views15 pages

Oop Assignment 2

The document outlines a C++ coursework assignment consisting of 25 practical programming tasks. Each task requires writing a program to solve a specific problem, such as determining if a number is positive or negative, checking for leap years, or simulating an ATM. The tasks cover various programming concepts including conditionals, loops, and basic arithmetic operations.

Uploaded by

ywkvtkf9jx
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/ 15

C++ Take-home Practical Coursework 1

1. Write a program that takes an integer and prints whether it's positive, negative,
or zero.

#include <iostream>
using namespace std;
int main () {
int num;
cout << "Enter an integer: ";
cin >> num;
if (num > 0)
cout << "Positive\n";
else if (num < 0)
cout << "Negative\n";
else cout << "Zero\n";
}

2. Check if a number is even or odd.

#include <iostream>
using namespace std;
int main () {
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 2 == 0) cout << "Even\n";
else cout << "Odd\n";
}
3. Input two numbers and print the larger one.

#include <iostream>
using namespace std;
int main () {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
if (a > b) cout << "Largest: " << a << endl;
else if (b > a) cout << "Largest: " << b << endl;
else cout << "Both are equal\n";
}

4. Determine if a given year is a leap year (divisible by 4, but not by 100 unless also
by 400).

#include <iostream>
using namespace std;
int main () {
int year;
cout << "Enter a year: ";
cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
cout << year << " is a Leap Year\n";
else cout << year << " is NOT a Leap Year\n";
}
5. Input a character and check if it's a vowel (a, e, i, o, u) or consonant.

#include <iostream>
using namespace std;
int main () {
char ch;
cout << "Enter a character: ";
cin >> ch;
ch = tolower(ch);
if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
cout << "Vowel\n";
else if (isalpha(ch))
cout << "Consonant\n";
else cout << "Not a letter\n";
}

6. Print the absolute value of a number without using abs ().

#include <iostream>
using namespace std;
int main () {
int num;
cout << "Enter a number: ";
cin >> num;
if (num < 0) num = -num;
cout << "Absolute value: " << num << endl;
}
7. Input a student's marks (0-100) and print "Pass" if ≥50, else "Fail".

#include <iostream>
using namespace std;
int main () {
int marks;
cout << "Enter marks (0-100): ";
cin >> marks;
if (marks >= 50) cout << "Pass\n";
else cout << "Fail\n";
}

8. Assign grades (A, B+, B, C+, C, D, F) based on marks (80+, 70+, 60+, 55+, 50+,
40+, 0+).

#include <iostream>
using namespace std;
int main () {
int marks;
cout << "Enter marks (0-100): ";
cin >> marks;
if (marks >= 80) cout << "Grade: A\n";
else if (marks >= 70) cout << "Grade: B+\n";
else if (marks >= 60) cout << "Grade: B\n";
else if (marks >= 55) cout << "Grade: C+\n";
else if (marks >= 50) cout << "Grade: C\n";
else if (marks >= 40) cout << "Grade: D\n";
else cout << "Grade: F\n";
}

9. Input three sides and check if they form a valid triangle (sum of any two > third).
#include <iostream>
using namespace std;
int main () {
int a, b, c;
cout << "Enter 3 sides of triangle: ";
cin >> a >> b >> c;
if ((a+b>c) && (a+c>b) && (b+c>a))
cout << "Valid Triangle\n";
else
cout << "Not a Valid Triangle\n";
}

10. Compute roots of ax? + bx + c = 0 (handle real and imaginary cases).

#include <iostream>
using namespace std;
int main () {
double a, b, c, d;
cout << "Enter coefficients a, b, c: ";
cin >> a >> b >> c;
d = b*b - 4*a*c;
if (d > 0) {
double r1 = (-b + sqrt(d)) / (2*a);
double r2 = (-b - sqrt(d)) / (2*a);
cout << "Two Real Roots: " << r1 << " and " << r2 << endl;
}
else if (d == 0) {
double r = -b / (2*a);
cout << "One Real Root: " << r << endl;
}

else {
double real = -b / (2*a);
double imag = sqrt(-d) / (2*a);
cout << "Complex Roots: " << real << " + " << imag << "i and "
<< real << " - " << imag << "i\n";
}
}

11. Build a simple calculator (+, -*, /) using switch-case.

#include <iostream>
using namespace std;
int main () {
double a, b;
char op;
cout << "Enter expression (a op b): ";
cin >> a >> op >> b;
switch(op) {
case '+': cout << "Result: " << a+b << endl; break;
case '-': cout << "Result: " << a-b << endl; break;
case '*': cout << "Result: " << a*b << endl; break;
case '/':
if (b!=0) cout << "Result: " << a/b << endl;
else cout << "Error: Division by zero\n";
break;
default: cout << "Invalid operator\n";
}
}

12. Input a number (1-7) and print the corresponding day (Sunday-Saturday).
#include <iostream>
using namespace std;
int main () {
int d;
cout << "Enter day number (1-7): ";
cin >> d;
switch(d) {
case 1: cout << "Sunday\n"; break;
case 2: cout << "Monday\n"; break;
case 3: cout << "Tuesday\n"; break;
case 4: cout << "Wednesday\n"; break;
case 5: cout << "Thursday\n"; break;
case 6: cout << "Friday\n"; break;
case 7: cout << "Saturday\n"; break;
default: cout << "Invalid day number\n";
}
}

13. Find the largest of three numbers (nested if-else).

#include <iostream>
using namespace std;
int main () {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
if (a>=b && a>=c) cout << "Largest: " << a << endl;
else if (b>=a && b>=c) cout << "Largest: " << b << endl;
else cout << "Largest: " << c << endl;
}

14. Input a month number (1-12) and print the number of days (consider leap years
for February).
#include <iostream>
using namespace std;
int main () {
int month, year;
cout << "Enter month (1-12): ";
cin >> month;
cout << "Enter year: ";
cin >> year;

switch(month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
cout << "31 days\n"; break;
case 4: case 6: case 9: case 11:
cout << "30 days\n"; break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
cout << "29 days (Leap Year)\n";
else
cout << "28 days\n";
break;
default:
cout << "Invalid month\n";
}
}

15. Simulate an ATM with options:


• Check Balance
• Withdraw
• Deposit (Use switch-case).

#include <iostream>
using namespace std;
int main () {
int balance = 1000, choice, amount;
do {
cout << "\nATM Menu:\n";
cout << "1. Check Balance\n2. Deposit\n3. Withdraw\n0. Exit\nChoice: ";
cin >> choice;

switch(choice) {
case 1:
cout << "Balance: " << balance << endl;
break;
case 2:
cout << "Enter amount to deposit: ";
cin >> amount;
balance += amount;
cout << "Deposited successfully!\n";
break;
case 3:
cout << "Enter amount to withdraw: ";
cin >> amount;
if (amount <= balance) {
balance -= amount;
cout << "Withdrawn successfully!\n";
} else cout << "Insufficient funds!\n";
break;
case 0:

cout << "Exiting ATM...\n";


break;
default:
cout << "Invalid choice!\n";
}
} while (choice != 0);
}

16. Apply discounts based on purchase amount:


• $100+: 10% off
• $500+: 20% off
• Else: No discount.

#include <iostream>
using namespace std;
int main () {
double purchase;
cout << "Enter purchase amount: ";
cin >> purchase;
if (purchase >= 1000) purchase *= 0.9;
else if (purchase >= 500) purchase *= 0.95;
cout << "Final amount: " << purchase << endl;
}

17. Calculate BMI and classify: Underweight (<18.5), Normal, Overweight, Obese.

#include <iostream>
using namespace std;
int main () {
double weight, height, bmi;
cout << "Enter weight (kg): ";
cin >> weight;
cout << "Enter height (m): ";
cin >> height;
bmi = weight / (height * height);
cout << "BMI = " << bmi << endl;
if (bmi < 18.5) cout << "Underweight\n";
else if (bmi < 25) cout << "Normal\n";
else if (bmi < 30) cout << "Overweight\n";
else cout << "Obese\n";
}

18. Compute income tax based on brackets (e.g., O-SSP 10000: 0%, SSP 10000- SSP
50000:10%, 50000-200000: 15% otherwise 200000+: 20%. (use if-else-if)

#include <iostream>
using namespace std;
int main () {
double income, tax=0;
cout << "Enter income: ";
cin >> income;
if (income <= 2500) tax = 0;
else if (income <= 5000) tax = (income-2500)*0.1;
else if (income <= 10000) tax = 2500*0.1 + (income-5000)*0.2;
else tax = 2500*0.1 + 5000*0.2 + (income-10000)*0.3;
cout << "Tax = " << tax << endl;
}
19. Check if a character is uppercase, lowercase, digit, or special symbol. (Clue: use
the ASClI values e.g. uppercase A – B: 65 – 96)

#include <iostream>
using namespace std;
int main () {
char ch;
cout << “Enter a character: “;
cin >> ch;
if (isupper(ch)) cout << “Uppercase Letter\n”;
else if (islower(ch)) cout << “Lowercase Letter\n”;
else if (isdigit(ch)) cout << “Digit\n”;
else cout << “Special Symbol\n”;
}

20. Convert 24-hour time to 12-hour format (e.g., 13:30 → 1:30 PM).

#include <iostream>
using namespace std;
int main () {
int hour, minute;
cout << “Enter time (HH MM): “;
cin >> hour >> minute;
string period = “AM”;
if (hour == 0) { hour = 12; period = “AM”; }
else if (hour == 12) { period = “PM”; }
else if (hour > 12) { hour -= 12; period = “PM”; }
cout << “12-hour format: “ << hour << “:” << (minute<10?”0”:””) << minute << “ “ <<
period << endl;
}

21. Rewrite any of the above problems (e.g., largest of two numbers) using the
ternary operator (?:).

#include <iostream>
using namespace std;
int main () {
int a, b;
cout << “Enter two numbers: “;
cin >> a >> b;
cout << “Largest = “ << (a > b ? a : b) << endl;
}

22. Check if a 3-digit number is a palindrome (e.g., 121) using only selection
statements (no loops).

#include <iostream>
using namespace std;
int main () {
int num, rev=0, temp;
cout << “Enter a 3-digit number: “;
cin >> num;
temp = num;
while (temp > 0) {
rev = rev*10 + temp%10;
temp /= 10;
}
if (num == rev) cout << “Palindrome\n”;
else cout << “Not a Palindrome\n”;
}

23. Check if a point (x, y) lies inside, outside, or on the boundary of a rectangle.

#include <iostream>
using namespace std;
int main () {
int x, y, x1, y1, x2, y2;
cout << "Enter bottom-left (x1,y1): ";
cin >> x1 >> y1;
cout << "Enter top-right (x2,y2): ";
cin >> x2 >> y2;
cout << "Enter point (x,y): ";
cin >> x >> y;
if (x>=x1 && x<=x2 && y>=y1 && y<=y2) cout << "Inside Rectangle\n";
else cout << "Outside Rectangle\n";
}

24. Classify a password as:


• Weak (length < 6)
• Medium (6-10 chars, no special symbols)
• Strong (> 10 chars with symbols).

#include <iostream>
using namespace std;
int main () {
string pass;
cout << "Enter password: ";
cin >> pass;
bool hasUpper=false, hasLower=false, hasDigit=false, hasSpecial=false;
for(char ch : pass) {
if (isupper(ch)) hasUpper = true;
else if (islower(ch)) hasLower = true;
else if (isdigit(ch)) hasDigit = true;
else hasSpecial = true;
}

if (pass.length() >= 8 && hasUpper && hasLower && hasDigit && hasSpecial)
cout << "Strong Password\n";
else
cout << "Weak Password\n";
}

25. Understand the Rock-Paper-Scissors Game then simulate a single round against
the computer (use rand () for computer choice).

#include <iostream>
using namespace std;
int main () {
srand(time(0));
string choices[3] = {"Rock", "Paper", "Scissors"};
int user, comp;
cout << "0 = Rock, 1 = Paper, 2 = Scissors\nEnter your choice: ";
cin >> user;
comp = rand() % 3;
cout << "Computer chose: " << choices[comp] << endl;
if (user == comp) cout << "Draw!\n";
else if ((user==0 && comp==2) || (user==1 && comp==0) || (user==2 && comp==1))
cout << "You Win!\n";
else
cout << "Computer Wins!\n";
}

You might also like