21 / 25
Feb 2020
28 days later

what’s problem in this code any one can help me
#include<stdio.h>
int main()
{
int t,num1,num2,i,j,flag;
scanf("%d",&t);
if(t<=10)
for(i=0;i<t;++i)
{
scanf("%d%d",&num1,&num2);
for(i=num1+1;i<num2;++i)
{
flag=0;
for(j=2;j<=i/2;++j)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("%d\n",i);
}
}
return 0;
}

3 months later

What is wrong with my code? It’s working on mine but it doesn’t accept my solution.
// SPOJ PRIME1 - Prime Generator
#include

using namespace std;

// Checks if prime or not
bool checkPrime(long j)
{
int checker = 0;
if (j > 1)
{
for (int i = 2; i <= 10; i++)
{
if (j % i == 0)
{
checker++;
}
}
if (checker > 1)
return false;
else if (checker == 1)
return true;
else
return false;
}
else
return false;
}

int main()
{
// Input
int step;
do
{
cin >> step;
}
while (step > 10);

// Iterates each step
for (int i = 0; i < step; i++)
{
	long m, n;
	cin >> m >> n;
	
	// Iterates from m to n
	for (int j = m; j <= n; j++)
	{
		bool isPrime = checkPrime(j);
		if (isPrime)
			cout << j << endl;
	}
	cout << endl;
}
return 0;

}

1 month later


Can someone explain why this code gives WA ?? I have used segmented Seive. It works on testcases which I tried.