-1

the program should ask for a number and print all the primes numbers between 1 to the number the user entered... why isn't it working?

bool isPrime = true;
int primes = 0;
Console.WriteLine("Enter a number");
int N = int.Parse(Console.ReadLine());
for (int i = 2; i <= N; i++)
{
    for (int j = 2; j <= Math.Sqrt(i); j++)
    {
        if (i % j == 0)
        {
            isPrime = false;
        }  
    }
    if (isPrime)
    {
        Console.WriteLine(i + " is a prime number");
        primes++;
    }
}
Console.WriteLine("Between 1 to " + N + " there are " + primes + " prime numbers");
DexT8eR
  • 3
  • 3
  • 4
    Possible duplicate of [Program to find prime numbers](https://stackoverflow.com/questions/1510124/program-to-find-prime-numbers) – Shmosi Oct 08 '18 at 11:05
  • What is the output? – Rarblack Oct 08 '18 at 11:07
  • 1
    consider using a [debugger](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Alexander Dmitriev Oct 08 '18 at 11:08
  • 3
    Welcome to stack overflow, do note that "Why isn't it working?" is a poor description of a problem. And one of the flag for closing reasons. Check the [how to ask](https://stackoverflow.com/help/how-to-ask) – Cleptus Oct 08 '18 at 11:09
  • Just an observation - C# will evaluate Sqrt(i) every time it goes around the loop. It would be faster to evaluate this once and assign it to a temporary variable. – Robin Bennett Oct 08 '18 at 11:26

2 Answers2

4

You have put the boolean out of the loops. So, once it is false, it will never be true in other loops and this cause the issue.

        int primes = 0;
        Console.WriteLine("Enter a number");
        int N = int.Parse(Console.ReadLine());
        for (int i = 2; i <= N; i++)
        {
           bool isPrime = true;
            for (int j = 2; j <= Math.Sqrt(i); j++)
            {
                if (i % j == 0)
                {
                    isPrime = false;
                }  

            }
            if (isPrime)
            {
                Console.WriteLine(i + " is a prime number");
                primes++;
            }
        }
        Console.WriteLine("Between 1 to " + N + " there are " + primes + " prime numbers");
elixenide
  • 44,308
  • 16
  • 74
  • 100
Rarblack
  • 4,559
  • 4
  • 22
  • 33
  • You are welcome and if this is the solution to your question upvote and mark it as the solution so others can easily find. – Rarblack Oct 08 '18 at 11:18
0

First define this class:

public static class PrimeHelper
{
    public static bool IsPrime(int candidate)
    {
        if ((candidate & 1) == 0)
        {
            if (candidate == 2)
            {
            return true;
            }
            else
            {
            return false;
            }
        }
        for (int i = 3; (i * i) <= candidate; i += 2)
        {
            if ((candidate % i) == 0)
            {
            return false;
            }
        }
        return candidate != 1;
    }
}

then call it in your application:

var finalNumber = int.Parse(Console.ReadLine());
for (int i = 0; i < finalNumber; i++)
{
    bool prime = PrimeHelper.IsPrime(i);
    if (prime)
    {
    Console.Write("Prime: ");
    Console.WriteLine(i);
    }
}
Siavash
  • 2,813
  • 4
  • 29
  • 42