Click here to Skip to main content
15,936,903 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Is there a way so that we can give the range and find the Prime Numbers between the given range. When I try to do that, and I give 2 as my first number and if I give any number as second number then the code finds the correct prime numbers between the given numbers. But instead of 2 if I give any other number as first number and then the code messes up. Error is: It shows all the values between the given range. The code which I wrote is similar to that of Rahul Aman.

Any help would be appreciated.

Thank You!



What I have tried:

StringBuilder sb = new StringBuilder();
int firstNumber, secondNumber;
bool isPrime = true;
firstNumber = int.Parse(tbxPN.Text);
secondNumber = int.Parse(tbxSPN.Text);
sb.Append(" The required prime numbers between "+firstNumber+" and "+secondNumber+" are:<br>");
for (int i=firstNumber;i<=secondNumber;i++)
    for (int j=firstNumber;j<=secondNumber;j++)
        if(i!=j && i % j == 0)
            isPrime = false;

    if (isPrime)
    isPrime = true;
lbResult.Text = sb.ToString();
Updated 29-Sep-16 12:14pm
[no name] 29-Sep-16 16:18pm    
Your description of what you say is the problem appears to be the desired behavior. In any case, learning how to debug your code is a valuable skill especially when you are copying code from the internet to get your homework assignment done.
Sandeep505 29-Sep-16 16:23pm    
For your information, first of all this is not my home work. And, the next thing is I did not copy the code. If you know what is going wrong in this then tell, else, please you can mind your work. I know how to figure out by myself.
[no name] 29-Sep-16 17:39pm    
Then go do it yourself then. It *is* homework and you admitted yourself that you copied the code from somewhere else.
Sandeep505 29-Sep-16 17:46pm    
You can think whatever you wanna think I don't give a damn or sh*t for that and I don't care. I've finished my school long back and I'm new to coding. Trying to learn by myself. And, for your information I figured out the solution for the problem. People like you never let others to learn.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^][^][^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

With the debugger, you will see that you are wrong on testing possibles divisors of an integer.
The possible divisors of an integer do not depend on the sequence you are checking.
If you think about it, you will see that you only need the integer you want to test to know what you need to do and when to stop. You can as well embed the primarity test in a function isPrime()

To DownVoters:
I would like to kno why you don't like this answer ?
What is wrong doing a recommendation of learning the debugger ?
Share this answer
David_Wimbley 30-Sep-16 18:47pm    
My two cents, i think what they don't like is your "canned" answer of "use the debugger". They get a few sentences in and give up and down vote. Had they kept reading they'd see you typically provide some guidance (in this case the isPrime() comment).

Maybe move your guidance portion of this snippet to the top and then the canned response of "you should use the debugger" to the bottom.

I 5'd the answer but it doesn't matter to me if you leave your format the way it is, just wanted to provide my thoughts
Patrice T 1-Oct-16 4:36am    
Thank you for your advice, i will think about it.
You say this isn't a homework problem, but it is very "homeworky" type assignment. A google search for figuring out prime numbers in c# would have brought you tons of results. Whether or not you are still in school doesn't matter, the one skill that you need to learn to master is researching your issues.

find prime numbers between 1 and 10 - Google Search[^]

Returns 691k results and the first two links answer your question. But keep in mind, programming doesn't always provide one size fits all might actually have to do some leg work to massage examples on the internet to meet your needs. Doing a search for exactly your problem often times doesn't reveal meaningful solutions.

With that said, here is one option adapting your code to finding prime numbers in a given range.

Take the IsPrime method from this link:

c# - Check if number is prime number - Stack Overflow[^]

And then work your code down to 1 loop. Something like this:

for (int i = firstNumber; i <= secondNumber; i++)
    if (IsPrime(i))
        sb.Append("" + i + "");


See how the isprime method was massaged into your code? That is what i mean by doing a little leg work to solve your issue.
Share this answer
Sandeep505 29-Sep-16 17:09pm    
I don't know why you think like this is a homework, those days are gone for me to do the so called "homework", this was my self assigned task. Though I fixed it, by myself and also by doing some research on websites on google. Thanks though for you advice. I appreciate for taking time to look at it. Never mind!
David_Wimbley 29-Sep-16 17:36pm    
People often come on this website to seek answers for their homework assignments in hopes someone will do the work for them so they can pass their class.

In terms of how this question was presented, it smells of being a homework assignment for many reasons. Finding prime numbers from a range is quite an academic don't have that issues in typical Line of business/CRUD applications. So that is great its not homework, but the point remains, critical thinking is what is important. But glad to hear it sounds like you figured it out.
Sandeep505 29-Sep-16 17:50pm    
I don't know about other people, but today is the first time I signed up to this blog and posted my first blog here today.
And, May be it smells like that but I know that I'm doing it for myself, I'm not in business as well, I'm new to coding and I'm trying to learn programming so that I can find a job. Anyways, Thank you for your advice I would keep that in my mind in my journey. I appreciate it.
David_Wimbley 29-Sep-16 17:55pm    
Best of luck to you then.
Sandeep505 29-Sep-16 17:58pm    
Thank you for very much for your time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900