0

I have tried the following code

#include<stdio.h> int main(void) { int t,i, N, j,count=0; printf("t\n"); scanf("%d",&t); for(i=0;i<t;i++) { printf("N\n"); scanf("%d",&N); for(j=1;j<=N;j++) { if(N%j==0) { count++; } } if(count==2) { printf("YES\n"); } else { printf("NO\n"); } } return 0; } 

But it just prints the first test case correctly, for eg. I gave the first test case as 3 it Prints yes, and 7 as the second test case it prints NO. What's the issue I am not able to figure out with this code. Similarly, if I enter the first test case as 7 it prints YES, and enters 3 as the second case it prints NO.

2
  • 1
    Did you reset the variable count to zero after every input? Commented Jun 24, 2021 at 6:06
  • Yes, that was creating the error. Fixed that. Commented Jun 24, 2021 at 6:29

1 Answer 1

1

You forgot to initialize count for each query. Add initialization to fix.

 printf("N\n"); scanf("%d",&N); count=0; /* add initialization here */ for(j=1;j<=N;j++) { 
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, Thanks, it's working, but can you plz explain the purpose of initializing it, at that place particularly. I mean I initialized count=0 in beginning, why its not counted??
Initialization is for having it doing the computation from the beginning. Your initialization is done only once before reading the number of query and it won't be helpful to prevent queries from being influenced by previous query.
@AviralMishra You should start using a debugger as soon as possible. Step through your code and watch values of your variables. This should answer that question about "why" immediately.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.