0% found this document useful (0 votes)
19 views15 pages

Muhammad Hamid OOP Lab Assignment2

Muhammad_Hamid_OOP_Lab_Assignment2

Uploaded by

Mian Hamid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views15 pages

Muhammad Hamid OOP Lab Assignment2

Muhammad_Hamid_OOP_Lab_Assignment2

Uploaded by

Mian Hamid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

OOP Lab Assignment #2

Roll Number: B-29624


Name: Muhammad Hamid
Subject: OOP (Lab)

Q1: Find Maximum: Create a function that accepts three numbers and
returns the maximum of the three. Use a built in function max().
Solution:
#include<iostream>

using namespace std;

int findMax(int a, int b, int c) {

return max(a, max(b, c)); // Using max() to find the maximum of three numbers

int main() {

int num1, num2, num3;

// Input three numbers

cout << "Enter three numbers: ";

cin >> num1 >> num2 >> num3;

// Call the function and display the result

int maximum = findMax(num1, num2, num3);

cout << "The maximum of the three numbers is: " << maximum << endl;

return 0;
}

Q2: Even or Odd: Write a function that checks if a number is even or odd.
Solution:
#include<iostream>

using namespace std;

void evenOdd(int num) {

// Check if the number is even or odd

if (num % 2 == 0)

cout << num << " is even." << endl; // If divisible by 2, it's even

else

cout << num << " is odd." << endl; // Otherwise, it's odd

int main() {
int number;

// Input a number from the user

cout << "Enter a number: ";

cin >> number;

// Call the function to check if the number is even or odd

evenOdd(number);

return 0;

}
Q3: Fahrenheit to Celsius: Create a function to convert Fahrenheit to
Celsius. Formula:
Solution:
#include<iostream>

using namespace std;

// Function to convert Fahrenheit to Celsius

float fahrenheitToCelsius(float fahrenheit) {

return (fahrenheit - 32) * 5.0 / 9.0; // Conversion formula

int main() {

float fahrenheit;

// Input temperature in Fahrenheit from the user

cout << "Enter temperature in Fahrenheit: ";

cin >> fahrenheit;

// Call the function to convert and display the result

float celsius = fahrenheitToCelsius(fahrenheit);

cout << fahrenheit << " Fahrenheit is equal to " << celsius << " Celsius." << endl;

return 0;

}
Q4: Area of Circle: Write a function that takes the radius as input and
returns the area of the circle.
Solution:

#include<iostream>

using namespace std;

// Function to calculate the area of a circle

float areaOfCircle(float radius) {

return 3.14159 * radius * radius; // Area formula

int main() {

float radius;

// Input the radius of the circle from the user

cout << "Enter the radius of the circle: ";

cin >> radius;


// Call the function to calculate and display the area

float area = areaOfCircle(radius);

cout << "The area of the circle with radius " << radius << " is: " << area << endl;

return 0;

Q5: Simple Interest Calculator: Create a function to calculate simple


interest using the formula SI=PXRXT 100
Solution:

#include<iostream>

using namespace std;

// Function to calculate Simple Interest

float calculateSimpleInterest(float principal, float rate, float time) {

return (principal * rate * time) / 100; // Simple Interest formula

int main() {

float principal, rate, time;


// Input the principal, rate, and time from the user

cout << "Enter the principal amount: ";

cin >> principal;

cout << "Enter the rate of interest: ";

cin >> rate;

cout << "Enter the time (in years): ";

cin >> time;

// Call the function to calculate and display the simple interest

float simpleInterest = calculateSimpleInterest(principal, rate, time);

cout << "The Simple Interest is: " << simpleInterest << endl;

return 0;

Q6: Check Prime: Write a function that checks if a given number is prime.
Solution:
#include<iostream>

using namespace std;

// Function to check if a number is prime


bool isPrime(int num) {

// Numbers less than or equal to 1 are not prime

if (num <= 1)

return false;

// Check for factors from 2 to the square root of num

for (int i = 2; i * i <= num; i++) {

if (num % i == 0) // If divisible, it's not prime

return false;

return true; // If no factors found, the number is prime

int main() {

int number;

// Input the number from the user

cout << "Enter a number: ";

cin >> number;

// Call the function to check if the number is prime

if (isPrime(number))

cout << number << " is a prime number." << endl;

else

cout << number << " is not a prime number." << endl;

return 0;

}
Q7: Factorial Calculation: Create a function that returns the factorial of a
given number.
Solution:
#include<iostream>

using namespace std;

// Function to calculate the factorial of a number

int factorial(int num) {

int result = 1;

// Multiply numbers from 1 to num

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

result *= i; // Multiply result by the current number

return result; // Return the factorial

int main() {

int number;

// Input the number from the user

cout << "Enter a number: ";

cin >> number;


// Call the function to calculate and display the factorial

int fact = factorial(number);

cout << "The factorial of " << number << " is: " << fact << endl;

return 0;

Q8: Find Smallest Element: Write a function to find the smallest number in
a list.
Solution:

#include<iostream>

using namespace std;

// Function to find the smallest element in an array

int findSmallestElement(int arr[], int size) {

int smallest = arr[0]; // Assume the first element is the smallest

// Loop through the array to find the smallest element

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

if (arr[i] < smallest) {

smallest = arr[i]; // Update smallest if a smaller element is found


}

return smallest; // Return the smallest element

int main() {

int size;

// Input the size of the array

cout << "Enter the number of elements in the array: ";

cin >> size;

int arr[size];

// Input elements of the array from the user

cout << "Enter " << size << " elements: ";

for (int i = 0; i < size; i++) {

cin >> arr[i];

// Call the function to find and display the smallest element

int smallest = findSmallestElement(arr, size);

cout << "The smallest element in the array is: " << smallest << endl;

return 0;

}
Q9: Reverse String: Write a function that reverses a character in string and
the reverse the character stored in array.
Solution:

#include<iostream>

#include<string>

using namespace std;

// Function to reverse a string

string reverseString(string str) {

int n = str.length();

// Swap characters starting from the two ends

for (int i = 0; i < n / 2; i++) {

swap(str[i], str[n - i - 1]);

return str; // Return the reversed string

// Function to reverse characters in an array


void reverseArray(char arr[], int size) {

// Swap elements starting from both ends

for (int i = 0; i < size / 2; i++) {

swap(arr[i], arr[size - i - 1]);

int main() {

// Part 1: Reverse a string

string inputString;

cout << "Enter a string: ";

cin >> inputString;

string reversedString = reverseString(inputString);

cout << "Reversed string: " << reversedString << endl;

// Part 2: Reverse characters in an array

int size;

cout << "Enter the size of the character array: ";

cin >> size;

char arr[size];

cout << "Enter " << size << " characters: ";

for (int i = 0; i < size; i++) {

cin >> arr[i];

// Reverse the character array

reverseArray(arr, size);

// Display the reversed array

cout << "Reversed character array: ";


for (int i = 0; i < size; i++) {

cout << arr[i];

cout << endl;

return 0;

Q10: Generate Multiplication Table: Create a function that takes a number


and prints its multiplication table up to 10.
Solution:
#include<iostream>

using namespace std;

// Function to generate and print the multiplication table of a number

void multiplicationTable(int num) {

// Loop from 1 to 10 to generate the table

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

// Print the result of num * i


cout << num << " x " << i << " = " << num * i << endl;

int main() {

int number;

// Input the number from the user

cout << "Enter a number to generate its multiplication table: ";

cin >> number;

// Call the function to print the multiplication table

multiplicationTable(number);

return 0;

You might also like