You are on page 1of 4

First one just says run the code

2. #include <iostream>
using namespace std;

int main()
{
int favoriteNumber, numberOfPods, peasPerPod, totalPeas;

cout << "Enter your favorite number: ";


cin >> favoriteNumber;
cout << "Your favourite number is: " << favoriteNumber << "\n";

cout << "Press return after entering a number.\n";


cout << "Enter the number of pods:\n";
cin >> numberOfPods;

cout << "Enter the number of peas in a pod:\n";


cin >> peasPerPod;

totalPeas = numberOfPods * peasPerPod;

cout << "If you have " << numberOfPods << " pea pods\n";
cout << "and " << peasPerPod << " peas in each pod, then\n";
cout << "you have " << totalPeas << " peas in all the pods.\n";

return 0;
}
3. #include <iostream>
using namespace std;

int main()
{
int favoriteNumber, numberOfPods, peasPerPod, totalPeas;
int firstNumber, secondNumber, sum;

cout << "Enter your favorite number: ";


cin >> favoriteNumber;
cout << "Your favourite number is: " << favoriteNumber << "\n";

cout << "Enter the first number: ";


cin >> firstNumber;

cout << "Enter the second number: ";


cin >> secondNumber;

sum = firstNumber + secondNumber;


cout << "The sum of " << firstNumber << " and " << secondNumber << " is " << sum << "\n";

cout << "Press return after entering a number.\n";


cout << "Enter the number of pods:\n";
cin >> numberOfPods;

cout << "Enter the number of peas in a pod:\n";


cin >> peasPerPod;
totalPeas = numberOfPods * peasPerPod;

cout << "If you have " << numberOfPods << " pea pods\n";
cout << "and " << peasPerPod << " peas in each pod, then\n";
cout << "you have " << totalPeas << " peas in all the pods.\n";

return 0;
}

4. #include <iostream>
using namespace std;

int main()
{
int favoriteNumber, numberOfPods, peasPerPod, totalPeas;
int firstNumber, secondNumber, result;

cout << "Enter your favorite number: ";


cin >> favoriteNumber;
cout << "Your favourite number is: " << favoriteNumber << "\n";

cout << "Enter the first number: ";


cin >> firstNumber;

cout << "Enter the second number: ";


cin >> secondNumber;

result = firstNumber - secondNumber;


cout << "The result of subtracting " << secondNumber << " from " << firstNumber << " is " << result << "\n";

cout << "Press return after entering a number.\n";


cout << "Enter the number of pods:\n";
cin >> numberOfPods;

cout << "Enter the number of peas in a pod:\n";


cin >> peasPerPod;

totalPeas = numberOfPods * peasPerPod;

cout << "If you have " << numberOfPods << " pea pods\n";
cout << "and " << peasPerPod << " peas in each pod, then\n";
cout << "you have " << totalPeas << " peas in all the pods.\n";
return 0;
}

In this modified version of the program:

 The second calculation now performs subtraction instead of addition.


 The program calculates the result of subtracting the second number from the first
number and displays it.
 The program then proceeds with the original functionality of calculating the total
number of peas based on the user's input.

Scenarios:
1. Negative Number (e.g., -22):
 If the user enters a negative number like -22 as the second input, the
subtraction operation will work correctly, and the result will be displayed
accordingly.
2. Extremely Large Number (e.g., 9,876,543,210):

 C++ data types like int have limits on the range of values they can
represent. If the user enters a number like 9,876,543,210 and subtracts
another number from it, the result may overflow the range of int , leading
to unexpected behavior or incorrect results. In such cases, using a data
type like long long with a larger range might be more appropriate to
handle extremely large numbers.
5.

You might also like