25 02 2025 Lab Task 2 (Nasreen Fatima)
25 02 2025 Lab Task 2 (Nasreen Fatima)
Department of CS & IT
Object Oriented Programming Lab Tasks
Instructor Name: Miss Iqra Adnan Course Code: CS02203 | 11
Course Title: Object Oriented Programming
Student Name: Nasreen Fatima University ID: 70150894#B
PART A
1. Write a program to calculate square root of any number (i.e. entered by user) using
library functions.
CODE:
#include<iostream>
#include<cmath>
using namespace std;
int main(){
double num;
cout<<"Enter a number = "<<endl;
cin>>num;
if(num< 0){
cout<<"error"<<endl;
}
else{
double result=sqrt(num);
cout<<"square root of "<<num<<" is = "<<result<<endl;
}
return 0;
}
OUTPUT:
2. Write a c++ program to demonstrate the working of default argument. Make a function
add() to add 2 integers and display sum in main function.
CODE:
#include<iostream>
using namespace std;
int add(int a,int b=2){
return a+b;
}
int main(){
int sum1=add(3,10);
cout<<"sum of two numbers is "<<sum1<<endl;
int sum2=add(8);
cout<<"sum of 8 and default value is = "<<sum2<<endl;
return 0;
}
OUTPUT:
3. Write a c++ program to return absolute value of variable types integers and float using
function overloading.
CODE:
#include <iostream>
using namespace std;
int absolute(int num) {
return (num<0) ? -num : num;
}
return 0;
}
OUTPUT:
4. Write three functions ‘output’. The first version takes no parameters and display 5
asterisks. The second version takes an integer and displays the number of i integers. The
third version takes an integer and a character as parameters and displays the number of n
characters.
CODE:
#include <iostream>
using namespace std;
void display() {
for (int i = i; i < 5; i++) {
cout << "*";
}
cout << endl;
}
void display(int n) {
for (int i = 0; i < n; i++) {
cout << "*";
}
cout << endl;
}
int main() {
display();
display(7);
show(5, '#');
return 0;
}
OUTPUT:
5. Write a program to calculate cube of any number. Declare the function with keyword
inline.
CODE:
#include <iostream>
using namespace std;
inline int cube(int num) {
return num * num * num;
}
int main() {
int num;
cout << "Enter a number = ";
cin >> num;
int result = cube(num);
cout << "Cube of " << num << " is " << result << endl;
return 0;
}
OUTPUT:
6. Write a program that inputs radius of circle and uses an inline function AREA () to
calculate and return the area of circle.
CODE:
#include <iostream>
using namespace std;
inline float AREA(float r) {
return 3.14 * r * r;
}
int main() {
float radius;
cout<<"Enter radius of circle is = ";
cin>>radius;
cout<<"Area of circle is = "<<AREA(radius);
return 0;
}
OUTPUT:
PART B
1. Write a program that calculates the sum of the following series using function. The
main() function inputs a number and passes it to a function. The function finds the sum
from 1 to the given number and returns the result to main function().
1+1/1!+1/2!+1/3!+1/4!+…
CODE:
#include <iostream>
using namespace std;
double factorial(int num) {
double fact=1;
for (int i=1; i<=num; i++) {
fact*= i;
}
return fact;
}
double sumSeries(int n) {
double sum = 1.0;
for (int i=1; i<= n; i++) {
sum += 1.0/factorial(i);
}
return sum;
}
int main() {
int number;
cout<<"Enter a number is = ";
cin>>number;
cout<<"Sum of the series is = " << sumSeries(number)<<endl;
return 0;
}
OUTPUT:
2. Write a program that inputs two integers. It passes first integer to a function that
calculates and returns its square. It passes second integer to another function that
calculates and returns its cube. The main() function adds both returned values and
displays the results.
CODE:
#include <iostream>
using namespace std;
int calculateSquare(int num) {
return num * num;
}
int calculateCube(int num) {
return num * num * num;
}
int main() {
int num1, num2;
cout << "Enter first integer is = ";
cin >> num1;
cout << "Enter second intege is = ";
cin >> num2;
int square = calculateSquare(num1);
int cube = calculateCube(num2);
return 0;
}
OUTPUT:
3. Write a program that inputs values in a two dimensional array with three rows and two
columns. The program passes the array to a function. The function returns the maximum
value in the array.
CODE
#include <iostream>
using namespace std;
int findMax(int arr[3][2]) {
int maxVal = arr[0][0];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
if (arr[i][j] > maxVal) {
maxVal = arr[i][j];
}
}
}
return maxVal;
}
int main() {
int arr[3][2];
return 0;
}
OUTPUT
4. Write two versions of an overloaded function sum(). This first version takes one
parameter of integer array and returns the sum of all elements of the array. The second
version takes two parameters of integer array and character ‘E’ or ‘O’ . If the character is
‘E’, it returns the sum of even elements of the array and if the character is ‘O’ , it returns
the sum of odd elements. In case of any other character, it returns 0.
CODE:
#include <iostream>
using namespace std;
return 0;
}
OUTPUT: