Skip to main content

Prime Number Program in Java: Is Number Prime Or Not

Question


  • You've to check whether a given number is prime or not.
  • Take a number "t" as input representing a count of input numbers to be tested.
  • Take a number "n" as input "t" number of times.
  • 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

Program

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

Popular posts from this blog

Tic Tac Toe Python 3: The Standard Tic-Tac-Toe Game in Python 3

I just finished Tic tac toe game as my first python project. This is a very simple classic game, for beginners in programming tic-tac-toe game is the best choice. I am using Jupyter notebook, you can use any python ide like Pycharm, Spyder etc. Scenario of tic-tac-toe game: Your task is to write a simple program which pretends to play  Tic-tac-toe game with the user.  To make it easier for you. We have decided to simplify the game. Here are our assumptions. the computer should play a game using 'X' the user should play a game using 'O' the first move belongs to the computer, it always 'X' in the middle of the board.  all squares are numbered row by row, start with '1'. the user move is to enter the number of the square they choose, the number must be valid, that is it must be an integer, it must be greater than  0 and less than 10, and it cannot point to a field which is already occupied. the program checks the ga

Prime Number Program in Java: Print All Prime Numbers

Question You've to print all prime numbers between a range. Take as input "low", the lower limit of the range. Take as input "high", the higher limit of the range. For the range print all the primes numbers between low and high (both included). Input Format low high Output Format n1 n2 .. all primes between low and high (both included) Constraints 2 <= low < high < 10 ^ 6 Sample Input 6 24 Sample Output 7 11 13 17 19 23 Program import java.util.*; public class Main{ public static void main(String[] args) { // write your code here Scanner scn = new Scanner(System.in); int low = scn.nextInt(); int high = scn.nextInt(); for (int i = low; i <= high; i++){ int count = 0; //try to divide n and incrtease count for (int div = 2; div * div <= i; div++){ if ( i % div == 0){ count++;