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 a multicast c# delegate?

739


What is difference between int and int in c#?

676


Explain concurrency with aop?

758


What is .dbml file?

699


Expalin the way you implement inheritance by using VB.NET/C#?

757


From which base class do all Web Forms inherit from?

734


What is type system in c#?

718


What are controls in c#?

712


Who is a accessibility modifier “protected internal” available to ?

800


What is an interface class in c#?

766


What is autopostback in c#?

702


What is console writeline in c#?

667


what is the index value of the first element in an array?

708


What is the main method?

714


Why do we use namespace in c#?

720