You are on page 1of 8

OBJECT ORIENTED

PROGRAMMING
LAB 4

HASNAAT RAZA
22F-3389
15TH FEB,2023
1

TASK 1:
#include <iostream>

using namespace std;

int sum(int num)

if (num == 0)

return 0;

else

return num + sum(num - 1);

int main()

int num;

cout << "Enter the number:" << endl;

cin >> num;

sum(num);

cout << "The sum is: " << sum(num) << endl;

double avg = sum(num) / num;

cout << "The average is: " << avg << endl;

return 0;

}
2

Task 2:
#include <iostream>

using namespace std;

void fibonacci(int n, int a = 0, int b = 1)

if (n <= 0)

return;

cout << a << " ";

fibonacci(n - 1, b, a + b);

int main()

int n;

cout << "enter the number of elements: ";

cin >> n;
3

cout << "fibonacci Series: ";

fibonacci(n);

return 0;

Task 4:
#include <iostream>

using namespace std;

struct Car

char carName[20];

char carModel[20];

int yearModel;

double cost;

};

int main()

Car car1 = {"Toyota", "Mustang", 2000, 25000};

Car car2 = {"Changan", "Alsvin", 2022, 20000};


4

if (car1.cost > car2.cost)

cout << "The " << car1.carName << " has a higher cost." <<
endl

<< endl;

else

cout << "The " << car2.carName << " has a higher cost." <<
endl

<< endl;

cout << "*** DATA THROGH OBJECTS ***" << endl;

cout << "Data Through car1" << endl;

cout << "Name: " << car1.carName << endl

<< "Car Model: " << car1.carModel << endl

<< "Year Model: " << car1.yearModel << endl

<< "Cost: " << car1.cost << endl

<< endl;

cout << "Data Through car2" << endl;

cout << "Name: " << car2.carName << endl

<< "Car Model: " << car2.carModel << endl

<< "Year Model: " << car2.yearModel << endl

<< "Cost: " << car2.cost << endl;

}
5

Task 5:
#include <iostream>

using namespace std;

struct distanc

int feet;

float inches;

};

int main()

distanc d1,d2,d3;

cout << "Enter the distance for d1 in feet: ";

cin >> d1.feet;

cout << "Enter the distance for d1 in inches: ";

cin >> d1.inches;

d2.feet = 10;

d2.inches = 5.25;
6

d3.feet = d1.feet + d2.feet;

d3.inches = d1.inches + d2.inches;

cout << "d1 = " << d1.feet << " feet " << d1.inches << " inches"
<< endl;

cout << "d2 = " << d2.feet << " feet " << d2.inches << " inches"
<< endl;

cout << "d3 = " << d3.feet << " feet " << d3.inches << " inches"
<< endl;

if (d3.inches >= 12.0) {

d3.feet += 1;

d3.inches -= 12.0;

cout << "AFTER IF CONDITION" << endl;

cout << "d1 = " << d1.feet << " feet " << d1.inches << " inches"
<< endl;

cout << "d2 = " << d2.feet << " feet " << d2.inches << " inches"
<< endl;

cout << "d3 = " << d3.feet << " feet " << d3.inches << " inches"
<< endl;

return 0;

}
7

You might also like