Lecture5_code - } for (i = 3; i <= (nNumber*...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
private void cmdFindPrimes_Click( object sender, EventArgs e) { Int32 i; Int32 nNumber; try { nNumber = Convert .ToInt16(txtNumber.Text); } catch { MessageBox .Show( "Error converting input!!!Default set to 2" ); nNumber = 2; } if (nNumber >= 2) MessageBox .Show( "Prime number: " + 2); for (i = 3; i <= nNumber; i+=2) { CheckNumber(i); //just call the method instead of the code below //bool prime = true; //Int32 a; //int nr = i; //for (a = 3; a < nr && prime == true; a += 2) //{ // if (nr % a == 0) // prime = false; //} //if (prime == true) // MessageBox.Show("Prime numbers smaller than " + i + " are: " + nr);
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } for (i = 3; i &lt;= (nNumber* 6)/5; i += 2) { CheckNumber(i); //bool prime = true; //Int32 a; //int nr = i; //for (a = 3; a &lt; nr &amp;&amp; prime == true; a += 2) //{ // if (nr % a == 0) // prime = false; //} //if (prime == true) // MessageBox.Show(&quot;Prime number: &quot; + nr); } } public void CheckNumber( Int32 nr) { bool prime = true ; Int32 a; for (a = 3; a &lt; nr &amp;&amp; prime== true ; a+=2) { if (nr % a == 0) prime = false ; } if (prime == true ) MessageBox .Show( &quot;Prime number: &quot; + nr ); }...
View Full Document

Ask a homework question - tutors are online