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?

Recommended Answers

All 9 Replies

Do 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++) etc.

how to implement in a code?

I guess this should do the trick:

double floorIt(double x)
{
    return x % 1 == 0 ? x : (int)x - (x < 0 ? 1 : 0);
}

Oh, and if you want to know why the square root: check here

Hi .I am sorry for asking stupid question,but could you show me how to insert in my code
Math.Floor,so the program could round it down if decimal is entered.Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace prime_number
{
    class findPrime
    {

        long[] num = new long[6]; /*Creating array for numbers*/
        int i, j;
        bool isprime, noprime = true;
        public findPrime()
        {
            Console.WriteLine("Please enter 6 numbers in this order: number-enter,number-enter:");
            Console.WriteLine("30,31,487,8893,987654323,131317171919");
            for (i = 0; i < 6; i++)

                num[i] = Int64.Parse(Console.ReadLine());  /* add Numbers to the Array*/

            for (i = 0; i < 6; i++)
            {
                isprime = true; /* initialize Current number as prime Number is "true"*/
                for (j = 2; j <= (num[i] / 2); j++)
                /* for Diving numbers for Remainder "0" from 2 to the half of the Number*/
                {
                    if (num[i] % j == 0)
                    {
                        isprime = false;
                        /*if  Remainder "0" Found set Current Number as prime is "false" and break the loop */
                        break;
                    }
                }

                if (isprime) /*if  no Remainder "0"  Display current Number as Prime Number*/
                {
                    noprime = false;
                    Console.WriteLine(num[i] + " is a Prime Number");
                }
            }
            if (noprime)
                Console.WriteLine(num[i] + "There is no a Prime number in  the list");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            findPrime pn = new findPrime(); /* Create object of the class */
            Console.ReadLine();
        }
    }
}

Int64.Parse on line 22 will fail if you parse a non integer.
So, you should test before that, but why the floor function? Why not use truncate instead?
What happend to the primecheck method you had in your first post.
Do you not read my answers?
Why is it now integrated in what I would call a class "blob"?
All those writestrings don't belong there, they should be in Main!
The so called FindPrime class should be dropped anyway.
Primecheck should be a method of its own.
But he, I don't force you if you like to do it your way, please continue.
Don't be afraid of the many dangers ahead...

So do you reckon the first method is better? You see,what i am trying to do is to see,if the user enters decimal value,it should be trancated as you say and than checked or prime.Agree,but how to implement it in the first code? Should i use long or double ?

Use long, there are no double primes.

Have one app,would like you to have a look on it and tell your opinion about the errors.If you can.Thanks

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.