先判断一个数字是不是合数,如果是对其进行质因数分解
#include<stdio.h>
int prime(int x)
{
int a = 1;
for (a = 2; a <= x-1; a++)
{
if (x % a == 0)
return 0;//如果是合数返回0
}
return 1;//不是合数返回1
}
void resolve(int x)
{
int y = 0, z = 0;
for (int a = 2; a < x; a++)
{
while (x % a == 0)
{
printf("%d * ", a);
x = x / a;
}
}
if(x>1)
printf("%d", x);
}
int main()
{
int m = 0, last= 0;
printf("Input m:");
scanf_s("%d", &m);
last = prime(m);
if (last)//如果是素数
{
printf("It is a prime number\n");
}
else
{
printf("%d = ", m);
resolve(m);
}
return 0;
}