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

``````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?");
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);

}

}

}
}
}
}``````

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)

## All 16 Replies

``````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");

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;
}

}
}
}``````

Hie guys!

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

``````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!");
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!

Some how the program is correct but the algorithm is not efficient of the large no.

http://www.daniweb.com/code/snippet809.html

Hie Rhohitman,

Thanks buddy for snippet link but you are on C# section & thats a C++ snippet. :D

Some how the program is correct but the algorithm is not efficient of the large no.

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

``````using System;
class g9{
public static void Main(){
int num;
Console.Write("Enter the number till you want Prime Number : ");
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+" ");

}
}
}``````

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:

``````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.

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 ;)

commented: Nice suggestion! +5

Yes, I do this also when confronted wih this kind of "problems".
Although these days I prefer the wonderfull debugger that comes with VS!

My algorithm below is similar to what slogfilet suggested: I consider using anything beyond the square root of the integer candidate to be redundant and a waste of time to use as a test divisor.

I'm not accepting user input, nor am I writing the prime numbers to the screen, but it's easy enough to add that in (actually, the writing to the screen is simply commented out). I was more interested in seeing how fast I could get the program to run, hence the use of the stopwatch. It's under 300 milliseconds to get all primes up to 1,000,000 on my machine, just about 5 seconds to go up to 10,000,000, and of course will increase significantly from there. That's the price of using nested loops on growing lists. But I'm not done working on it, and I'm not going to assume I know enough about C# to say that I've thought of the best looping structure.

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

class Program
{
static void Main()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();

List<int> primes = new List<int>();

for (int i = 3; i < 1000000; i += 2)
{
bool isPrime = true;
int stoppingPoint = (int)Math.Pow(i, 0.5) + 1;
foreach (int p in primes)
{
if (i % p == 0)
{
isPrime = false;
break;
}

if (p > stoppingPoint)
break;
}

if (isPrime)
{
//Console.WriteLine(i);
}
}

stopwatch.Stop();
Console.WriteLine("-- {0}", primes.Count);
Console.WriteLine(stopwatch.ElapsedMilliseconds);
}
}``````

Thank you for going over this step by step. I am a self taught new programmer and this has helped me.

Welcome iajm,

``````private string  isPrime(string text)
{
int num = Convert.ToInt32(text);
int ctr = 2;
while (ctr <= num)
{
if (num % ctr == 0)
break;
ctr++;
}
if(ctr==num)
return "prime number"; return "not a prime number";

}``````

Hi friends

Here i am using simple logic to get prime numbers from 1 to N numbers.

``````using System;
using System.Collections.Generic;
using System.Text;
namespace prime
{
class Program
{
static void Main(string[] args)
{
int a,n,i;
Console.Writeline("Enter ur number");
for(i=0;i<=n;i++)
{
if((i%2==0)&&(i%i==0))
{
a=i;
Console.Writeline("The prime numbers are",a);
}
}
}
}
}``````

Output:
Enter number:9
The prime numbers are :2 3 5 7 9

Explanation:

modulus will take reminder as output as all we know.

If we write only i%2==0 it won't print 2 as a prime number.
so, i wrote i%i==0 here.
Output will come if and only if both conditions are true

Can you please explain it in a simple way.