-
Notifications
You must be signed in to change notification settings - Fork 2
/
25.1 Prime Factors.java
47 lines (38 loc) · 1.2 KB
/
25.1 Prime Factors.java
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
/*
Richa and her daughter Ahaana are playing a game. Richa is going to tell one number and Ahaana need to tell the prime factors of the number. Help Ahaana by completing the code to find prime factor of the number. Write a method which calculate prime factors and print and call the method in main.
Input Format
An integer value
Constraints
N will be lie between 10-50
Output Format
All the prime factors will be printed exectly once with space.
*/
import java.io.*;
import java.util.*;
public class Solution {
static void primeFactor(int x)
{
if(x<=1)
return;
for(int i=2;i*i<=x;i++)
{
if(x%i==0)
{
System.out.print(i+" ");
while(x%i==0)
x /= i;
}
}
if(x>1)
System.out.print(x);
}
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n>=10 && n<=50)
primeFactor(n);
else
System.out.print("Invalid Input");
}
}