1
Reply

Display Array Index

Nik

Nik

Sep 15 2007 9:41 PM
2.4k

I know for most of you guys out there this might be a simple task.  I am taking a programming class and need some help. Here is the question:

Write a C# program that computes and stores the first 20 prime numbers in a single dimensional array.  Use a loop to display the array index plus 1 and the value of the prime number at that index.

I think a have the first part solve but I do not understand what the second part is.  Here is what I have so far:

 

 

private void btnOk_Click(object sender, System.EventArgs e)

{

int limit = 20;

int num = 2;

int div;

string display;

// Define and initialize arrays to hold data

//Displa colum headers in ListBox

display = " Array Index Prime Number";

this.lstPrimeNum.Items.Add(display);

//Process all elements in a loop and send results to ListBox

// first prime is 2 (1 is not a prime)

this.lstPrimeNum.Items.Add(num.ToString());

// only odd numbers need to be checked

for (num = 3; num <= limit; num += 2)

{

// prime test loop

for (div = 3; num % div != 0; div += 2)

;

// prime is divisible only by itself

if (div == num)

// add prime to listbox

this.lstPrimeNum.Items.Add(num.ToString());

}

}

}


Answers (1)