-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactor.java
More file actions
50 lines (45 loc) · 1.24 KB
/
Copy pathFactor.java
File metadata and controls
50 lines (45 loc) · 1.24 KB
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
44
45
46
47
48
49
50
//WAP to print Factors and prime factors of a number
import java.util.*;
public class Factor {
static boolean checkPrime(int n) //Function to check if prime
{
int ctr=0;
for(int i=1;i<=n/2;i++)
{
if(n%i==0) //Checking for divisibility
ctr++; //Increasing counter if divisible..
}
if(ctr==1)
return true;
else
return false;
}
static void printFactors(int n) //Function to print factors
{
for(int i=1;i<=n;i++)
{
if(n%i==0)
System.out.print(i+"\t");
}
}
static void printPFactors(int n) //Function to print Prime factors
{
for(int i=1;i<=n;i++)
{
if(n%i==0)
if(checkPrime(i)) //Checking for prime
System.out.print(i+"\t");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int num;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
num=sc.nextInt();
System.out.print("Factors of "+num+" are: ");
printFactors(num); //Calling printFactors()
System.out.print("\nPrime Factors of "+num+" are: ");
printPFactors(num); //Calling printPFactors()
}
}