Question: C++ code problem. Attached is my code in full text form. The problem is attached as well. The first loop is supposed to sum from
C++ code problem.
Attached is my code in full text form. The problem is attached as well.
The first loop is supposed to sum from 1 to any number the user enters. This works just fine.
The second is supposed to display the length of an interval entered by the user. But keeps coming to zero.
1 to 9 is 7. (We exclude 1 and 9.)
And the last is supposed to take the product.
3^4 =81
This should be easy but I am just struggling so much right now. Please help.
My code:
#include
using std::endl;
using std::cout;
using std::cin;
//using namespace::std;
int main()
{
// Part (a)
int check = 0, number, answer=0, x1, x2;
int length = 0;
cout << "Please enter an integer: " << endl;
while (check != 0)
system("Pause");
{
cin >> number;
if (number > 0)
{
for (int i = 1; i <= number; i++)
{
answer = answer + i;
}
cout << "The sum of the integers from 1 to " << number << " is "
<< answer << endl;
//break;
}
else
//system("Pause");
{
cout << "Enter a number greater than or equal 1: " << endl;;
}
}
// Part (b)
cout << "Enter two numbers to find the length: " << endl;
cin >> x1 >> x2;
if (x1 < x2)
{
while (++x1 < x1)
{
length++;
}
}
cout << "The length between " << x1 << " and " << x2 << " is: " << length << endl;
check = 0;
// Part(c)
cout << "Enter another pair of numbers to find the product of both: " << endl;
while (check != 0);
{
cin >> x1 >> x2;
if (x2 > 0)
{
if (x2 == 0)
{
answer = 0;
}
else {
answer = x1;
do {
answer = answer ^ x1;
break;
} while (--x2 > 1);
}
}
else {
cout << "Enter the pair without 2nd being negative: " << endl;
}
}
cout << "The product of " << x1 << " and " << x2 << " is: " << answer << endl;
system("Pause");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
