-3

How can I display the prime numbers between 1 and 100 in c#?

Heiko Hatzfeld
  • 3,127
  • 16
  • 15
Sudhakar K
  • 29
  • 1
  • 1
  • 6
  • 7
    My guess is that this is homework... in which case I'm not going to just provide a solution, because that won't help you learn. However, we'll help you get there. How far have you got? What are you finding difficult? – Jon Skeet Sep 22 '09 at 09:24
  • 2
    Sounds like homework - did you give it a try on your own? – tanascius Sep 22 '09 at 09:24
  • 29
    2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 – manji Sep 22 '09 at 09:24
  • 2
    You should at least try to put effort in answering your own question. Also, notice that a question should end with a question mark, and try to provide any additional info that may be needed in order to be answered. – giorgian Sep 22 '09 at 09:27
  • How to Hello World in C# – Isaac Sep 22 '09 at 09:36
  • Poor kid, you have walked straight into this X-), you should have known better... – Adriaan Stander Sep 22 '09 at 09:43

5 Answers5

12

This might help:

using System;
namespace HelloWorld
{
    class Hello 
    {
        static void Main() 
        {
            System.Console.WriteLine("2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97");
        }
    }
}
sth
  • 200,334
  • 49
  • 262
  • 354
luckykrrish
  • 128
  • 6
3

Assuming you wish to find prime numbers, this is a standard question.

  1. Most efficient code for the first 10000 prime numbers?
  2. Most elegant way to generate prime numbers
  3. Prime number calculation fun
  4. Sieve of Eratosthenes [wikipedia]
  5. Sieve of Atkin [wikipedia]
Community
  • 1
  • 1
Eamon Nerbonne
  • 43,645
  • 18
  • 92
  • 161
2

Always fun to try to solve things like this as a one-liner... ;)

Enumerable.Range(2, 100).Where(n => Enumerable.Range(2, n - 2).Count(d => n % d == 0) == 0).ToList().ForEach(Console.WriteLine);
Guffa
  • 640,220
  • 96
  • 678
  • 956
  • 3
    @zzzuperfly: Seriously? Do you really think that he could hand that in as his own code? Well, in the off chance that he does, I wish I could be there to see him try to explain it... – Guffa Sep 22 '09 at 12:43
  • @Guffa: very nice. I'm surprised to see that, for Range(2, 40000), this code is just 2.3 to 4.7 times (depending on the outer for to be i++ or i+= 2) slower than its trivial translation in two for statements, List and all. – giorgian Sep 22 '09 at 17:54
0

I think you should try to implement the sieve of Eratosthenes. There are more complicated sieves available, but thats usually a good version to start with.

Make sure you choose you datatypes correctly, and dont waste space by picking the wrong ones

If the requirement is as loose as in your question, you even might get away with a simple print statement that outputs a ready list of prime numbers ;) najmeddine already supplied the requiered information for you...

Heiko Hatzfeld
  • 3,127
  • 16
  • 15
0

I suggest http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes I wrote a method like this years ago in Turbo Pascal 7 (wow i'm that old!)