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: