Day 25: Running Time and Complexity
For Explanation Check Video:
Sample Input
3
12
5
7
Sample Output
Not prime
Prime
Prime
Code:
import java.io.*;import java.util.*;
public class Solution {
public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while(t-->0){ int n = scn.nextInt(); if(n==1 || n<1){ System.out.println("Not prime"); }else{ boolean prime = true; for(int i=2;i*i<=n;i++){ if(n%i==0){ prime = false; break; } } if(prime){ System.out.println("Prime"); }else{ System.out.println("Not prime"); } } } }}
Comments
Post a Comment