Hi all,here is the task Write a program y = PrimeCheck (n) that inputs an integer n > 1, and outputs an integer y
that is 1 if n is a prime number and 0 if it is not. The method used should use a brute force
check to see whether n has any positive integer factor k, checking all values of k (if
necessary) up to floor(√n).
namespace IntPrime
{
class Program
{
static void Main(string[] args)
{
int y;
Console.WriteLine("Enter number to check whether it is Prime Number or Not:");
y = int.Parse(Console.ReadLine());
Program pr = new Program();
if (pr.PrimeCheck(y))
{
Console.WriteLine("Is a Prime Number");
}
else
{
Console.WriteLine("It is Not a Prime Number");
}
Console.ReadLine();
}
// Find given number is Prime or Not
private bool PrimeCheck(int n)
{
int c;
for (c = 2; c <= (n / 2); c++)
{
if (n % c == 0)
{
return false;
}
}
return true;
}
}
}
How to improve and implement floor function?
PulsarScript
0
Junior Poster
Recommended Answers
Jump to PostDo you mean the floor method from the Math class?
EDIT:
Almost forgot:use this as a for loop:int sq = Math.Round(Math.Sqrt(n)); for (c = 2; c <= sq; c++) …
Jump to PostI guess this should do the trick:
double floorIt(double x) { return x % 1 == 0 ? x : (int)x - (x < 0 ? 1 : 0); }
Jump to PostOh, and if you want to know why the square root: check here
All 9 Replies
ddanbe
2,724
Professional Procrastinator
Featured Poster
PulsarScript
0
Junior Poster
ddanbe
2,724
Professional Procrastinator
Featured Poster
ddanbe
2,724
Professional Procrastinator
Featured Poster
PulsarScript
0
Junior Poster
ddanbe
2,724
Professional Procrastinator
Featured Poster
PulsarScript
0
Junior Poster
ddanbe
2,724
Professional Procrastinator
Featured Poster
PulsarScript
0
Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.