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
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 |
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 |
What is the meaning of int parse in c#?
What is the use of base keyword? Tell me a practical example for base keyword’s usage?
Using system; class main { public static void Main() { int a = 1; for (int i = 0; i < 10; i++) { int j = a * 5; Console.WriteLine(a + "*5=" + j); a++; } Console.ReadLine(); } }
Explain the OOPS concept in C#?
What is the use of generics in c#?
Can we override main method in c#?
What is the purpose of c#?
What is c# most used for?
In a site to turn off cookies for one page which method is followed?
What is Interface Polymorphism?with E.g
What is form feed in c#?
Describe an abstract class?