Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
when I was trying to calculate the sum of even numbers the result isn't correct if the starting number is negative?
Here is the code:
#include <iostream>
using namespace std;
int main() {
signed int startingNumber, upperLimit;
signed int result = 0;
cout << "please enter the starting number: "; cin >> startingNumber;
cout << "please enter the upper limit: "; cin >> upperLimit;
while (true) {
if (startingNumber % 2 != 0) result += startingNumber;
if (upperLimit == startingNumber) break;
startingNumber++;
}
cout << "The result is: " << result << endl;
}
But when I run the script the following is the result:
please enter the starting number: -2
please enter the upper limit: 4
The result is: 3
I think the result should be 4 not 3, as the calculation is -2 +0 + 2 + 4 = 4.
Regards,,,
You're using if (startingNumber % 2 != 0) which means it will only add up numbers which are not divisible by two, resulting in -1 + 1 + 3 = 3. The condition in the if statement is only true if the remainder of dividing startingNumber by two is non-zero, which will be the case for odd numbers.
Use if (startingNumber % 2 == 0) result += startingNumber; instead and you should get the right sum.
Change this (startingNumber % 2 != 0) with this:
(startingNumber % 2 == 0)
Because what you want is to sum the even numbers and not odd numbers.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
/*when you will enter any number between 0-100 .
There are four ranges and the program will show in which range your number lies*/
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter your number between 0-100\n";
cin >> number;
if(number <= 25 || number == 0)
{
cout << "your number is between 0-25\n";
}
else if (number > 25 || number <= 50)
{
cout << "your number is between 25-50\n";
}
else if (number > 50 || number <= 75)
{
cout << "your number is between 50-75\n";
}
else if(number >75 || number <= 100)
{
cout << "your number is between 75-100\n";
}
else
{
cout<<"number is not between 0-100\n";
}
return 0;
}
The second if will always fire if the first if did not. Reason is it should not be OR, ||, but and AND, &&, same with the remaining tests.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm new to C++ and I want to be able to run the program again as long as the user desires. But the problem is, whenever I loop the program, it doesn't show the outputs anymore. How do I fix this problem?
long numCoco = 0, numPeople = 0;
char again = 'Y';
do
{
cout << "enter the number of coconuts gathered: ";
cin >> numCoco;
//validate user input
while (numCoco < 1 || numCoco > 5000)
{
if (cin.fail())
{
cin.clear();
cin.ignore();
cout << "You have entered the wrong input\n";
cin >> numCoco;
}
if (!cin.fail())
break;
}
for (numPeople = numCoco - 1; numPeople > 1 && !found; numPeople--)
{
found = splitCoco(numCoco, numPeople);
if (found)
{
cout << numCoco << " Coconuts, " << numPeople << " Persons, " << " and 1 monkey\n\n";
}
}
if (!found)
{
cout << numCoco << " no solution\n\n";
}
cout << "Run again? (Y/N): ";
cin >> again;
} while (again == 'y' || again == 'Y');
Desired Output
Enter the number of coconuts gathered: 25
25 coconuts, 3 persons, and 1 monkey
Run again(Y/N): y
Enter the number of coconuts gathered: 30
30 coconuts, No solution
Run again(Y/N): N
Actual Output
Enter the number of coconuts gathered: 25
25 coconuts, 3 persons, and 1 monkey
Run again(Y/N): y
Enter the number of coconuts gathered: 30
Run again(Y/N): N
You must set "found" variable to 0 before the for loop.You expect "found" variable to be 0 when the input is 30 but actually the line:
found = splitCoco(numCoco, numPeople);
never executes in the second iteration.
The problem here is that after the first iteration(when input is 25), found is set to true and in the second iteration(when input is 30), found is still true so both the condition of "for loop" and the condition of the "if block" are false so none of them executes; so nothing is printed and the loop starts over.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am not sure if anyone can help me here but I am having a problem in my for loop and my loop continuation.
This is what the code is supposed to output.
Enter a starting integer value: 8
Enter an ending integer value: 121
Enter a positive increment: 17
Sum (using a while loop): 413
Sum (using a for loop): 413
This is what my code outputs.
Enter the starting integer value: 8
Enter the ending integer value: 121
Enter the positive increment: 17
Sum(using a while loop) = 413
Sum(using a for loop)= 110
Here is my code if someone can help me.
#include <iostream>
using namespace std;
int main()
{
//defining the integers
int startingNumber, endingNumber, positiveIncrement;
cout <<"Enter the starting integer value: ";
cin >> startingNumber;
cout <<"Enter the ending integer value: ";
cin >> endingNumber;
cout <<"Enter the positive increment: ";
cin >> positiveIncrement;
//maiking sure the starting number is greater than 0
//also making sure the ending number is greater than
//the starting number.
if ((startingNumber <= 0) || (startingNumber > endingNumber))
{
cout<<"Error in input provided"<< endl;
return 0;
}
int while_loop_Sum = 0;
//start of while loop
while_loop_Sum = startingNumber;
while ((startingNumber + positiveIncrement) <= endingNumber)
{
startingNumber += positiveIncrement;
while_loop_Sum += startingNumber;
}
cout << "Sum(using a while loop) = " << while_loop_Sum << endl;
//end of while loop
//start of for loop
int for_loop_Sum = 0;
{
for ((for_loop_Sum = startingNumber);((startingNumber +
positiveIncrement) <= endingNumber);(startingNumber +=
positiveIncrement))
{
for_loop_Sum += (startingNumber+positiveIncrement);
}
cout << "Sum(using a for loop)= " << for_loop_Sum;
//end of for loop
}
return 0;
}
Help would be greatly appreciated.
You never reset starting_number after the while loop! You cin >> startingNumber;, then in the while loop you startingNumber += positiveIncrement; and then you go on to use it in the for loop as if it's good, but it's not!
You need to store the actual starting number in a variable when you get it and then use some other temporary in the while and for to avoid this issue. Maybe something like:
cin >> startingNumber;
...
int tmpStarting = startingNumber;
while ((tmpStarting + positiveIncrement) <= endingNumber) {
...
}
...
tmpStarting = startingNumber; //Reset starting number for the for!
for(...
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have this code, its just a part from another bigger code. This may seem like a very simple mistake, but I still cannot figure out what it is. My problem is that the value of 'house1' cannot exit the while loop and move on to switch statement. for example, if I entered house1 = 0, the program will print "Invalid value, please enter again" but if I entered house1 = 1 after I enter the invalid value, it will also print "invalid value, please enter again". Does anyone have any idea what was my mistake?
cin >> house1 ;
while ((0 <= house1) || (house1 >= 6))
{
cout << "Invalid value! please enter again:";
cin >> house1;
}
switch (house1)
{
case '1':
h1_p1 = h1_p1 - 5;
h2_p1 = h2_p1 + 1;
h3_p1 = h3_p1 + 1;
h4_p1 = h4_p1 + 1;
h5_p1 = h5_p1 + 1;
house_p1 = house_p1 + 1;
break;
case '2':
h2_p1 = h2_p1 - 5;
h3_p1 = h3_p1 + 1;
h4_p1 = h4_p1 + 1;
h5_p1 = h5_p1 + 1;
house_p1 = house_p1 + 2;
break;
If you meant the valid value should be from 1 to 5, the condition should be
while ((house1 <= 0) || (house1 >= 6))
{
cout << "Invalid value! please enter again:";
cin >> house1;
}
BTW: You're not checking invalid input (the failbit of cin) for house1.
The main problem with this code is this statement
while ((0 <= house1) || (house1 >= 6))
See if you enter 0,1,2,3,4,5,6,7,... or any positive integer number (as the value of house1) this while statement is correct for that.
So the control goes into the while loop and will not come out of the loop until you enter the negative number... Because when you enter the negative number only then the condition in the while statement will not satisfy and the control will come out of the loop.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to print out all prime factors of any given number by the user. So far I was able to get the code to check and print the prime numbers however, I am having issues with reprinting the number multiple times. Ex: Enter a prime number: 9 , Program prints: 3 * 3
#include <iostream>
#include <cmath>
#include <stack>
using namespace std;
int main()
{
int number;
stack<int> PrimeFactor;
cout << "Please enter an integer: ";
cin >> number;
cout << endl;
cout << "The prime factors of " << number << " in descending order are: ";
if((number % 2) == 0)
PrimeFactor.push(2);
if((number % 3) == 0)
PrimeFactor.push(3);
if((number % 5) == 0)
PrimeFactor.push(5);
if((number % 7) == 0)
PrimeFactor.push(7);
if((number % 11) == 0)
PrimeFactor.push(11);
for (int i = 2; i <= sqrt(number); i++)
{
if (number % i == 0 && i % 2 != 0 && i % 3 != 0 && i % 5 !=0 && i % 7 != 0 && i % 11 != 0)
{
PrimeFactor.push(i);
}
}
while(!PrimeFactor.empty())
{
cout << PrimeFactor.top() << " * ";
PrimeFactor.pop();
}
cout << endl;
return 0;
}
It returns "3 *" because 3 is the only number ever pushed onto the stack, which means the logic you are using is wrong.
Think like this: for the number the user enters, you have to find the first prime that can divide the number, and then do the same for the result of the division, and so on.
A very simple pseudocode:
number = X;
can 2 divide X?
yes -> push 2; Y = X/2;
can 2 divide Y?
no
can 3 divide Y?
no
can <next prime> divide Y?
yes -> push <next prime>; Z = Y/<next prime>
This means your loop counter "i" should be incremented to the next prime, and not by one.
To point your error perfectly:
when i = 3, the condition i % 3 != 0 fails, and 3 doesn't get pushed onto the stack.
You shouldn't test if a number is prime dividing it by itself (which is what you are doing in the if condition), because you will always get 0 and a result that says it is not prime because of that.
If you remove "i % 3 != 0" from the if condition, you will see that you get the result that you expect: 3 * 3.
To avoid this, I recommend writing a simple isPrime(int x) function that checkes if a number is prime.