0% found this document useful (0 votes)
9 views

OOP Assignment

Uploaded by

ahmadhaseeb5964
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

OOP Assignment

Uploaded by

ahmadhaseeb5964
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Mirpur University of Science and Technology

Department of Software Engineering

Assignment No: 1
Names: Haseeb Ahmed
Roll No: FA23-BSE-027
Session: 23-27

Subject: Object Oriented programming

Submitted to: Aqsa Rasheed


Q-1: A program that reads a number and prints its square if
the number is greater than 10 otherwise prints its cube.

Code:

#include <iostream>

int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num;

if (num > 10) {


std::cout << "The square of " << num << " is: " << num * num
<< std::endl;
}
else {
std::cout << "The cube of " << num << " is: " << num * num *
num << std::endl;
}

return 0;
}

Output:

Program Report:

This C++ program prompts the user to enter a number, then


checks if it's greater than 10. If so, it outputs the square of
the number; otherwise, it outputs the cube.

Q-2: A program that reads an integer and prints whether a


program is even or odd number.

Code:

#include <iostream>

int main() {
int num;
std::cout << "Enter an integer: ";
std::cin >> num;

if (num % 2 == 0) {
std::cout << num << " is an even number." << std::endl;
}
else {
std::cout << num << " is an odd number." << std::endl;
}

return 0;
}

Output:
Program Report:

This C++ program prompts the user to enter an integer and


checks if it's even or odd by examining its remainder when
divided by 2. If the remainder is 0, it's even; otherwise, it's
odd.

Q-3: A program that reads three numbers and prints the


largest one.

Code:

#include <iostream>

int main() {
int num1, num2, num3;
std::cout << "Enter three numbers: ";
std::cin >> num1 >> num2 >> num3;

int largest = num1;


if (num2 > largest) {
largest = num2;
}
if (num3 > largest) {
largest = num3;
}

std::cout << "The largest number is: " << largest << std::endl;

return 0;
}

Output:
Program Report:

This C++ program prompts the user to input three numbers,


then compares them to find the largest among them.

It initially assumes the first number is the largest, then


checks the other two numbers. If either of them is greater
than the current largest, it updates the largest variable.
Finally, it prints out the largest number.

Q-4: A program that reads three numbers and prints the


smallest one.
Code:

#include <iostream>

int main() {
int num1, num2, num3;
std::cout << "Enter three numbers: ";
std::cin >> num1 >> num2 >> num3;

int smallest = num1;


if (num2 < smallest) {
smallest = num2;
}
if (num3 < smallest) {
smallest = num3;
}

std::cout << "The smallest number is: " << smallest << std::endl;

return 0;
}

Output:

Program Report:

This C++ program prompts the user to input three numbers


and finds the smallest among them using a series of
conditional checks. It initializes the variable 'smallest' with
the first input, then compares it with the remaining two
numbers, updating 'smallest' if a smaller number is found.
Finally, it prints the smallest number.

Q-5: A program that reads a letter and prints whether it is


lower case or uppercase letter.
Code:

#include <iostream>
#include <cctype>

int main() {
char letter;
std::cout << "Enter a letter: ";
std::cin >> letter;

if (std::isupper(letter)) {
std::cout << "The letter is an uppercase letter." << std::endl;
}
else if (std::islower(letter)) {
std::cout << "The letter is a lowercase letter." << std::endl;
}
else {
std::cout << "The input is not a letter." << std::endl;
}

return 0;
}

Output:
Program Report:

This C++ program prompts the user to enter a letter and


checks if it's uppercase, lowercase, or neither using the
std::isupper() and std::islower() functions from the <cctype>
library.

If it's uppercase, it outputs accordingly; if it's lowercase, it


does the same; otherwise, it notifies the user that the input is
not a letter.

Q-6: A program that will produce a table of equivalent temperatures


in both Fahrenheit and Celsius with an increment of 5 from 5o to
100 as given below.

Code:

#include <iostream>
#include <iomanip>

int main() {
std::cout << "Fahrenheit\tCelsius\n";
for (int fahrenheit = 50; fahrenheit <= 100; fahrenheit += 5) {
double celsius = 5.0 / 9.0 * (fahrenheit - 32);
std::cout << std::setw(8) << fahrenheit
<< "\t" << std::setw(8) << std::fixed << std::setprecision(2) <<
celsius << std::endl;
}

return 0;
}

