I'm not sure why this code isn't working as intended.
"input % 7 != 3 || input % 7 != 4)". I'm saying if input modulo 7 does not equal to 3 or 4 then stay in the loop. however when I enter 10 it still doesn't work when 10 modulo 7 equals to 3.
#include <iostream>
#include <string>
using namespace std;
int main(){
int input;
int count = 1;
cout << "Please Enter a positive integer that is 3 or 4 modulo 7: ";
cin >> input;
while (input <= 0 || input % 7 != 3 || input % 7 != 4){
count++;
cout << count << "tries, " << "please try again: ";
cin >> input;
}
cout << "Congratulations, you passed";
return 0;
}
I'm saying if input modulo 7 does not equal to 3 or 4 then stay in the loop.
In that case, you need to change your condition to this:
while (input <= 0 || (input % 7 != 3 && input % 7 != 4))
There is a logical error, here, this is the condition you might need:
while (input <= 0 || (input % 7 != 3 && input % 7 != 4)){
You want to stay in the loop while the modulu is different from both 3 and 4.
I'm not sure why this code isn't working as intended. "input % 7 != 3 || input % 7 != 4)". I'm saying if input modulo 7 does not equal to 3 or 4 then stay in the loop
No, you're not!
You're saying if input modulo 7 does not equal 3, or input modulo 7 does not equal 4, stay in the loop.
That condition always holds because input modulo 7 cannot be both 3 and 4 at the same time.
This kind of rewrite of logical connectives is actually an English mistake; although colloquially accepted in English, it's not in C++.
You meant "if input modulo 7 does not equal 3, and input modulo 7 does not equal 4, stay in the loop", i.e. input % 7 != 3 && input % 7 != 4.
Related
int main()
{
int number;
do
{
cout << "Input a number (1 - 8): " << endl;
cin >> number;
}
while (number < 0 || number > 8);
}
Why do the numbers 0 and 8 break out of the loop even though the while expressions are not satisfied? 0 < 0 == false and 8 > 8 == false?
Currently, the condition in your do-while loop is checking if your number is less than 0 or greater than 8. If the condition is true the loop repeats until your number is within the range from 0 to 8. The loop repeats as long as the condition is true. I think you are trying user to input a number in the range 1 to 8, if the input is out of this range, you want to keep asking the user to input a correct number. If this is the case, you need to edit your while condition this way:
while (number < 1 || number > 8);
I've just had one of my exams at the college
one of the questions were to program a function that counts how many prime numbers are between 2 and a given number from the user(including the number).
I wrote this algorithm which works for me but they still deducted all of the points of the question as if it was completely wrong.
Can please someone tell me what's wrong with the code?
Thanks a lot.
#include <iostream>
using namespace std;
void main()
{
int count = 0;
int num;
cout << "Please enter a natural number " << endl;
cin >> num;
for (int i = 2; i <= num; i++)
{
if ((i == 2 || i == 3 || i == 5||i == 7) || (i % 3 != 0 & i % 5 != 0 & i % 7 != 0 & i % 2 != 0))
count++;
}
cout << "There are " << count << " prime numbers beteween 2 amd " << num << endl;
}
Your code only checks if a number is a multiple of 2, 3, 5, or 7. That's missing a lot of multiples, meaning your code likely gives many false positives.
That condition is also quite difficult to read, so you probably would have lost points for that as well.
You would have been much better off replacing that condition in the loop with another for loop, and checking every odd number from 3 to the square root of the test number.
do
{
cout << "Enter a positive integer: ";
cin >> n;
}while ( n <= 1 || n == 0);
How should I code so that if I enter '1234', it would reject because there is a digit '1' there.
One approach would be to input a std::string from the user, and use std::string::find to see if 0 or 1 are present. Once you're done with that, attempt to convert the string to an integer.
Alternatively, if you want to keep everything "numeric", you can always use % 10 to extract the rightmost digit:
if (n % 10 < 2){
/*not allowed: I'm assuming n is positive here*/
}
Followed by
n /= 10
to remove that digit, and repeat, until you're left with zero. Obviously you'll need to test the special case of n starting at 0 separately.
My program executes just fine, but I have questions about how my while loop is set up.
I know the Boolean values for true and false are 1 and 0, respectively, but I'm not understanding why my output displays the even and odd numbers backwards (to my understanding, it's backwards). Simply put, I don't understand why if ( number % 2 == 0 ) would display that a number is even and when I change it to 1, it displays odd. I'm reading this line as, if (even number equals to false). I don't know if that's where I'm going wrong. What's the correct way to read this line?
The way I have my code set up now displays the numbers correctly, I'm just not understanding why. Can anyone help?
// Program indefinitely displays an even
// or odd number until a negative number
// is entered.
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Please enter a number: ";
cin >> number;
while ( number >= 0 )
{
if ( number % 2 == 0 )
{
cout << number << " is even \n";
cout << "Please enter a number: ";
cin >> number;
}
else
{
cout << number << " is odd \n";
cout << "Please enter a number: ";
cin >> number;
}
}
cout << "Thank you. \n";
return 0;
}
number % 2 is 0 if number divides 2 (i.e. is even), 1 if number is positive and does not divide 2 (i.e. is odd), and -1 if number is negative and does not divide 2 (i.e. is odd). (The last point must be the case from C++11 onwards).
So, since 0 == 0 is true, number % 2 == 0 is true if, and only if, number is even.
So you've written if ( number % 2 == 0 ) to trap all even cases, and the else traps the odd cases.
Testing if ( number % 2 == 1 ) is only a test for positive oddness, but older C++ standards allow this to be true for negative number.
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.