Write 1 To 1000 Prime Numbers in C#

Prime Numbers

A prime number is a whole number greater than 1, whose only two whole-number factors are 1 and itself. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.

A Prime Number can be divided evenly only by 1, or itself and it must be a whole number greater than 1.

Example: 5 can only be divided evenly by 1 or 5, so it is a prime number.

But 6 can be divided evenly by 1, 2, 3 and 6 so it is NOT a prime number (it is a composite number).
Take any number, say N and iterate the loop through N/2 from 2. If any one of the number is divisble with 0 remainder, then it is not a prime number and then exit from the loop.

The variable bPrime is used to store and return whether the given number is a prime number or not.

  1. Create a Windows Form Application.

    File -> Project -> Windows Form Application.

    Project Name : PrimeNumberWinForms.

    Windows Form application

  2. Switch to Property Window by pressing F4 function key. Set following properties Form1.

    Form1.Text = Prime Numbers 0 To 1000

  3. Drag N Drop following controls on forms:

    Control Type Control (Name) ID Description
    Button btnPrimeNumbers To generate prime numbers. Prime number generations logic written under this button click.
    Button btnExit To exit from this application.
    ListBox lstPrimeNumbers Result of button clicked and prime number will display with control.

    Change following properties of controls.

    1. btnPrimeNumbers.Text : Prime Numbers
    2. btnExit.Text : Exit
    3. lblResult.Text : [Result]
    4. lstPrimeNumbers.AutoSize : False
    5. lblResult.Width : 117
    6. lblResult.Height: 576

    Double click on Exit button and write following code.

  4. btnExit click event code:

    1. private void btnExit_Click(object sender, EventArgs e)  
    2. {  
    3.     //Exit from this form.  
    4.     this.Dispose();  
    5. }   
    Double click on Prime Numbers button and write following code.

  5. btnPrimeNumbers click event code:

    1. private void btnPrimeNumbers_Click(object sender, EventArgs e)  
    2. {  
    3.     lstPrimeNumbers.Items.Add("Prime Numbers From 0 To 1000.");  
    4.     for (int i = 0; i < 1000; i++)  
    5.     {  
    6.         if (CheckNumberPrimeNumber(i) == true) lstPrimeNumbers.Items.Add(i.ToString());  
    7.     }  
    8. }  
  6. CheckNumberPrimeNumber: This method will check number is prime number or not.
    1. public static bool CheckNumberPrimeNumber(int num)  
    2. {  
    3.     bool CheckPrimeNumber = true;  
    4.     int factor = num / 2;  
    5.     int i = 0;  
    6.     for (i = 2; i <= factor; i++)  
    7.     {  
    8.         if ((num % i) == 0) CheckPrimeNumber = false;  
    9.     }  
    10.     return CheckPrimeNumber;  
    11. }  

You scroll to check prime number from 0 to 1000 in list box.

Prime Number

Please, feel free to asked any question related to this blog.

Next Recommended Reading Prime Number in C#