Click here to Skip to main content
15,886,680 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
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");
                i = Convert.ToInt32(Console.ReadLine());

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

                    Console.ReadKey();
                }
            }
            Console.ReadKey();
        }
    }
}
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");
    i = Convert.ToInt32(Console.ReadLine());

    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.ReadKey(true);
        }
    }

Console.Clear();
Console.ReadKey();
}

}
}


but it says tat variable i is not assigned
Posted
Updated 4-Aug-15 6:41am
v3
Comments
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?

Except the fact that your algorithm is totally wrong...
4 / 2 != 0 (4 / 2 = 2) so 4 is a prime (according to your code)!!!
 
Share this answer
 
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[^]
 
Share this answer
 
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[^].
 
Share this answer
 
Comments
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.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
tusharkaushik 3-Aug-15 7:23am    
but it shows 2 is not prime
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".

- The debugger is your friend. it will help you to understand what is doing your code.

- google is your friend too. you will find numerous algorithms that say if a number is prime or not.
 
Share this answer
 
v2
Comments
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");
i = Convert.ToInt32(Console.ReadLine());


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.ReadKey(true);

}
}

Console.Clear();
Console.ReadKey();
}

}
}

but it says tat variable i is not assigned
Hi,

i think this code can help you:

C#
int i , m;
bool isPrime = true;
Console.WriteLine("Enter the number\n");
i = Convert.ToInt32(Console.ReadLine());
 
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();
 
Share this answer
 
v2
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900