QUOTE(pavankumar1405 @ 1 Jul, 2008 - 04:44 AM)

prime number generation using recursive function in C#.Net
OK.
First make a form with a button on it called button1.
Then make sure the output window is visible by selecting View->Output on the toolbar.
Then put the code below inside the button1_Click event...
CODE
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("--------------------"); // This will print a line of dashes that will show you where the method PrintPrimeNumber is called from outside the method PrintPrimeNumber.
NumberOfPrimesPrinted = 0; // This is the variable that will be incremented inside the method PrintPrimeNumber. You need to set this to zero every time before you call the method (but just from outside the method, for more info on this see the comment from inside the method when this variable is incremented by one.
PrintPrimeNumber(); // This is the call to the method from outside the method that will then generate a prime number recursively.
}
OK. Now put this code at the top of the Forms class (probaly called Form1)
CODE
// Sample will recursively generate prime number
private const int PrimeNumberToGenerate = 37; // Use this constant to specify which prime number you want to be generated using recursion. Make sure you enter a prime number here. ex: 1, 3, 5, 7, 11... Do not enter a number like 4 or 100 as they are not prime numbers.
private const int NumberOfPrimesToPrint = 3; // Use this constant to specify the number of times you want the prime number to be generated. This number can be a prime number like 1,3,5,7 or actually any other number like 4 or 100. It doesn't matter here because this is just the number of times the prime will be printed.
private int NumberOfPrimesPrinted = 0; // This variable will be used to count the number of times the prime number is printed using recursion. When this variables value is equal to the value in NumberOfPrimesToPrint the recursion will stop and the prime number will have been printed the number of times indicated by the value of the constant NumberOfPrimesToPrint. This number needs to be reset to 0 every time the method is called form outside the method (but not when it is called from inside the method because then it would never add up to the value specified in the constant NumberOfPrimesToPrint unless the value specified in NumberOfPrimesToPrint is 1 in which case I guess it would be OK to increment the counter inside the PrintPrimeNumber method.
OK. Now this code can go anywhere basically inside the Form1 class. Maybe put it at the bottom.
CODE
public void PrintPrimeNumber() // This is the method that will be run recursively by itself to generate the prime number. It will run itself until the number of primes printed is equal to the number of primes we want to have printed.
{
Console.WriteLine(PrimeNumberToGenerate.ToString().Substring(0, PrimeNumberToGenerate.ToString().Length)); // Print hte prime number specified in the constant PrimeNumberToGenerate
NumberOfPrimesPrinted++; // Increment the counter variable by one to indicate that the PrimeNumberToGenerate has been printed.
if (NumberOfPrimesPrinted < NumberOfPrimesToPrint) // This line checks the value in the variable NumberOfPrimesPrinted against the value of the constant NumberOfPrimesToPrint to see if the prime has been printed the number of times we want to print it.
PrintPrimeNumber(); // This line calls the method recursively from inside the method which is what recursion is. This line is only run if the number of primes printed is less than the number of primes we want to print.
}
OK.
Now when you press the button you should see the prime number generated by recursion in the Output window.
Hope this helps,