-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathifPrime.java
More file actions
43 lines (35 loc) · 984 Bytes
/
ifPrime.java
File metadata and controls
43 lines (35 loc) · 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
# check if an input is prime or not
steps:
-> handle separately, if input is 1
-> assign a boolean variable ifPrime with true
-> run a for loop,
-> if input is divisible by (2 to root(input)), then, make ifPrime false & break loop
-> finally print the result
*/
import java.util.*;
public class Main
{
public static void main (String args [])
{
Scanner sc = new Scanner (System.in);
int num = sc.nextInt();
boolean ifPrime = true;
// if input is 1, which is nonprime
if (num == 1)
ifPrime = false;
// trying to divide input by 2 to root of input
for (int i = 2; i*i < num; i++)
{
if ((num % i) == 0)
{
ifPrime = false;
break;
}
}
if (ifPrime)
System.out.println ("Prime number");
else
System.out.println ("Composite number");
}
}