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();


}
}
}

Answers were Sorted based on User's Feedback



hi, I am a begineer to c sharp. I have written a code for finding out prime numbers. Can anyone i..

Answer / renuka thakur

Hi im Renu , once check out this


class Program
{
static void Main(string[] args)
{
int a,b=1;
Console.WriteLine("enter a number: ");
a = int.Parse(Console.ReadLine());

if (a % b == 0 && a % 2 != 0 && a % a == 0)
{
Console.WriteLine(a + " is a prime number");
}
else
if (a % b == 0 && a % 2 == 0)
{
Console.WriteLine(a % 2);
Console.WriteLine("Number is notPRIME");
}
Console.ReadLine();

}
}

Is This Answer Correct ?    1 Yes 0 No

hi, I am a begineer to c sharp. I have written a code for finding out prime numbers. Can anyone i..

Answer / 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

More C Sharp Interview Questions

What is Implementation inheritance

0 Answers  


How do you specify a custom attribute for the entire assembly (rather than for a class)?

0 Answers  


What is parse method in c#?

0 Answers  


What are the basics of c#?

0 Answers  


What is difference between C# and VB.NET?

0 Answers   CDC,






How do you inherit from a class in C#?

1 Answers  


What is stringwriter c#?

0 Answers  


Explain the difference between “system.array.clone()” and “system.array.copyto()” in c#?

0 Answers  


What is collection class c#?

0 Answers  


What is the purpose of escape sequence?

0 Answers  


In which way you can convert a value-type to a reference-type?

0 Answers   Siebel,


Does main have to be static c#?

0 Answers  


Categories