Output shows "not prime" regardless of the input given

Question is as follows:

  1. You’ve to check whether a given number is prime or not.
  2. Take a number “t” as input representing count of input numbers to be tested.
  3. Take a number “n” as input “t” number of times.
  4. For each input value of n, print “prime” if the number is prime and “not prime” otherwise.

This is my code:

The output is showing “not prime” regardless of the no. What’s the mistake?

hi @yanshigoyal1221_811b6a2e867b2070
Run the inner loop till j < n
Also u haven’t taken the input of n…
It should work fine…

@yanshigoyal1221_811b6a2e867b2070


corrected code… it’s working fine now… if there is still anything do let me know…

I noticed that when I write cin>>n inside the first for loop, it shows correct output. but upon writing cin>>n before starting the loop ( I mean in the first few lines where the n is initialized), it gives the wrong output. Why is it so?

just bcoz u have to take input of n for every test case (ie t times)… but when u take input of n outside the for loop, then u are taking input only once… hence it will work correct only for one test case… for remaining t-1 test cases it will give wrong ans…
Hope it clears your doubt…

@yanshigoyal1221_811b6a2e867b2070
I hope it’s clear now???

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.