Using Stack To Print Prime Numbers [closed] - c++

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.

Related

1.My problem in code is that when i input integers 75 and 100 it did not show my desired output [closed]

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.

I need help solve my problem of my activity

So I need my help to correct my code, which is given to my programming class assignment. My activity is to print numbers divisible by 5 for the integers from 1 to 99. So I tried to code like this:
#include <iostream>
using namespace std;
int main () {
int num, min, max;
cout << "Enter Number: ";
cin >> num;
min = 1;
max = 99;
if (num > min || num < max) {
if (num % 5 == 0) {
cout << "Divisible.";}
else {
cout << "Not Divisible";}
}
else {
if (num % 5 == 0) {
cout << "Error Input.";}
else {
cout << "Error input.";}
}
return 0;
}
So when I compile and run, I test to enter a divisible number by 5 or not. When I put 0, it says "Error input," that's correct. However, when I put 100, it says "divisible" instead of "error input." What is the correct input of my code?
The input is an integer from 1 to 99, which means that it should be >= 1 AND <= 99.
So, simply change
if (num > min || num < max)
to
if (num >= min && num <= max)
You made a mistake in the first if statement. When you are giving the OR operator, it'll return true even if only one of the conditions are satisfied.
You should use the AND operator for your code to work as expected.
Moreover, you don't have to use the min and max variables also, it is making the program unnecessarily big (only 2 lines though, but still).

A program that counts how many prime numbers are between 2 and a given number

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.

Loops and Modulo

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.

Why showing incorrect even sum in c++? [closed]

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.