0

Question

  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.

Input Format A number t A number n A number n .. t number of times

Output Format prime not prime not prime .. t number of times

Constraints 1 <= t <= 10000 2 <= n < 10^9

Sample Input 5 19 21 33 37 121

Sample Output prime not prime not prime prime not prime

What am I doing wrong? The program is takin inputs alright, but the result is varying again and again. The output is right for some inputs but wrong for some other? What am I missing??


    import java.util.*;
    
    public class Main {
    
        public static void main(String[] args) {
            Scanner scn = new Scanner(System.in);
    
            boolean flag = true;
            int t = scn.nextInt();
            for (int i = 0; i < t; i++) {
                int n = scn.nextInt();
                for (int k = 2; k * k <= n; k++) {
                    if (n % k == 0) {
                        System.out.println("not prime");
                        flag = false;
                        break;
                    }
                }
                if (flag)
                    System.out.println("prime");
            }
        }
    }

  • Guess your nextInt doesn't take the good value, print the value after the nextInt to be sure the code use the one you think – azro Sep 22 '20 at 17:57
  • If it doesn't work, replace all the nextint by `Integer.parseInt(scn.nextLine())` (i'd suggest to use that 100% of time to avoid any problem – azro Sep 22 '20 at 17:57
  • Hi, welcome to SO. In order to improve the chances of getting help here I may suggest you a few small things if it's okay with you. First, I'd suggest editing the title for the question so it's short and more objective right to the point for you're trying to achieve, like 'How do I find if a number is prime' or something like this. – Francislainy Campos Sep 22 '20 at 17:59
  • Also, when you say the answer is wrong, better to try and be more clear about what exactly do you mean by that, what do you get specifically. – Francislainy Campos Sep 22 '20 at 18:00
  • @Makoto OP example doesn't contain any `nextLine()` call. Are you sure this duplicate is correct here? – Pshemo Sep 22 '20 at 18:02
  • @Pshemo: That's *exactly* why the dupe is right - because it *doesn't* clear the line. The dupe will answer this question. – Makoto Sep 22 '20 at 18:05
  • @Makoto Are you sure that that is the problem and not the fact that `flag` is never reset? – Pshemo Sep 22 '20 at 18:05
  • I'm reasonably confident. If the OP has an issue with the dupe closure, they can edit their post to clarify. – Makoto Sep 22 '20 at 18:06
  • @Makoto I realize that this duplicate is very often correct for most Scanner related problems but I am afraid this may not be the case. Notice that problem description states that "answer is wrong for some inputs". So OP claims it is about *result* not *input* (anyway `nextInt` would work regardless of `nextLine` being called or not). Problem here is that `flag` variable is not reset to true for each `n` and will forever become `false` after first non-primal number preventing primal numbers from printing correct message. – Pshemo Sep 22 '20 at 18:18
  • @TheDracula Move `boolean flag = true;` inside `for (int i = 0; i < t; i++)` loop. – Pshemo Sep 22 '20 at 18:26
  • If someone is wondering why I am not voting to reopen despite not agreeing with duplicate selection it is because question is at level of typographical error which is unique enough to not be able to help anyone in the future so it still is off-topic for this site (and I am not sure if at this time more people will gather to cast their close-votes after question will be reopened). – Pshemo Sep 22 '20 at 18:40
  • If you want help with the question, please give us the input, the output, and the expected output. Right now, if I compiled your program, I couldn't say I debugged it, because I don't know your input. – NomadMaker Sep 22 '20 at 18:52
  • When you start checking for a new prime, you don't reset flag to true. – NomadMaker Sep 22 '20 at 18:57
  • Nomad and Pshemo, u were right. The problem was with the position of my flag statement boolean flag = true; It was supposed to be inside for (int i = 0; i < t; i++) loop instead of outside. I corrected it and my code worked fully functionally. @FrancislainyCampos It was my first time here. I didn't know any better. But I heard u now, and I will be more accurate and precise while asking questions in the future.. Thanku everyone who commented for making me understand. – TheDracula Sep 23 '20 at 18:32
  • No worries! It takes a bit of time to get to know the best practices here on this platform but little by little you'll get used to it. – Francislainy Campos Sep 24 '20 at 07:24

0 Answers0