Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Program to find prime factors of any number
- Example:
- number = 6
- Prime Factors=2,3
- 2*3=6
- number =15
- Prime Factors=3,5
- 3*5=15
- number=45
- Prime Factors=3,3,5
- 3*3*5
- 9*5
- 45
- */
- import java.util.Scanner;
- class PrimeFactors
- {
- public static void main(String args[])
- {
- Scanner sc=new Scanner(System.in);
- System.out.println("Enter a number");
- int n=sc.nextInt();
- primeFactors(n);
- }
- void static primeFactors(int num)
- {
- int i;
- while(num!=1)
- {
- for(i=2;;i++)
- {
- if((num%i)==0)
- break;
- }
- System.out.println(i);
- num=num/i;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment