The eroor is :Exception Unhadled :System.DivideByZeroException: 'Attempted to divide by zero.'And this is the code :
Console.Write("Enter the number: ");
int A =Convert.ToInt32(Console.ReadLine());
bool flag = false;
if (A < 2)
{
Console.WriteLine($"{A} is a prime number!! ");
}
for (int i = 0; i < A; i )
{
if (A % i == 0)
{
flag = true; break;
}
}
if (flag == true)
{
Console.WriteLine($"{A} is not a prime number!!");
}
else
{
Console.WriteLine($"{A} is a prime number !!");
}
Console.ReadKey();
CodePudding user response:
A % 0 will generate a DivideByZeroException. Start your loop with i = 1.
CodePudding user response:
First A % 0 will report an exception, set i=0 to i=1, if you want to get specific exception information, wrap it in the "try...catch" code block
try{
for (int i = 0; i < A; i )
{
if (A % i == 0)
{
flag = true; break;
}
}
}catch(Exception ex){
Console.WriteLine("Exception caught: {0}", ex);
}