Hi I'm new to coding on C# and thought I should try to make Prime number checker, where you would input a number and it would checked to see if it was prime or not. I wanted to try and use functions but came across some issues and I am bit confused. Also I believe my logic is also wrong but I'm not sure how to fix it. Here is my code any help is appreciated, also could you explain each part of your logic if you fix it:
``internal class Program
{
static void Main(string[] args)
{
int number = Convert.ToInt32(Console.ReadLine());
IsNumberPrime();
Console.ReadKey();
}
static int IsNumberPrime(int number)
{
if (number <= 0)
{
Console.WriteLine(number " is " "not Prime");
}
else if (number == 1)
{
Console.WriteLine(number " is " "not Prime");
}
else if (number > 1)
{
for (int a = 2; number % a == 0; a )
{
if (number % a == 0)
{
Console.WriteLine(number " is " "not Prime");
}
else
{
Console.WriteLine(number " is " " Prime");
}
}
}
}
}`
`
CodePudding user response:
There are several issues in your code, it's completely wrong.
first I will focus on the logical problems in your code.
you are not using the return value, so you can change the return type to void
.
you should pass number
as a parameter to the method IsNumberPrime
.
you should modify the condition in the for a loop.
you should move the Console.WriteLine(number " is Prime");
outside the for
loop.
static void IsNumberPrime(int number)
{
if (number <= 0)
{
Console.WriteLine(number " is not Prime");
}
else if (number == 1)
{
Console.WriteLine(number " is not Prime");
}
else if (number > 1)
{
for (int a = 2; number > a; a )
{
if (number % a == 0)
{
Console.WriteLine(number " is not Prime");
return;
}
}
Console.WriteLine(number " is Prime");
}
}
Now comes to the performance, you can run the loop till the square root of the number, not till the number, which will improve the performance of the code. also, no need to increment the value of a
by 1, you can increment by 2 (first check divide by 2 and then just check divide by the odd numbers, no need to check with the other even numbers).
Here is the optimized code.
static void IsNumberPrime(int number)
{
if (number <= 1)
{
Console.WriteLine(number " is " "not Prime");
return;
}
if (number == 2)
{
Console.WriteLine(number " is " " Prime");
return;
}
if (number % 2 == 0)
{
Console.WriteLine(number " is " "not Prime");
return;
}
for (int i = 3; i <= Math.Sqrt(number); i = i 2)
{
if (number % i == 0)
{
Console.WriteLine(number " is " "not Prime");
return;
}
}
Console.WriteLine(number " is " " Prime");
}
CodePudding user response:
My comments will make your solution run, I didn't check the algorithm for Primes. Your Main function is calling IsNumberPrime() function which expects an integer value in its parameter. Another remark is that the IsNumberPrime returns an integer value. Because you’re logging the results to the console, you should change the return type in the method declaration to void. Below are my adjustments to your code:
static void Main(string[] args)
{
int number = Convert.ToInt32(Console.ReadLine());
IsNumberPrime(number);
Console.ReadKey();
}
static void IsNumberPrime(int number)
{
if (number <= 0)
{
Console.WriteLine(number " is " "not Prime");
}
else if (number == 1)
{
Console.WriteLine(number " is " "not Prime");
}
else if (number > 1)
{
for (int a = 2; number % a == 0; a )
{
if (number % a == 0)
{
Console.WriteLine(number " is " "not Prime");
}
else
{
Console.WriteLine(number " is " " Prime");
}
}
}
}