0% found this document useful (0 votes)
12 views8 pages

Lab Task

This document contains code for 5 tasks related to object oriented programming lab 4. Task 1 defines a recursive function to calculate the sum and average of integers input by the user. Task 2 defines a recursive function to print the Fibonacci sequence for a given number of elements. Task 3 defines a Car structure with data members and prints car details through objects. Task 4 adds distances stored in a struct and checks if the total inches exceeds 12 to increment the feet.

Uploaded by

hasnaat malik
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)
12 views8 pages

Lab Task

This document contains code for 5 tasks related to object oriented programming lab 4. Task 1 defines a recursive function to calculate the sum and average of integers input by the user. Task 2 defines a recursive function to print the Fibonacci sequence for a given number of elements. Task 3 defines a Car structure with data members and prints car details through objects. Task 4 adds distances stored in a struct and checks if the total inches exceeds 12 to increment the feet.

Uploaded by

hasnaat malik
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/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy