Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have done the program with my knowledge and by not peeking into others' code. I feel that the logic is fine but whenever I enter the nth number I am not getting the corresponding prime number.

What I have tried:

Java
import java.util.*;
public class Main
{
	public static void main(String[] args) {
	 int count=0;
	 int flag;
	 Scanner obj=new Scanner(System.in);
	 int num=obj.nextInt();
	 int i;
	 while(count<=num)
	 {
	     flag=0;
	     for(i=2;i<=num/2;i++)
	     {
	         if(num%i==0)
	         {
	             flag=1;
	             break;
	         }
	     }
	     if(flag==0)
	     {
	         count++;
	     }
	    if(count==num) 
	    {
	        System.out.println(i);
	    }
	 }
	}
}
Posted
Updated 7-Jul-21 19:41pm
v2
Comments
Greg Utas 8-Jul-21 7:38am    
You test divisors from 2 to num/2. You can improve that. Hint: multiplication is commutative.

Quote:
How do I find the nth prime number?

By testing every integer 2, 3, 4, 5, 6, 7, 8 ... to see which are primes.
Your code miss this logic, in fact you continuously check if num is prime or not.
C++
flag=0;
for(i=2;i<=num/2;i++)
{
    if(num%i==0)
    {
        flag=1;
        break;
    }
}


To help you understand what is going on in your code, the tool of choice is the debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
CPallini 8-Jul-21 1:59am    
5.
Patrice T 8-Jul-21 2:09am    
Thank you.
Nandini Sathyan S 8-Jul-21 2:17am    
Thank you so much! I have learned to debug as u suggested
Patrice T 8-Jul-21 2:27am    
Advice: practice debugger as much as possible, it will improve your learning curve.
Nandini Sathyan S 8-Jul-21 3:22am    
Sure
If the "logic was fine" you wouldn't be having problems. Your implementation for finding a prime number is, shall we say, naive at best.

You're testing every single number up to the number you enter in the console. That's NOT the nth prime number. For example, if you enter 10, your code is testing the numbers 2, 3, 4, and 5. It's NOT testing more and more numbers until the count of primes found is reached.
 
Share this answer
 
Comments
CPallini 8-Jul-21 1:59am    
My 5.

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