Day 25: Running Time and Complexity

  • + 0 comments

    Java Solution

     Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            for(int j=n;j>0;j--)
            {
                int a = sc.nextInt();
                boolean pr = true;
                if(a==1)
                {
                    System.out.println("Not prime");
                    continue;
                }
                for(int i=2;i<=a/2;i++)
                {
                    if(a%i==0)
                    {
                        pr = false;
                        System.out.println("Not prime");
                        break;
                    }
                }
                if(pr==true)
                {
                        System.out.println("Prime");
                    
                }
            }