| | |
Displaying Prime Number
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
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
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)
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
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Text; namespace prime { class Program { static void Main(string[] args) { int num; int result; int i, j; Console.WriteLine("Untill what number u want to show prime number?"); num = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("The prime number between o and ur number are:"); if (num == 2) Console.WriteLine("2"); else { Console.WriteLine("2"); for (i = 3; i <= num; i++) { for (j = 2; j <=num / 2; j++) { result = i % j; if (result!=0) Console.WriteLine(i); } } Console.ReadLine(); } } } }
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)
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
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!
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { bool current = false; int j; Console.WriteLine("Enter any integer"); int num = Int32.Parse(Console.ReadLine()); for (int i = 2; i <= num; i++)//2 is the first prime number. //I set i to 2 beacuse it has to print 2 firstly... { for (j = 2; j < i; j++) { if (i % j == 0)//Controls i is prime number or not... { current = true; break;//breaks for not controlling anymore... } } if (current == false) Console.Write("{0} ", j);//if i is prime number, print it... else current = false; } Console.ReadLine(); } } }
Hie guys!
Heres something i have created to display all the prime numbers from zero up to the number entered by user.
This code works fine for me!
Heres something i have created to display all the prime numbers from zero up to the number entered by user.
C# Syntax (Toggle Plain Text)
using System; class pnumber { public static void Main() { int value; // Value Enter By User! int count,count2; // For inner & outer loop int prime; // used to check prime number Console.WriteLine("Welcome To My Program\nPlease enter a number to check whether its a prime number or not!"); value=Convert.ToInt32(Console.ReadLine()); for(count=2;count<value;count++) // Outer loop { prime = 1; // to set prime value to 1 every time outer loop works for(count2=2;count2<count;count2++) // Inner loop { if(count%count2 == 0) // If Value is divisible.. { prime = 0; // Set prime to 0 } } if(prime==1) // If prime value is 1 then... Console.WriteLine("{0} Is Prime Number",count); // display count variable } } }
This code works fine for me!
Last edited by puneetkay; Nov 21st, 2007 at 9:09 am. Reason: changing code to code=csharp
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
see this link
http://www.daniweb.com/code/snippet809.html
•
•
•
•
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
Thanks buddy for snippet link but you are on C# section & thats a C++ snippet.
•
•
Join Date: May 2008
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
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
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.
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
-2
#8 15 Days Ago
c# Syntax (Toggle Plain Text)
using System; class g9{ public static void Main(){ int num; Console.Write("Enter the number till you want Prime Number : "); num=Convert.ToInt32(Console.ReadLine()); for(int i=2;i<=num;i++){ if(i<=5 && i!=4){ Console.Write(i+" "); } if(i%2!=0 && i%3!=0 && i%5!=0) Console.Write(i+" "); } } }
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)
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:
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.
C# Syntax (Toggle Plain Text)
num = 9: i j i%j output 3 2 1 3 3 3 0 3 4 3 3 4 2 0 4 3 1 4 4 4 0 5 2 1 5 5 3 2 5 5 4 1 5 6 2 0 6 3 0 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
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
![]() |
Similar Threads
- C++ prime number program help (C++)
- newbie here, help requested, prime number finder using factoring (C++)
- Print Max Prime Number??? (C++)
Other Threads in the C# Forum
- Previous Thread: Inheritance between windows forms
- Next Thread: Login form appears before (parent) Form1
| Thread Tools | Search this Thread |
.net access algorithm angle array barchart bitmap box broadcast c# capturing check checkbox client combobox control conversion csharp custom database datagrid datagridview dataset datetime dbconnection degrees delegate design development disappear draganddrop drawing encryption enum eventhandlers excel file firefox form format forms function gdi+ image index input install java label leak libraries list listbox mandelbrot math monodevelop mouseclick movingimage msword mysql operator path pause photoshop picturebox pixelinversion post programming radians regex remoting resourcefile richtextbox round server sleep socket sql statistics stream string system.servicemodel table tcpclientchannel text textbox thread time timer update usercontrol validation virtualization visualbasic visualstudio webbrowser windows winforms wpf xml







