0
public class Program
{

    //Program Working Correctly

    public static void Main(string[] args)
    {

        Console.WriteLine("Enter the Number : ");
        int num = int.Parse(Console.ReadLine());
        Boolean isPrime = true;
        
        for (int i = 2; i <= num; i++)
        {
            for (int j = 2; j <= num; j++)
            {
                if (i != j && i % j == 0)
                {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime)
            {
                Console.WriteLine(i);
            }
            isPrime = true;
        }
    }
}

Result:

Enter the Number : 10

2

3

5

7

This program finds the prime numbers whatever number you input my question is I want to fetch the prime number with the index number.

For example: I enter the index number 3 in index number 3 it must return the 7 coz 7 is the prime number which is placed in the index number 3.

So how to fetch the prime number value with the index number?

Community
  • 1
  • 1
  • 2
    Well I'd probably change your code to have a method which returns an `IEnumerable` using an iterator - change `Console.WriteLine(i)` to `yield i`, basically. Then you could use `Primes().ElementAt(index - 1)` – Jon Skeet Dec 15 '17 at 11:19
  • 1
    Do you want to find the prime numbers under 10 *and then* get a result by the index, or are you saying you just want to give an index, so like `getPrimeNumber(5);` will return the 5th prime number? – Reinstate Monica Cellio Dec 15 '17 at 11:21
  • yes sir i want get a result by the index #Archer – Tauseef khan Dec 15 '17 at 11:25
  • you want the one closer to the number you gave or the first prime ever? cause that's pretty different. or you want all the indexes until the number, lets say you gave 100, so it will pass the position where prime numbers exist from 1 to 100? – Zorkind Dec 15 '17 at 11:28
  • yes, sir if I enter the index no it will pass the position where prime numbers exist from 1 to 100? – Tauseef khan Dec 15 '17 at 11:36
  • Take a look at this, it's the proper way to go about it https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes – Zorkind Dec 15 '17 at 11:38

1 Answers1

1

If I understand your question correctly, this should give you what you need...

public static bool IsPrime(int number)
{
    if (number == 1) return false;
    if (number == 2) return true;
    if (number % 2 == 0)  return false;

    var boundary = (int)Math.Floor(Math.Sqrt(number));

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

    return true;        
}

public static int GetPrimeByIndex(int index)
{
    var i = 2;

    while (index > 0)
    {
        i++;
        if (IsPrime(i)) index--;                
    }

    return i;
}

IsPrime(n) is taken from this question:

Check if number is prime number

I've simply wrapped that in a function that finds the prime number by whatever index you supply.

Here's a dotnetfiddle for you to see it working...

https://dotnetfiddle.net/uaZeQB

You can use GetPrimeByIndex(n) to get the prime number at the given index, without having to create a list first and then get an item by index from that list. This may not be what you want, but this is how I read your question.

Reinstate Monica Cellio
  • 25,975
  • 6
  • 51
  • 67