13,739,603 members
See more:
`e.g. if N=9 prime numbers upto N are 2,3,5,7`

Can someone help me identify what is wrong with my code

What I have tried:

```#include<iostream>
#include<cmath>
using namespace std;
int main()
{
short N,pc=0,num=2;
bool prime;
while(pc!=N)
{
prime=true;
for(short i=2;i<=sqrt(N);i++)
{
if(N%i==0)
{
prime=false;
break;
}
}
}
if(prime)
{
pc++;
cout<<num;
}
return 0;}```
Posted 11-Aug-18 20:36pm
Updated 12-Aug-18 7:45am
v2

## Solution 2

First problem:
Quote:
Create a program to print prime number upto N

What is the value of `N` in this code ?

Second problem:
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
```#include<iostream>
#include<cmath>
using namespace std;
int main()
{
short N,pc=0,num=2;
bool prime;
while(pc!=N)
{
prime=true;
for(short i=2;i<=sqrt(N);i++)
{
if(N%i==0)
{
prime=false;
break;
}
}
}  // this line is misplaced
if(prime)
{
pc++;
cout<<num;
}
// and should be here
return 0;
}```

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
ultraedit[^]

Third problem:
Your code is always checking if N is prime or not. It is wrong.

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

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

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.

## Solution 1

You have not set variable N to any value.
```short N,pc=0,num=2;
bool prime;
while(pc!=N) // what is the value of N?
{
```

## Solution 3

You should do same basic input/output in your code to get N.

And the inequality check is a very bad idea because than N has to be equal to pc, else it runs forever. I would call it a design flaw to not call it a severe bug.

You better check with the less operator, but your code is unclear for now.
Patrice T 12-Aug-18 15:31pm

See comments I put in code in S2.

Top Experts
Last 24hrsThis month
 Jason Gleim 120 CPallini 110 OriginalGriff 80 Richard MacCutchan 75 Dave Kreskowiak 70
 OriginalGriff 5,886 Richard MacCutchan 2,189 Patrice T 1,672 CPallini 1,384 Dave Kreskowiak 1,270