Displaying Prime Number

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 1
Reputation: panpwintlay is an unknown quantity at this point 
Solved Threads: 0
panpwintlay's Avatar
panpwintlay panpwintlay is offline Offline
Newbie Poster

Displaying Prime Number

 
-1
  #1
Jun 6th, 2007
Hello friends

As I am a beginner of C# in .NET , I got stuck in this program
This program is about displaying prime numbers between the range of o and the number you entered.
Its running properly.But the output is not correct.

Here is my coding

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace prime
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. int num;
  12. int result;
  13. int i, j;
  14.  
  15. Console.WriteLine("Untill what number u want to show prime number?");
  16. num = Convert.ToInt32(Console.ReadLine());
  17. Console.WriteLine("The prime number between o and ur number are:");
  18. if (num == 2)
  19. Console.WriteLine("2");
  20.  
  21. else
  22. {
  23.  
  24. Console.WriteLine("2");
  25. for (i = 3; i <= num; i++)
  26. {
  27. for (j = 2; j <=num / 2; j++)
  28. {
  29. result = i % j;
  30. if (result!=0)
  31. Console.WriteLine(i);
  32.  
  33. }
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. }
  42. Console.ReadLine();
  43.  
  44. }
  45. }
  46. }
  47. }

If I put 5 , it is still ok.( output shows 235)
but If I put 7 or 9 or larger number then the outputs are not correct .(out put shows 23455777)
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,955
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 917
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Displaying Prime Number

 
0
  #2
Jun 9th, 2007
A prime number is ...
any natural number greater than 1 that has the two divisors 1 and itself
(defined in: http://en.wikipedia.org/wiki/Prime_number)

Your algorithm is pretty bad, take a look at this C# code snippet at DaniWeb to help you out:
http://www.daniweb.com/code/snippet676.html
Last edited by vegaseat; Jun 9th, 2007 at 4:23 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 53
Reputation: FoX_ is an unknown quantity at this point 
Solved Threads: 7
FoX_'s Avatar
FoX_ FoX_ is offline Offline
Junior Poster in Training

Re: Displaying Prime Number

 
0
  #3
Jun 9th, 2007
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace ConsoleApplication1
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. bool current = false;
  12. int j;
  13.  
  14. Console.WriteLine("Enter any integer");
  15. int num = Int32.Parse(Console.ReadLine());
  16.  
  17. for (int i = 2; i <= num; i++)//2 is the first prime number.
  18. //I set i to 2 beacuse it has to print 2 firstly...
  19. {
  20. for (j = 2; j < i; j++)
  21. {
  22. if (i % j == 0)//Controls i is prime number or not...
  23. {
  24. current = true;
  25. break;//breaks for not controlling anymore...
  26. }
  27. }
  28. if (current == false)
  29. Console.Write("{0} ", j);//if i is prime number, print it...
  30. else
  31. current = false;
  32. }
  33.  
  34. Console.ReadLine();
  35.  
  36.  
  37.  
  38. }
  39. }
  40. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: Displaying Prime Number

 
0
  #4
Nov 21st, 2007
Hie guys!

Heres something i have created to display all the prime numbers from zero up to the number entered by user.

  1. using System;
  2. class pnumber
  3. {
  4. public static void Main()
  5. {
  6. int value; // Value Enter By User!
  7. int count,count2; // For inner & outer loop
  8. int prime; // used to check prime number
  9.  
  10. Console.WriteLine("Welcome To My Program\nPlease enter a number to check whether its a prime number or not!");
  11. value=Convert.ToInt32(Console.ReadLine());
  12. for(count=2;count<value;count++) // Outer loop
  13. {
  14. prime = 1; // to set prime value to 1 every time outer loop works
  15. for(count2=2;count2<count;count2++) // Inner loop
  16. {
  17. if(count%count2 == 0) // If Value is divisible..
  18. {
  19. prime = 0; // Set prime to 0
  20. }
  21. }
  22. if(prime==1) // If prime value is 1 then...
  23. Console.WriteLine("{0} Is Prime Number",count); // display count variable
  24. }
  25. }
  26. }

This code works fine for me!
Last edited by puneetkay; Nov 21st, 2007 at 9:09 am. Reason: changing code to code=csharp
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: Displaying Prime Number

 
0
  #5
Jan 28th, 2008
Some how the program is correct but the algorithm is not efficient of the large no.

see this link
http://www.daniweb.com/code/snippet809.html
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: Displaying Prime Number

 
0
  #6
Jan 28th, 2008
Originally Posted by Rhohitman View Post
Some how the program is correct but the algorithm is not efficient of the large no.

see this link
http://www.daniweb.com/code/snippet809.html
Hie Rhohitman,

Thanks buddy for snippet link but you are on C# section & thats a C++ snippet.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 1
Reputation: slogfilet is an unknown quantity at this point 
Solved Threads: 0
slogfilet slogfilet is offline Offline
Newbie Poster

Re: Displaying Prime Number

 
0
  #7
May 8th, 2008
Originally Posted by Rhohitman View Post
Some how the program is correct but the algorithm is not efficient of the large no.

see this link
http://www.daniweb.com/code/snippet809.html
I wrote something similar in C++, but the concept is the same... instead of using modulus with every ineger less than num, try this:

for (int i = 2; i <= sqrt(num); i++)


once i becomes larger than the square root of num, you're doing the same work over again!

you can also restrict it to only divide by ODD numbers, which theoretically cuts your processing time in half.

i ended up playing with an array, adding each new prime number to the array, adn only testing num against those values. worked pretty well, if i recall
Last edited by slogfilet; May 8th, 2008 at 2:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: gauravm2k7 is an unknown quantity at this point 
Solved Threads: 0
gauravm2k7 gauravm2k7 is offline Offline
Newbie Poster
 
-2
  #8
15 Days Ago
  1. using System;
  2. class g9{
  3. public static void Main(){
  4. int num;
  5. Console.Write("Enter the number till you want Prime Number : ");
  6. num=Convert.ToInt32(Console.ReadLine());
  7. for(int i=2;i<=num;i++){
  8. if(i<=5 && i!=4){
  9. Console.Write(i+" ");
  10. }
  11. if(i%2!=0 && i%3!=0 && i%5!=0)
  12. Console.Write(i+" ");
  13.  
  14. }
  15. }
  16. }
Last edited by peter_budo; 15 Days Ago at 5:33 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 307
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 55
Ryshad's Avatar
Ryshad Ryshad is online now Online
Posting Whiz
 
1
  #9
15 Days Ago
Any time you are having troubles with iterations like this (especially nested) your first step should be to run through it step by step and track each variable at each stage:
  1. num = 9:
  2.  
  3. i j i%j output
  4. 3 2 1 3
  5. 3 3 0
  6. 3 4 3 3
  7. 4 2 0
  8. 4 3 1 4
  9. 4 4 0
  10. 5 2 1 5
  11. 5 3 2 5
  12. 5 4 1 5
  13. 6 2 0
  14. 6 3 0
  15. 6 4 2 6

This allows you to see exactly whats going on and why the results arent what you expect. I wont tell you how to rewrite your algorythm as there are plenty of examples already here, just thought i'd chip in with some advice on solving similar issues in the future.

Originally Posted by Me;
Give a man a solution, and his problem is solved for today; give him the tools to trouble shoot his work, and his problems are solved for a lifetime
Last edited by Ryshad; 15 Days Ago at 7:35 am.
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,908
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 273
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso
 
0
  #10
14 Days Ago
@Ryshad
Yes, I do this also when confronted wih this kind of "problems".
Although these days I prefer the wonderfull debugger that comes with VS!
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC