hi,
I am a begineer to c sharp. I have written a code for
finding out prime numbers. Can anyone identify what are the
flaws in my code.
Kindly donot complex the code or present logic because i am
new to c sharp and just started learning programming
language.Thanks in advance.

class Program
{
static void Main(string[] args)
{
int a,b=1;
a = int.Parse(Console.ReadLine());
c= int.Parse(Console.ReadLine());
if (a % b == 0 && a % 2 != 0 && a % a == 0)

Console.WriteLine(a);
else if (a % b == 0 && a % 2 == 0)
Console.WriteLine(a%2);
Console.WriteLine("Number is not
PRIME");
Console.ReadLine();


}
}
}

Answer Posted / randheer

class Program
{
static void Main(string[] args)
{
int a;
int count = 0;
Console.WriteLine("please enter a number");
a = int.Parse(Console.ReadLine());
for (int i = 2; i <= a; i++)
{
int c = a%i;
if (c == 0)
count++;
}
if (count == 1)
{
Console.WriteLine(a + " :is aprime number");
}
else
{
Console.WriteLine(a+" :is not a prime number");
}
Console.ReadKey();
}
}

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is thread in c#?

742


What is difference between float and decimal?

730


What is difference between yielding and sleeping?

643


What is ispostback c#?

667


How many destructors can a class have?

796


What are properties in C#?

727


hi all.... i need code snippets for store and retrive tiff fromat images in sqlserver....... kindy provide it.......

1585


What is difference between ienumerable and list?

735


Why hashtable is thread safe in c#?

713


Can you store strings in arrays?

748


What is session variable in c#?

771


What is difference between arraylist and list in c#?

622


What are assemblies?

727


What is string literal in c#?

694


Is string a primitive data type in c#?

659