using namespace std; // using cout statements int main() // main function { int
ID: 3651565 • Letter: U
Question
using namespace std; // using cout statementsint main() // main function
{
int n; //declaring variables
int i;
int x;
int count;
int temp;
cout << "Give me an integer n: " << endl; // prompts the user to enter an integer
cin >> n; //stores the entered value
for(i=3; i<=n; i++)//find the factors of i while updating the value of i with this for loop
{
count = 1;
temp = 0;
for(x=2; x<i; x++)
{
if((i%x) == 0)
{
temp++;
}
if (temp == 0)
{
count++;
}
}
}
cout << "There are " << count << " primes(s) less than or equal to "<< n <<endl;
return 0; // end function
}
can anyone explain to me why this code only prints out one prime number for every integer enteredand does not count all primes and how it has to be fixed/why
Explanation / Answer
#include using namespace std; // using cout statements int main() // main function { int n; //declaring variables int i; int x; int count; int temp; coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.