Bootstrap

在一个正整数范围,判断是否为质数,找出所有因数并求个数

import java.util.Scanner;
//在一个正整数范围内,判断是否为质数,找出所有因数并求个数

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int count = 2;
        System.out.println("该数可被整除的数为" + 1);
        System.out.println("该数可被整除的数为" + num);
        for (int i = 2; i <=(int) Math.sqrt(num); i++) {
            if(num % i == 0) {
                if( i == num/i ){
                    System.out.println("该数可被整除的数为" + i);
                    count++;
                }
                else {
                    System.out.println("该数可被整除的数为" + i);
                    System.out.println("该数可被整除的数为" + num / i);
                    count +=2;
                }
            }
        }
        if(count == 2){
            System.out.println("该数为质数");
        }
        else{System.out.println("该数不为质数");
            System.out.println("因数个数为"+count);
        }
    }
}
;