-2

I have this task: Write an expression that checks if given positive integer number n (n ≤ 100) is prime. E.g. 37 is prime.

    int number = int.Parse(Console.ReadLine());


    for (int i = 1; i < 100; i++)
    {
        bool isPrime = (number % number == 0 && number % i == 0);

        if (isPrime)
        {
            Console.WriteLine("Number {0} is not prime", number);
        }
        else
        {
            Console.WriteLine("Number {0} is prime", number);
            break;
        }

    }

This doesn't seem to work. Any suggestioins?

Rndm
  • 6,710
  • 7
  • 39
  • 58
D_Andreev
  • 107
  • 2
  • 4
  • 10
  • Please tell us what it's doing that is wrong? Also here is a clue - look at the if statement and the writeline are they correct? – Preet Sangha Nov 02 '12 at 02:48
  • 2
    Is this homework or course work of some kind? – William Lawn Stewart Nov 02 '12 at 02:49
  • 1. Since this appears to be homework of some kind to me I don't want to write the answer for you. 2. What about you have provided "doesn't work"? 3. If your algorithm is wrong (and I'm not saying it's correct or it's wrong) you may want to see http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes or http://stackoverflow.com/questions/1510124/program-to-find-prime-numbers – Frito Nov 02 '12 at 02:51

6 Answers6

2
int number = int.Parse(Console.ReadLine());

bool prime = true;
// we only have to count up to and including the square root of a number
int upper = (int)Math.Sqrt(number);
for (int i = 2; i <= upper; i++) {
   if ((number % i) == 0) {
       prime = false;
       break; 
   }
}
Console.WriteLine("Number {0} is "+ (prime ? "prime" : "not prime"), number);
azz
  • 5,852
  • 3
  • 30
  • 58
1

a. What are you expecting number % number to do?

b. Your isPrime check is "reset" each time through the loop. Something more like this is required:

bool isprime = true;
for(int i = 2; i < number; i++) {
    // if number is divisible by i then
    //    isprime = false;
    //    break
}
// display result.
John3136
  • 28,809
  • 4
  • 51
  • 69
0

The problems are: the for should start from 2 as any number will be dived by 1. number % number == 0 - this is all the time true the number is prime if he meats all the for steps so

else
{
     Console.WriteLine("Number {0} is prime", number);
     break;
}

shold not be there.

The code should be something like this:

        int number = int.Parse(Console.ReadLine());
        if (number == 1)
        {    Console.WriteLine("Number 1 is prime");return;}

        for (int i = 2; i < number / 2 + 1; i++)
        {
            bool isPrime = (number % i == 0);

            if (isPrime)
            {
                Console.WriteLine("Number {0} is not prime", number);
                return;
            }
        }
        Console.WriteLine("Number {0} is prime", number);

please notice that I didn't test this...just wrote it here. But you should get the point.

0

Semi-serious possible solution with LINQ (need smaller range at least):

var isPrime = !Enumerable.Range(2, number/2).Where(i => number % i == 0).Any();
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

The Program checks the given number is prime number or not.

A prime number is a number that can only be divided by 1 and itself

class Program
{
    bool CheckIsPrimeNumber(int primeNum)
    {
        bool isPrime = false;

        for (int i = 2; i <= primeNum / 2; i++)
        {
            if (primeNum % i == 0)
            {
                return isPrime;
            }
        }
        return !isPrime;
    }
    public static void Main(string[] args)
    {
        Program obj = new Program();
        Console.Write("Enter the number to check prime  :  ");
        int mPrimeNum = int.Parse(Console.ReadLine());

        if (obj.CheckIsPrimeNumber(mPrimeNum) == true)
        {
            Console.WriteLine("\nThe " + mPrimeNum + " is a Prime Number");
        }
        else
        {
            Console.WriteLine("\nThe " + mPrimeNum + " is Not a Prime Number");
        }

        Console.ReadKey();
    }
}

Thanks!!!

kasim
  • 346
  • 2
  • 5
  • 23
-1

Here is for you:

void prime_num(long num)
    {
        bool isPrime = true;
        for (int i = 0; i <= num; i++)
        {
            for (int j = 2; j <= num; j++)
            {
                if (i != j && i % j == 0)
                {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime)
            {
                Console.WriteLine ( "Prime:" + i );
            }
            isPrime = true;
        }
    }
hoang nguyen
  • 2,119
  • 5
  • 21
  • 20
  • That prints ALL the primes between 1 and the number, it doesn't check if the input is prime, which was the question. – azz Nov 02 '12 at 02:55
  • Plus it will throw a `StackOverflowException` when `i` will reach `Int32.MaxValue` as you are comparing it against a `long`... – Guillaume Nov 02 '12 at 04:00