Output:
Program Report:

This C++ program generates a table of Fahrenheit to


Celsius conversions, ranging from 50 to 100 Fahrenheit in
increments of 5.

It calculates the Celsius equivalent for each Fahrenheit


value and prints them in a formatted table, with
temperatures aligned and rounded to two decimal places for
clarity.

Q-7: A program that prints the sum of the following sequences


using for loop.

Code:

#include <iostream>

int main() {
int n, sum = 0;
std::cout << "Enter the value of n: ";
std::cin >> n;

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


sum += i * i + 4 * i + 4;
}

std::cout << "The sum of the sequence is: " << sum << std::endl;

return 0;
}

Output:

Program Report:

This C++ program calculates the sum of a sequence where


each term is the square of its position in the sequence plus
four times its position plus four, up to a specified value of \
( n \).

It prompts the user to input the value of \( n \), computes the


sum of the sequence using a loop, and then outputs the total
sum.
Q-8 : A program that prints the sum of the following sequences
using while loop.

Code:

#include <iostream>

int main() {
int n, sum = 0, i = 1;
std::cout << "Enter the value of n: ";
std::cin >> n;

while (i <= n) {
sum += i * i + 4 * i + 4;
i++;
}

std::cout << "The sum of the sequence is: " << sum << std::endl;

return 0;
}

Output:

Program Report:

This C++ program prompts the user to input a value for 'n',
then iterates from 1 to 'n', accumulating the sum of a
sequence defined by the expression 'i * i + 4 * i + 4' at each
step.
Finally, it outputs the total sum of the sequence to the
console, providing a concise demonstration of user input,
loop iteration, and arithmetic operations in C++.

Q-9: A menu-driven program using a Switch case to calculate the


following: 1. Area of circle 2. Area of square 3. Area of sphere 4.
Area of triangle 5. Area of rectangle. The output of the program
should be like this as given below:

Code:

#include <iostream>
#include <cmath>

using namespace std;

int main() {
int choice;
double radius, side, base, height, length, breadth;

while (true) {
cout << "\nPress 1 to calculate the area of circle" << endl;
cout << "Press 2 to calculate the area of square" << endl;
cout << "Press 3 to calculate the area of sphere" << endl;
cout << "Press 4 to calculate the area of triangle" << endl;
cout << "Press 5 to calculate the area of rectangle" << endl;
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter radius: ";
cin >> radius;
cout << "Area of circle: " << M_PI * radius * radius << endl;
break;
case 2:
cout << "Enter side: ";
cin >> side;
cout << "Area of square: " << side * side << endl;
break;
case 3:
cout << "Enter radius: ";
cin >> radius;
cout << "Area of sphere: " << 4 * M_PI * pow(radius, 3) <<
endl;
break;
case 4:
cout << "Enter base: ";
cin >> base;
cout << "Enter height: ";
cin >> height;
cout << "Area of triangle: " << 0.5 * base * height << endl;
break;
case 5:
cout << "Enter length: ";
cin >> length;
cout << "Enter breadth: ";
cin >> breadth;
cout << "Area of rectangle: " << length * breadth << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
break;
}

cout << "\nPress any key to continue.....\n";


system("pause");
}

return 0;
}
Output:

Program Report:

The code uses a while loop to display the menu to the user
repeatedly until they choose to exit. Inside the loop, a switch
statement is used to perform different actions based on the
user's choice.

The default case in the switch statement displays an error


message if the user enters an invalid choice. The required
area is calculated using the appropriate formula based on
the user's choice, and the result is displayed to the user.

The system("pause") function is used to pause the program


and wait for the user to press any key before continuing.

Q-10: A simple menu card program that allows the user to select a
drink….?
Code:

#include <iostream>
using namespace std;

