Advertisement
setiadi

bilangan prima

Aug 25th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.63 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Prima {
  4.  
  5.     public static void main(String[] args) {
  6.         // TODO Auto-generated method stub
  7.        
  8.         System.out.println("Masukkan angka");
  9.         Scanner sc = new Scanner(System.in);
  10.         int n = sc.nextInt();
  11.        
  12.        
  13.         for (int i = 1; i <= n; i++) {
  14.             Boolean prima = isPrime(i);
  15.            
  16.             if(prima) {
  17.                 System.out.print(i+" ");
  18.             }
  19.         }
  20.        
  21.        
  22.     }
  23.    
  24.      static boolean isPrime(int n) {       
  25.          
  26.          int jumlah = 0;
  27.          
  28.          for(int i = 1; i <= n; i++) {
  29.  
  30.              if(n%i == 0){
  31.                  jumlah++;
  32.              }
  33.          }
  34.          
  35.         if(jumlah == 2 ) {
  36.              return true;
  37.          }else {
  38.              return false;
  39.          }
  40.          
  41.     }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement