Aviral Mishra
Aviral Mishra

Reputation: 13

Code which prints YES if the given number is prime and NO if it is not in C

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.

Upvotes: 0

Views: 83

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

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++)
        {

Upvotes: 1

Related Questions