I have to find prime numbers for t cases. Examples of input / output below:
Input:
2
1 10
3 5
Output:
2
3
5
7
3
5
Notice the space between the answers as well.
#include <iostream>
#include <cmath>
bool prime (int x, int y);
using namespace std;
int main()
{
int t, x, y;
cin >> t;
for(int i = 0; i < t; i++)
cin >> x >> y;
for(int i = 0; i < t; i++){
for (int i = x; i <= y; i++)
cout << prime(x, y) << endl;
}
return 0;
}
bool prime(int x, int y){
bool prime = true;
for (int i = x; i <= y; i++){
for (int j = 2; j <= sqrt(i); j++)
if (i % j == 0)
prime = false;
return prime;
}
}
My program outputs only 1 all the time, why is that?