分解质因数的C语言怎么写?
来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/08/23 17:28:42
分解质因数的C语言怎么写?
#include
#include
void analyze(int n)
{
int a[20];
int count = 0;
int i;
int number = n;
while(1)
{
for(i = 2; i < (int)sqrt(n); i++)
{
if(number % i == 0)
{
a[count] = i;
count++;
number = number / i;
break;
}
}
if(number == 1)
{
break;
}
}
printf("%d = ",n);
for(i = 0; i < count; i++)
{
printf("%d * ",a[i]);
}
printf("\b\b");
}
void main()
{
int number;
printf("please input a number:");
scanf("%d",&number);
analyze(number);
}
#include
void analyze(int n)
{
int a[20];
int count = 0;
int i;
int number = n;
while(1)
{
for(i = 2; i < (int)sqrt(n); i++)
{
if(number % i == 0)
{
a[count] = i;
count++;
number = number / i;
break;
}
}
if(number == 1)
{
break;
}
}
printf("%d = ",n);
for(i = 0; i < count; i++)
{
printf("%d * ",a[i]);
}
printf("\b\b");
}
void main()
{
int number;
printf("please input a number:");
scanf("%d",&number);
analyze(number);
}