Click here to Skip to main content
13,739,603 members
Rate this:
 
Please Sign up or sign in to vote.
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

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.
Notepad++ Home[^]
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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

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?
{
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

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.
  Permalink  
Comments
Patrice T 12-Aug-18 15:31pm
   
See comments I put in code in S2.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180920.1 | Last Updated 12 Aug 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100