-1

I would like to write a code that shows a list of prime number one number to another number. For example from 1 to 8, it would be 2, 3, 5, 7. I got a code from "Check if number is prime number" by user1954418 because I did not know where to begin, so I take NO credit whatsoever for the code.

int num1;
Console.WriteLine("Prime Number:");
num1 = Convert.ToInt32(Console.ReadLine());
if (num1 == 0 || num1 == 1)
{
    Console.WriteLine(num1 + " is not prime number");
    Console.ReadLine();
}
else
{
    for (int a = 2; a <= num1 / 10; a++)
    {
        if (num1 % a == 0)
        {
            Console.WriteLine(num1 + " is not prime number");
            return;
        }
    }
    Console.WriteLine(num1 + " is a prime number");
    Console.ReadLine();
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Abdulhamid
  • 317
  • 1
  • 3
  • 12
  • make a loop to check and find – sujith karivelil Apr 01 '16 at 01:45
  • wouldn't that be making a for loop in another for loop? – Abdulhamid Apr 01 '16 at 01:53
  • check the answer below; place break point and do `F11` see what is happening and that help you to understand – sujith karivelil Apr 01 '16 at 01:54
  • 2
    What have you tried? What exactly is going wrong? Do you get any errors? What are those errors? What have you tried to do to fix those errors? What happened when you did that? Remember to include these things when you make your question. Follow this guide to make sure your questions are of high quality: https://stackoverflow.com/help/how-to-ask – Matt C Apr 01 '16 at 02:06
  • 1
    The code you are posting appears to be mostly taken from [Check if number is prime number](http://stackoverflow.com/questions/15743192/check-if-number-is-prime-number). You should indicate that in your question properly –  Apr 01 '16 at 02:32
  • My apologies to those who find the code to be faulty. As I am new here, I will try my best to include those features in the immediate future. – Abdulhamid Apr 01 '16 at 03:05

3 Answers3

2

Make it simple; following code will help you:

Console.WriteLine("Enter the Limit:");
int Limit;
if (!int.TryParse(Console.ReadLine(), out Limit))
   {
      Console.WriteLine("Invalid input");
   }
Console.WriteLine("List of prime numbers between 0 and {0} are :",Limit);
for (int i = 2; i < Limit; i++)
   {
      if (checkForPrime(i))
          Console.WriteLine(i);
   }
Console.ReadKey();

Where checkForPrime() is defined as follows:

public static bool checkForPrime(int Number)
    {
        for (int a = 2; a <= Number / 2; a++)
        {
            if (Number % a == 0)
            {
                return false;
            }
        }
        return true;
    } 
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
2

Here is a sample that should work. The code from isPrime I just got from here Here

Then In the main function just have the loop that goes from you starting number to the end number, and runs the isPrime function on each one.

Here is the code:

class Program
{
    static void Main(string[] args)
    {
        int numberStart;
        int numberEnd;

        // Take in the start point and the end point
        Console.WriteLine("Starting Number:");
        if(!int.TryParse(Console.ReadLine(), out numberStart)){
            Console.WriteLine("Your input is invalid.");
        }

        Console.WriteLine("Ending Number:");
        if (!int.TryParse(Console.ReadLine(), out numberEnd))
        {
            Console.WriteLine("Your input is invalid.");
        }

        // Loop from the first number to the last number, and check if each one is prime
        for (int number = numberStart; number < numberEnd; number++)
        {
            Console.WriteLine(number + " is prime?");
            Console.WriteLine(isPrime(number) + "\n");
        }



        Console.ReadLine();

    }

    // Function for checking if a given number is prime.
    public static bool isPrime(int number)
    {
        int boundary = (int) Math.Floor(Math.Sqrt(number));

        if (number == 1) return false;
        if (number == 2) return true;

        for (int i = 2; i <= boundary; ++i)
        {
            if (number % i == 0) return false;
        }

        return true;
    }
}

Note that the function isPrime() only checks up to the root, as it is unnecessary to check further as mentioned by the user from the link.

Hope it helps :)

Community
  • 1
  • 1
Nikita V
  • 56
  • 6
1

Here's some code I wrote/copied to do this in one of my projects. It could probably be a little smoother but it gets the job done.

    // Found this awesome code at http://csharphelper.com/blog/2014/08/use-the-sieve-of-eratosthenes-to-find-prime-numbers-in-c/
    // This creates a List of Booleans where you can check if a value x is prime by simply doing if(is_prime[x]);
    public static bool[] MakeSieve(int max)
    {
        var sqrt = Math.Sqrt((double)max);
        // Make an array indicating whether numbers are prime.
        var isPrime = new bool[max + 1];
        for (var i = 2; i <= max; i++) isPrime[i] = true;

        // Cross out multiples.
        for (var i = 2; i <= sqrt; i++)
        {
            // See if i is prime.
            if (!isPrime[i]) continue;
            // Knock out multiples of i.
            for (var j = i * 2; j <= max; j += i)
                isPrime[j] = false;
        }
        return isPrime;
    }

    public static List<int> GetListOfPrimes(int max)
    {
        var isPrime = MakeSieve(max);
        return new List<int>(Enumerable.Range(1, max).Where(x => isPrime[x]));
        //var returnList = new List<int>();
        //for (int i = 0; i <= max; i++) if (isPrime[i]) returnList.Add(i);
        //return returnList;
    }

Here's an example of usage:

    static void Main(string[] args)
    {
        var x = GetListOfPrimes(8);

        foreach (var y in x)
        {
            Console.WriteLine(y);
        }

        Console.Read();
    }
wentimo
  • 471
  • 3
  • 12