int main() {
cout << "******************************************\n";
cout << " MENU CARD\n";
cout << "******************************************\n";
cout << "Please select your drink from the following :\n";
cout << "1.COFFEE\n2.TEA\n3.COLD COFFEE\n4.MILK
SHAKE\n5.STALC\n";
cout << "******************************************\n";

int choice, quantity;


double rate, totalAmount = 0;

cout << "Please Select Your Drink: ";


cin >> choice;

switch (choice) {
case 1:
cout << "You have selected Coffee.\n";
rate = 5.0; // Assuming the rate for coffee is $5
break;
case 2:
cout << "You have selected Tea.\n";
rate = 3.0; // Assuming the rate for tea is $3
break;
case 3:
cout << "You have selected Cold Coffee.\n";
rate = 6.0; // Assuming the rate for cold coffee is $6
break;
case 4:
cout << "You have selected Milk Shake.\n";
rate = 4.0; // Assuming the rate for milk shake is $4
break;
case 5:
cout << "You have selected Stalc.\n";
rate = 2.0; // Assuming the rate for stalc is $2
break;
default:
cout << "Invalid choice!\n";
return 1;
}

cout << "Enter the quantity: ";


cin >> quantity;

totalAmount = rate * quantity;

cout << "Total amount: " << totalAmount << endl;


cout << "----------------------------------------------\n";

return 0;
}

Output:
Program report:

The program starts by displaying a menu card with options


numbered from 1 to 5. It prompts the user to select a drink
by entering the corresponding number.

Based on the user's input, it uses a switch statement to


match the choice with the selected drink and assigns the
respective rate. Then, it prompts the user to enter the
quantity and calculates the total amount by multiplying the
rate with the quantity.

Finally, it prints out the total amount for the order.

Q-11: Write a program to find Grade of a Student Using


Switch Case..?

Code:
#include <iostream>
using namespace std;

int main() {
int marks;
cout << "Enter the marks: ";
cin >> marks;
char grade;

// Using switch case to determine grade based on marks


switch (marks / 10) {
case 10:
case 9:
grade = 'A';
break;
case 8:
grade = 'B';
break;
case 7:
grade = 'C';
break;
case 6:
grade = 'D';
break;
case 5:
grade = 'E';
break;
default:
grade = 'F';
}

if (marks >= 40 && marks <= 49)


grade = 'E'; // Adjusting for 'E-' grade

cout << "Grade: " << grade << endl;

return 0;
}

Output:
Program Report:

The program prompts the user to enter the marks obtained


by the student. It then uses a switch case to determine the
grade based on the range of marks.

Each case corresponds to a specific range of marks, and the


appropriate grade is assigned accordingly. Additionally, if
the marks fall in the range of 40 to 49, the grade is adjusted
to 'E' as per the requirement.

Finally, it prints out the calculated grade for the student.

Q-12: A program to find Grade of a Student Using if statement

Code:

#include <iostream>
using namespace std;

int main() {
int marks;
cout << "Enter the marks: ";
cin >> marks;

char grade;

if (marks >= 90 && marks <= 100)


grade = 'A';
else if (marks >= 80 && marks <= 89)
grade = 'B';
else if (marks >= 70 && marks <= 79)
grade = 'C';
else if (marks >= 60 && marks <= 69)
grade = 'D';
else if (marks >= 50 && marks <= 59)
grade = 'E';
else if (marks >= 40 && marks <= 49)
grade = 'E'; // Adjusting for 'E-' grade
else
grade = 'F';

cout << "Grade: " << grade << endl;

return 0;
}

Output:
Program Report:

The program prompts the user to enter the marks obtained


by the student. It then checks each range of marks using if
statements and assigns the appropriate grade accordingly.
Additionally, it adjusts the grade to 'E' if the marks fall in
the range of 40 to 49. Finally, it prints out the calculated
grade for the student.

Q-13: Write a program that read string to reverse a given string.

Code:
#include <iostream>
#include <string>
using namespace std;

int main() {
string str;
cout << "Enter a string: ";
getline(cin, str);

int length = str.length();

for (int i = length - 1; i >= 0; i--) {


cout << str[i];
}

cout << endl;

return 0;
}
Output:

Program Report:

The program prompts the user to enter a string using


`getline()` to ensure it can read spaces in the string. It
calculates the length of the string using the `length()`
function of the `string` class.

Then, it iterates through the characters of the string in


reverse order starting from the last character (at index
`length - 1`) to the first character (at index `0`), and prints
each character. This effectively reverses the given string and
outputs it to the console.

Q-14: A program to Check Prime Number (dynamic user


input based).
Code:
#include <iostream>
using namespace std;

bool isPrime(int num) {


if (num <= 1) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}

int main() {
int number;
cin >> number;
cout << (isPrime(number) ? "Prime" : "Not Prime") << endl;
return 0;
}

Output:

Program Report:

The program checks if the given number is prime or not by


iterating through all possible divisors up to the square root
of the number.
If any divisor is found, the number is not prime. Otherwise,
it is prime. Finally, it prints the result.

Q-14: Write a program that …………………………………….?

Code:

#include <iostream>
#include <cmath>
using namespace std;

int main() {
double a, b, c;
cout << "Quadratic equation ke coefficients (a, b, c) dalo: ";
cin >> a >> b >> c;

double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
double root1 = (-b + sqrt(discriminant)) / (2 * a);
double root2 = (-b - sqrt(discriminant)) / (2 * a);
cout << "Roots real aur alag hain.\nRoot 1 = " << root1 << "\
nRoot 2 = " << root2 << endl;
}
else if (discriminant == 0) {
double root = -b / (2 * a);
cout << "Roots real aur same hain.\nRoot = " << root << endl;
}
else {
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
cout << "Roots complex aur alag hain.\nRoot 1 = " << realPart
<< " + " << imaginaryPart << "i\nRoot 2 = " << realPart << " - " <<
imaginaryPart << "i" << endl;
}
return 0;
}

Output:

Program Report:

This program demonstrates the use of the quadratic formula


to calculate the roots of a quadratic equation based on the
values of its coefficients.

The program also uses conditional statements to print the


roots based on the value of the discriminant. The program
uses the pow and sqrt functions to perform mathematical
operations on the coefficients.

The program also uses the i to represent the imaginary unit


in the output.

Q-16: A program that reads string and then check if a given string,
is a palindrome.
Code:

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
string str;
cout << "Enter a string: ";
cin >> str;
string reversed = str;
reverse(reversed.begin(), reversed.end());
cout << (str == reversed ? "Palindrome" : "Not Palindrome") <<
endl;
return 0;
}

Output:

Program Report:

The program prompts the user to enter a string and then


checks if it's a palindrome by comparing it with its reversed
version.
It first copies the original string into another string variable,
then reverses the second string using the `reverse()`
function from the `<algorithm>` header.

Finally, it compares the original string with the reversed


string and prints whether it's a palindrome or not.

Q-17: Write A program that reads an integer and length of table


then print multiplication table of number to that length.

Code:

#include <iostream>
using namespace std;

int main() {
int number, length;
cout << "Enter a number: ";
cin >> number;
cout << "Enter the length of the table: ";
cin >> length;

cout << "Multiplication table of " << number << " up to length " <<
length << ":\n";
for (int i = 1; i <= length; ++i) {
cout << number << " * " << i << " = " << number * i << endl;
}

return 0;
}

Output:
Program Report:

This program first takes an integer input from the user


representing the number whose multiplication table is to be
printed, and then another integer input specifying the length
of the table.

It then prints the multiplication table of the given number


up to the specified length using a for loop.

Q-17: Write a program to print asterisk patterns as shown


below.

a) ……
Code:

#include <iostream>
using namespace std;

int main() {
int rows = 4;

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


for (int j = 1; j <= 5; j++) {
cout << "*";
}
cout << endl;
}

return 0;
}

Output:

b)………

Code:

#include <iostream>
using namespace std;

int main() {
cout << "*" << endl;
cout << "**" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "*****" << endl;
return 0;
}

Output:

c) …….

Code:

#include <iostream>
using namespace std;

int main() {
int n = 6; // number of rows
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}

Output:
d)…..
Code:

#include <iostream>
using namespace std;

int main() {
int n = 5; // number of rows
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || j == i) {
cout << "*";
}
else {
cout << " ";
}
}
cout << endl;
}
for (int i = 1; i <= n; i++) {
cout << "*";
}
return 0;
}
Output:

e)…
Code:

#include <iostream>
using namespace std;

int main() {
int n = 5; // number of rows
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i + 1; j++) {
cout << " ";
}
for (int j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1) {
cout << "*";
}
else {
cout << " ";
}
}
cout << endl;
}
return 0;
}

Output:
f)…..
Code:
#include <iostream>
using namespace std;

int main() {
int n = 5; // number of rows
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}

Output:

The End!

You might also like