[此贴子已经被作者于2005-8-23 17:54:06编辑过]
[此贴子已经被作者于2005-8-23 17:54:06编辑过]
判断输入的数字是否为质数
using System;
public class Application
{
public static void Main()
{
string strNum;
Console.WriteLine("请输入一个正整数");
strNum = Console.ReadLine();
int num = int.Parse(strNum);
string result = "";
if(num % 2 == 0)
if(num != 2)
result = strNum + "不是质数";
else
result = strNum + "是质数";
else
{
bool ok = true;
for( int i=2; i <= num; i++)
{
if(i != num && num % i == 0)
{
result = strNum + "不是质数";
ok = false;
break;
}
if( i == num && ok )
result = strNum + "是质数";
}
}
Console.WriteLine(result);
}
}
判断输入的数字是否为质数
using System;
public class Application
{
public static void Main()
{
string strNum;
Console.WriteLine("请输入一个正整数");
strNum = Console.ReadLine();
int num = int.Parse(strNum);
string result = "";
if(num % 2 == 0)
if(num != 2)
result = strNum + "不是质数";
else
result = strNum + "是质数";
else
{
bool ok = true;
for( int i=2; i <= num; i++)
{
if(i != num && num % i == 0)
{
result = strNum + "不是质数";
ok = false;
break;
}
if( i == num && ok )
result = strNum + "是质数";
}
}
Console.WriteLine(result);
}
}
[此贴子已经被作者于2005-12-6 14:54:41编辑过]