OOP Assignment
OOP Assignment
Assignment No: 1
Names: Haseeb Ahmed
Roll No: FA23-BSE-027
Session: 23-27
Code:
#include <iostream>
int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num;
return 0;
}
Output:
Program Report:
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:
Code:
#include <iostream>
int main() {
int num1, num2, num3;
std::cout << "Enter three numbers: ";
std::cin >> num1 >> num2 >> num3;
std::cout << "The largest number is: " << largest << std::endl;
return 0;
}
Output:
Program Report:
#include <iostream>
int main() {
int num1, num2, num3;
std::cout << "Enter three numbers: ";
std::cin >> num1 >> num2 >> num3;
std::cout << "The smallest number is: " << smallest << std::endl;
return 0;
}
Output:
Program Report:
#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:
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:
Code:
#include <iostream>
int main() {
int n, sum = 0;
std::cout << "Enter the value of n: ";
std::cin >> n;
std::cout << "The sum of the sequence is: " << sum << std::endl;
return 0;
}
Output:
Program Report:
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++.
Code:
#include <iostream>
#include <cmath>
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;
}
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.
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";
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;
}
return 0;
}
Output:
Program report:
Code:
#include <iostream>
using namespace std;
int main() {
int marks;
cout << "Enter the marks: ";
cin >> marks;
char grade;
return 0;
}
Output:
Program Report:
Code:
#include <iostream>
using namespace std;
int main() {
int marks;
cout << "Enter the marks: ";
cin >> marks;
char grade;
return 0;
}
Output:
Program Report:
Code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout << "Enter a string: ";
getline(cin, str);
return 0;
}
Output:
Program Report:
int main() {
int number;
cin >> number;
cout << (isPrime(number) ? "Prime" : "Not Prime") << endl;
return 0;
}
Output:
Program Report:
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:
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:
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:
a) ……
Code:
#include <iostream>
using namespace std;
int main() {
int rows = 4;
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!