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

Oops Assignment 2

The document contains three C++ programs that demonstrate different functionalities. The first program calculates the area and total cost of carpeting based on user input for length, breadth, and cost per square meter. The second program is a guessing game where the user has three attempts to guess a predefined number, and the third program generates a sequence based on the Collatz conjecture for a given positive integer.

Uploaded by

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

Oops Assignment 2

The document contains three C++ programs that demonstrate different functionalities. The first program calculates the area and total cost of carpeting based on user input for length, breadth, and cost per square meter. The second program is a guessing game where the user has three attempts to guess a predefined number, and the third program generates a sequence based on the Collatz conjecture for a given positive integer.

Uploaded by

bizzylazing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

OOPS ASSIGNMENT 2

1. #include <iostream>
using namespace std;

int main()
{
double length, breadth, costPerSqM, area, totalCost;

cout << "Enter the length(in meters): ";


cin >> length;

if (length <= 0)
{
cout << "Invalid input." << endl;
return 0;
}

cout << "Enter the breadth(in meters): ";


cin >> breadth;
if (breadth <= 0)
{
cout << "Invalid input." << endl;
return 0;
}

cout << "Enter the cost per square meter of carpet: ";
cin >> costPerSqM;

if (costPerSqM <= 0)
{
cout << "Invalid input." << endl;
return 0;
}

area = length * breadth;


totalCost = area * costPerSqM;

cout << "Area of the room: " << area << " square
meters" << endl;
cout << "Total cost of carpeting: Rs. " << totalCost <<
endl;

return 0;

OUTPUT:

2. #include <iostream>
using namespace std;

int main()
{

int actual_num = 42;


int guess;
int trials = 3;
cout << "Guess the number (between 1 and 100)." <<
endl;

for (int i = 1; i <= trials; i++)


{
cout << "Trial " << i << ": Enter your guess: ";
cin >> guess;

if (guess == actualNumber)
{
cout << "You guessed the number" << endl;
return 0;
}
else if (guess > actualNumber)
{
cout << "Your guess is higher than the actual
number." << endl;
}
else
{
cout << "Your guess is lower than the actual
number." << endl;
}
}

cout << "Out of trials! The actual number was: " <<
actual_num << endl;

return 0;

OUTPUT:

3. #include <iostream>
using namespace std;
int main()
{

int n;

cout << "enter a positive integer:" ;


cin >> n;

if (n<=0)
{
cout << " invalid input, enter a positive integer.";
return 0;
}

cout << "generated sequence : ";

while(n!=1)
{
cout << n << " ";
if(n%2==0)
{
n=n/2;
}

else
{
n=3*n+1;
}
}

cout << n << endl;

return 0;

OUTPUT:

You might also like