一个验证质数的小程序
// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "math.h"
//验证x是否为质数
int main()
{
int x, A=2, B=2, FLAG; //A是第一个试验因数,B是第二个试验因数
printf("The number: ");
scanf_s("%d",&x); //输入需验证数字
while ( (A * B) <= x ) //这层用来改变第一个试验因数
{
while ( (A * B) <= x ); //这层用来改变第二个试验因数
{
if ( (A*B) == x )
{
FLAG = 0;goto END;
}
else if ( (A*B) < x )
B = ( B + 1 );
else
goto NEXT;
}
NEXT:
A = (A + 1) ; B = A;
}
END:
if (FLAG = 0)
{
printf("It's not a prime number/n");
printf("The factors that had been found are:%d AND %d", A, B);
}
else
printf("It's a prime number");
return 0;
}