Advertisement
AldiDwik

Tugas 3 punya aldii

Dec 9th, 2020
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Aldidwik_CekPrimaRekursif {
  4. static int n;
  5. static int cekPrima(int p){
  6. if(p==1){
  7. return 1;
  8.  
  9. }else if(n%p==0){
  10. return 0;
  11. }else{
  12. return cekPrima(p-1);
  13. }
  14. }
  15. public static void main(String[] args) {
  16. Scanner input=new Scanner(System.in);
  17. System.out.print("Masukkan nilai n : ");
  18. n=input.nextInt();
  19. if(n>1){
  20. int p = cekPrima(n-1);
  21. if(p==1){
  22. System.out.println(n + " adalah bilangan prima");
  23. }else{
  24. System.out.println(n +" bukan bilangan prima");
  25. }
  26. }else{
  27. System.out.println(n+" bukan bilangan prima");
  28. }
  29. }
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement