What is wrong with my code for the problem PRB01(beginner)?

#include <stdio.h>

int main(void) {
int n;
scanf("%d\n",&n);
while(n–)
{
int num,c=0;
scanf("%d\n",&num);
for(int j=1;j<=num;j++)
{
if(num%j==0)
c++;
}
if (c==2)
printf(“Yes\n”);
else
printf(“no\n”);
}
return 0;
}

1 Like

There are many mistakes in your code.

  1. for(int j=1;j<=num;j++)- for checking if a number is prime or not using bruteforce we divide the number by 2…n-1, so correct syntax would be for(int j = 2;j<=num-1;j++)
  2. if c>0 which means the number is not prime
  3. if c == 0 then you’re suppose to print ‘yes’ else ‘no’ (needs to be in lowercase)

Few corrections in your code: https://www.codechef.com/viewsolution/23411470

Hi @mampy_21
Here I have fixed your code.
Your logic is correct. The only mistake you had was that you printed Yes instead of yes. The tester is case sensitive and thus it gave you WA.

Please try to format your code properly before posting or better, provide a link to your submission in future. People don’t like to read poorly formatted code. This will help you get your doubt get answered here faster.

Hope this helped :slight_smile:

I agree with your logic sir, but I have checked the number of factors for a number and if they are two, then it will be prime and if not, it won’t be a prime number. Is my logic wrong?

thank you sir

1 Like