current position:Home>Java classic programming 50 questions sequel

Java classic programming 50 questions sequel

2022-01-26 22:58:08 #HashMap#

[ Procedure four ] subject : Divide a positive integer into prime factors . for example : Input 90, Print out 90=2 * 3 * 3 * 5
public class TestDecomposition {
    
	public static void main(String[] args) {
    
		System.out.print(" Please enter a number :");
		Scanner scanner = new Scanner(System.in);
		int num = scanner.nextInt();// Enter numbers on the keyboard 
		System.out.print("num=");
		for(int i = 2;i<=num;i++){
    // from 2-num retrieval 
			while(num%i==0&&num!=i){
    // If it can be divisible and is not equal to i Then meet the requirements 
				num = num/i;
				System.out.print(i+"*");// Output 
			}
			if(num==i){
    // In the first case, if it is a prime number, it will directly output , The second case is to output the final factor 
				System.out.println(i);
				break;
			}
		}
	}
}

copyright notice
author[#HashMap#],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262258059837.html

Random recommended