将一个正整数分解质因数。例如输入90,输出90=2*3*3*5。

#include 
#include 
#include 

int main(){
	int i,n,j,count=0;
	int *A,m=0;

	printf_s("enter(n>=2):\n");
	scanf_s("%d", &n);
	//
	for (i = 2; i <= n; i++){
		for (j = 2; j <= sqrt(i); j++){
			if (i%j == 0) break;
		}
		if (j > sqrt(i)) count++;
		
	}
	A = (int*)malloc(sizeof(int)*count);

	for (i = 2; i <= n; i++){
		for (j = 2; j <= sqrt(i); j++){
			if (i%j == 0) break;
		}
		if (j > sqrt(i)){
			A[m] = i;
			m++;
		}

	}

	printf_s("%d=", n);
	i = 0;
	do{
			while(n%A[i] == 0) {
				n = n / A[i];
				printf_s("%d", A[i]);
				if (n>1)printf_s("*");
		}
			i++;
	} while (n!=1);

	printf_s("\n");
	return 0;
}

你可能感兴趣的:(C)