15,920,704 members
See more:
Dear sir !
I've written code for checking given number whether it is prime or not.

Here 's the code:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace prime
{
class Program
{
static void Main(string[] args)
{
int i, m;

for (m = 0; m<=4;m++)
{
Console.WriteLine("Enter the number\n");

if ((i / 2)!= 0)
{
Console.WriteLine(i + "\t is  prime");

continue;
}
else
{
Console.WriteLine(i+"\t is not prime");

}
}
}
}
}
But this is not generating desired output
when I enter 4 it shows that 4 is prime .

Second version
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace prime
{
class Program
{
static void Main(string[] args)
{
int i , m;

for (m = 2; m<=i-1; m++)
{
Console.WriteLine("Enter the number\n");

if ((i %m ) == 0)
{
Console.WriteLine(i + "{0} is not prime \n");
Console.WriteLine();
}
else if(m==i)
{
Console.WriteLine(i + "\t is prime");
}
}

Console.Clear();
}

}
}

but it says tat variable i is not assigned
Posted
Updated 4-Aug-15 6:41am
v3
Ralf Meier 3-Aug-15 6:37am
You should use the MOD-Command.
If myNumber MOD 2 equal 0 then myNumber is dividable through 2.
But that does not say, that you have no Prime. You must also check 3 and 5 and 7 and so on.
And ... btw. 1 is also a Prime ...
Maciej Los 3-Aug-15 6:38am
DamithSL 3-Aug-15 6:39am
Matt T Heffron 4-Aug-15 13:07pm
Second version:
on this line:
for (m = 2; m<=i-1; m++)
the variable i is used but has not yet been assigned a value.
How many times so you expect the for loop to execute?

## Solution 1

Except the fact that your algorithm is totally wrong...
4 / 2 != 0 (4 / 2 = 2) so 4 is a prime (according to your code)!!!

## Solution 2

4 / 2 is 2 which is not 0 so your code is saying it is prime. Your code seems to be checking for odd\even rather than prime. There are mathematicians who have dedicated their lives to working out if numbers are prime, and (as for as I know) no solution has yet been found.

http://www.wikihow.com/Check-if-a-Number-Is-Prime[^]

## Solution 3

First, lets review
C#
if ((i / 2)!= 0)
{
Console.WriteLine(i + "\t is prime");

continue;

}

If i is 4 and you divide it by 2, you get 2 which is not 0 and hence you get into the if part of the condition.

Second, if you need to know whether your number is a prime number or not, your logic needs to be different.
You cannot divide by just 2 and find out. 9 for e.g. is not divisible by 2 but still not a prime number.
Prime numbers - https://en.wikipedia.org/wiki/Prime_number[^].

tusharkaushik 3-Aug-15 7:06am
when I check with 4 numbers it raises an exception :System.FormatException was unhandled
Message="Input string was not in a correct format."
Source="mscorlib"
StackTrace:
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Convert.ToInt32(String value)
at prime.Program.Main(String[] args) in C:\Users\Admin\Documents\Visual Studio 2008\Projects\prime\prime\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
InnerException:
tusharkaushik 3-Aug-15 7:23am
but it shows 2 is not prime

## Solution 4

If your question is "why don't I get the desired output" ?
The answer is "because a simple division by 2 have never been ennough to say if a number is prime or not".

- google is your friend too. you will find numerous algorithms that say if a number is prime or not.

v2
tusharkaushik 4-Aug-15 8:53am
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace prime
{
class Program
{
static void Main(string[] args)
{
int i , m;

for (m = 2; m<=i-1

; m++)
{
Console.WriteLine("Enter the number\n");

if ((i %m ) == 0)
{
Console.WriteLine(i + "{0} is not prime \n");
Console.WriteLine();

}
else if(m==i)
{
Console.WriteLine(i + "\t is prime");

}
}

Console.Clear();
}

}
}

but it says tat variable i is not assigned

## Solution 5

Hi,

C#
int i , m;
bool isPrime = true;
Console.WriteLine("Enter the number\n");

for (m = 2; m < i/2; m++)
if ((i % m ) == 0)
{
isPrime = false;
break;
}

if(isPrime == true)
Console.WriteLine(i + "{0} is prime \n");
else
Console.WriteLine(i + "{0} is not prime \n");

Console.ReadKey();

v2

## Solution 6

Version 2
The compiler tells you that you try to use "i" before giving it a value.
Advice: look at your code search where you try to use "i" and search where you give a value to "i".
You should easily find what is wrong.
As I said earlier, use the debugger and see what your code is doing, it is a great tool to see what is wrong.

Comment: your code is still very wrong, but at least you try to code.