java判斷輸入的數是不是素數


 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 //判斷輸入的數是不是素數
 6 public class Test18 {
 7     public static void main(String[] args) {
 8         Scanner s = new Scanner(System.in);
 9         System.out.println("輸入判斷的數");
10         int a = s.nextInt();        
11         for(int i=2 ; i<=a;i++){    //最小的素數是2
12             if(a % i == 0&& i != a){   //除了1和本身之后沒有約數即為素數
13             System.out.println("不是素數");
14             break;
15             }else if(i == a){
16             
17             System.out.println("是素數");
18         
19             }
20         }
21     }
22 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM