Pages

Friday, August 7, 2015

Prime Factor of integers

The prime factors of 13195 are 5, 7, 13 and 29.

Input: 13195

Ouput : 5,7,13,29


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
     public static void main(String []args){
         int n = 13195;
         
         isPrimeFactor(n);
         
     }
     
     public static void isPrimeFactor(int n){
         
         int count=0;
         ArrayList arr = new ArrayList();
         for(int x=2;x<=n;x++){
            if(n%x==0){
               arr.add(x);
               
               n = n/x;
               x--;
               count++;      
            }
         }
         
         System.out.print(arr.toString());
     }

No comments:

Post a Comment