# Prime Number Calculator • ### Question

• I've just started programming and I tried making a Prime Number Calculator. I've googled some of the problems I've had until I was able to run the program.  When the program is running I am able to input a number. The problem is that the program is not working as intended. It is supposed to tell me if a number is a prime number or not. But sometimes there is no answer or an incorrect answer. With my knowledge I can't solve the problem. So I wonder if there is someone who can tell me what is wrong.

```using System;

namespace test2
{
class MainClass
{
public static void Main (string[] args)
{
int x;
double z = Math.Sqrt(x);
int y = Convert.ToInt32(z);
if (x > 1 & y > 1)
{
if (x % y == 0)
{
x = x - x;
}
if (x % y != 0)
{
y = y - 1;
}
if  (x == 0)
{
Console.WriteLine("Your number is not a prime number!");
x = x + 1;
}
if (y == 1)
{
Console.WriteLine("Your number is a prime number!");
y = y - 1;
}

}
}
}
}```

Saturday, August 29, 2015 1:38 PM

• Hi JonathanSMB,

As you had just started programming I would suggest you should learn about the code Debugging feature in Visual Studio, how to debug your code. So that way you will get more clear concept and understanding about coding. There's a lot of tutorial on the internet and in MSDN.

Coming to your problem I tried to run your code and found that the decision you are giving whether the number is prime or not is by using the below code block (from your code),

```if  (x == 0)
{
Console.WriteLine("Your number is not a prime number!");
x = x + 1;
}
if (y == 1)
{
Console.WriteLine("Your number is a prime number!");
y = y - 1;
}```

So if x=0 or y=1 there will be an output on the screen, when I debug the code I found that there's some time for the number you are checking for prime or not there is a output neither x=o nor y=1 so, for that case no condition is given that's why there is no output and also in the first "if" checking section there's an "&" missing,

```int x;
double z = Math.Sqrt(x);
int y = Convert.ToInt32(z);

if (x > 1 && y > 1)```

So the code look like this,

```using System;

namespace test2
{
class MainClass
{
public static void Main (string[] args)
{
int x;
double z = Math.Sqrt(x);
int y = Convert.ToInt32(z);

if (x > 1 && y > 1)
{
if (x % y == 0)
{
x = x - x;
}
if (x % y != 0)
{
y = y - 1;
}
if  (x == 0)
{
Console.WriteLine("Your number is not a prime number!");
x = x + 1;
}
if (y == 1)
{
Console.WriteLine("Your number is a prime number!");
y = y - 1;
}

}
}
}
}```

And also there's ton of example of prime number on the internet so you could look into that and make your code more efficient. Hope this might help you.

Thanks,

Sabah Shariq

Saturday, August 29, 2015 5:00 PM
• I wrote somethng about the 5 ways to check for prime numbers:

First of, I would turn the check function into a function that returns bool. Seperate processing and output, it makes understanding issues much easier.

Secondly I don't get what you are doing there:
You are doing something with the Square Root?
Where is the loop that brute forces all the odd numbers/prime numbers? I don't think this code could eve give you reliable results.

Saturday, August 29, 2015 2:00 PM
• You can do it with this code:

```	 public bool IsPrime(int number)
{
for (int i = 3; (i * i) <= number; i += 2)
{
if ((number % i) == 0)
{
return false;
}
}
return number != 1;
}```

Saturday, August 29, 2015 3:19 PM
• Hi JonathanSMB,

In the General area, for a positive integer n, if all integers between 2 to remove are not divisible, then n is prime.
Prime number greater than or equal to 2 not divisible by the number other than itself and 1.
I wrote the following code:

```using System;

namespace test2
{
class MainClass
{
public static void Main (string[] args)
{
int x;
double z = Math.Sqrt(x);
int y = Convert.ToInt32(z);
if (x > 1 && y > 1)
{
int i;
for(i=2;i<= y;i++)
{
if(x%i==0)
{
break;
}
}
if  (i < y)
{
Console.WriteLine("Your number is not a prime number!");
}
if (i > y)
{
Console.WriteLine("Your number is a prime number!");
}

}
}
}
}```

Best Regards,

Ann

Monday, August 31, 2015 7:00 AM

### All replies

• I wrote somethng about the 5 ways to check for prime numbers:

First of, I would turn the check function into a function that returns bool. Seperate processing and output, it makes understanding issues much easier.

Secondly I don't get what you are doing there:
You are doing something with the Square Root?
Where is the loop that brute forces all the odd numbers/prime numbers? I don't think this code could eve give you reliable results.

Saturday, August 29, 2015 2:00 PM
• You can do it with this code:

```	 public bool IsPrime(int number)
{
for (int i = 3; (i * i) <= number; i += 2)
{
if ((number % i) == 0)
{
return false;
}
}
return number != 1;
}```

Saturday, August 29, 2015 3:19 PM
• The line:

```if (x > 1 & y > 1)
```

isn't doing what you think it is doing. Refer to your C# textbook to review what '&' does.

What you want is:

```if (x > 1 && y > 1)
```

Saturday, August 29, 2015 3:31 PM
• Hi JonathanSMB,

As you had just started programming I would suggest you should learn about the code Debugging feature in Visual Studio, how to debug your code. So that way you will get more clear concept and understanding about coding. There's a lot of tutorial on the internet and in MSDN.

Coming to your problem I tried to run your code and found that the decision you are giving whether the number is prime or not is by using the below code block (from your code),

```if  (x == 0)
{
Console.WriteLine("Your number is not a prime number!");
x = x + 1;
}
if (y == 1)
{
Console.WriteLine("Your number is a prime number!");
y = y - 1;
}```

So if x=0 or y=1 there will be an output on the screen, when I debug the code I found that there's some time for the number you are checking for prime or not there is a output neither x=o nor y=1 so, for that case no condition is given that's why there is no output and also in the first "if" checking section there's an "&" missing,

```int x;
double z = Math.Sqrt(x);
int y = Convert.ToInt32(z);

if (x > 1 && y > 1)```

So the code look like this,

```using System;

namespace test2
{
class MainClass
{
public static void Main (string[] args)
{
int x;
double z = Math.Sqrt(x);
int y = Convert.ToInt32(z);

if (x > 1 && y > 1)
{
if (x % y == 0)
{
x = x - x;
}
if (x % y != 0)
{
y = y - 1;
}
if  (x == 0)
{
Console.WriteLine("Your number is not a prime number!");
x = x + 1;
}
if (y == 1)
{
Console.WriteLine("Your number is a prime number!");
y = y - 1;
}

}
}
}
}```

And also there's ton of example of prime number on the internet so you could look into that and make your code more efficient. Hope this might help you.

Thanks,

Sabah Shariq

Saturday, August 29, 2015 5:00 PM
• Hi JonathanSMB,

In the General area, for a positive integer n, if all integers between 2 to remove are not divisible, then n is prime.
Prime number greater than or equal to 2 not divisible by the number other than itself and 1.
I wrote the following code:

```using System;

namespace test2
{
class MainClass
{
public static void Main (string[] args)
{
int x;
double z = Math.Sqrt(x);
int y = Convert.ToInt32(z);
if (x > 1 && y > 1)
{
int i;
for(i=2;i<= y;i++)
{
if(x%i==0)
{
break;
}
}
if  (i < y)
{
Console.WriteLine("Your number is not a prime number!");
}
if (i > y)
{
Console.WriteLine("Your number is a prime number!");
}

}
}
}
}```

Best Regards,

Ann

Monday, August 31, 2015 7:00 AM