Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Type the next higher prime following the number entered in the console

What I have tried:

int num = int.Parse(Console.ReadLine());
            int pm = 0;

            for (int j = num + 1; j < 100; j++)
            {
                string respuesta = "si";
                for (int i = 2; i < j; i++)
                {
                    if (j % i == 0)
Posted
Updated 21-Feb-22 20:38pm
Comments
Patrice T 22-Feb-22 0:05am    
Part of your code is missing.
What is the problem with this code?
darelin rijo hernandez 22-Feb-22 0:08am    
I don't know how to continue with it
Patrice T 22-Feb-22 0:38am    
You can't just have this piece of code like that.
I can understand that you are stuck at some point, but this can't be the whole code.
Use Improve question to update your question.

Say n is the entered number. The straightforward approach (maybe not the most efficient) would be iteratively apply a primality test to (n+1), (n+2), .. stopping at first prime found.
So you need a primality test function. Since it is a well-maintained secret, i strogly doubt you may find some useful info about primality test on the web[^].
 
Share this answer
 
Comments
_Asif_ 22-Feb-22 3:23am    
+5
CPallini 22-Feb-22 3:48am    
Thank you.
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

Think about the question and how you would do it manually. read it carefully, it tells you exactly what you have to do.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

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