https://www.codechef.com/submit/POTATOES

Hey , Why this program is not printing anything???

#include
#include
using namespace std;

void isprime(int x)
{ int limit=sqrt(x);
int flag=0;
for(int i=2;i<=limit;i++)
{
if(x%i==0)
flag=1;
}
}

int main() {

int flag=0,t,x,y,i,sum;
while(t--)
{
    cin>>x>>y;
    sum=x+y;
    cout<<"hello";
    for(int i=1;i<11;i++)
            {
                sum+=1;
                isprime(sum);
                if(flag==0)
                {       cout<<i<<endl;
                        break;
                }
                else
                        cout<<"hello";
            }
}
return 0;

}

You did not input t (test cases).

1 Like

Your prime function literally does nothing. You are assigning the answer to the flag, but you are forgetting that flag is a temporary variable. As soon as the function returns, the flag would be thrown away, in fact, all the temporary variables are thrown away

The correct implementation to check if a number is prime

bool isPrime(int n){
    if(n == 0 || n==1)
    return false;
    
    for(int i=2;i*i<=n;i++){
        if(n%i == 0)
         return false;
     }
    return true;
}

I would suggest you have a look at how functions work in C++, like what is passing by value, what is passing by reference etc.