c语言编程:求一个数的质因子

Python014

c语言编程:求一个数的质因子,第1张

#include<stdio.h>

int main()

{

   int i, j, a[999999], x, y

   scanf("%d", &x)   //读取

   y = x

   for(i = 2, j = 0 i <= x i++)  //遍历质因数

   {

      if(x % i == 0)    //如果读取的数能够被质因数整除

      {

         a[j] = i   //将符合条件的质因数存到数组中

         j++        //数组下标递增

         x /= i     //重新赋值

         i = 2      //重新遍历

      }

   }

   printf("%d=%d", y, a[0])    //输出原数、等号、第一个质因数

   for(i = 1 i < j i++)

      printf("×%d", a[i])   //如果存在第二个质因数,则继续输出

   return 0

}

代码文本:

#include "stdio.h"

int IsPrime(int n){//素数判断

int i

if(n>2 &&!(n&1) || n<2)

return 0

for(i=3i*i<=ni+=2)

if(!(n%i))

return 0

return 1

}

int main(int argc,char *argv[]){

int n,i

printf("Enter n(int n>1 and not a prime)...\nn=")

if(scanf("%d",&n) &&n>1 &&!IsPrime(n)){

putchar('\n')

for(i=2i<=ni++)

if(IsPrime(i) &&n%i==0){

printf(i!=n ? "%d " : "%d\n",i)

n/=i

i=1

}

}

else

printf("Input error, exit...\n")

return 0

}