Advertisement
Guest User

Rangga Prime

a guest
Nov 11th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. import java.util.Scanner;
  2. //rangga
  3. class PrimeNumberDemo
  4. {
  5. public static void main(String args[])
  6. {
  7. int n;
  8. int status = 1;
  9. int num = 3;
  10. Scanner scanner = new Scanner(System.in);
  11. System.out.println("Berapa bilangan prima : ");
  12. n = scanner.nextInt();
  13. if (n >= 1)
  14. {
  15. System.out.println(n+ "Urutan Bilangan Prima awal");
  16. System.out.println(2);
  17. }
  18.  
  19. for ( int i = 2 ; i <=n ; )
  20. {
  21. for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
  22. {
  23. if ( num%j == 0 )
  24. {
  25. status = 0;
  26. break;
  27. }
  28. }
  29. if ( status != 0 )
  30. {
  31. System.out.println(num);
  32. i++;
  33. }
  34. status = 1;
  35. num++;
  36. }
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement