Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 107,638 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 988 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



prime number

 
Reply to this topicStart new topic

prime number

pavankumar1405
post 1 Jul, 2008 - 02:44 AM
Post #1


New D.I.C Head

*
Joined: 1 Jul, 2008
Posts: 2

prime number generation using recursive function in C#.Net
User is offlineProfile CardPM

Go to the top of the page


pavankumar1405
post 1 Jul, 2008 - 02:59 AM
Post #2


New D.I.C Head

*
Joined: 1 Jul, 2008
Posts: 2

prime number generation using recursive function in C#.Net
User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 1 Jul, 2008 - 03:19 AM
Post #3


D.I.C Addict

Group Icon
Joined: 25 Jan, 2007
Posts: 549



Thanked 10 times

Dream Kudos: 125
My Contributions


Please read rule number 2 given in yellow box above new post text area whenever you feel posting like this.

2. You must show your code before receiving help. We won't do your homework for you.
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 1 Jul, 2008 - 05:55 AM
Post #4


DIC.Rules == true;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,141



Thanked 50 times

Dream Kudos: 7700

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI

My Contributions


Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
User is online!Profile CardPM

Go to the top of the page

djkitt
post 1 Jul, 2008 - 06:03 AM
Post #5


D.I.C Head

**
Joined: 22 May, 2008
Posts: 116



Thanked 13 times
My Contributions


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,
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/29/08 06:13PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month