adspace


Write a program in C# for checking a given number is PRIME or not.

Answer Posted / Jitendra Kumar Paswan

Here's a simple C# program to check if a number is prime:

using System;

namespace PrimeNumberChecker
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a number: ");
int num = Convert.ToInt32(Console.ReadLine());

if (IsPrime(num))
Console.WriteLine(num + " is prime");
else
Console.WriteLine(num + " is not prime");
}

static bool IsPrime(int num)
{
if (num <= 1) return false;
for (int i = 2; i < Math.Sqrt(num); i++)
if (num % i == 0) return false;
return true;
}
}
}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How to assign Null value to Var?

1070


What is an abstract class c#?

973


How do you inherit a class into other class in c#?

999


Which namespaces are necessary to create a localized application?

1147


Why can't we use a static class instead of singleton?

961


What is expression tree in c#?

1004