Hey. I have this assignment in which i've to generate prime numbers up to a certain limit. Since i'm extremely new to C++ so i have absolutely no idea what's wrong with the program i have written. I have to keep it simple for my understanding. I've written it in Turbo C++ Here:

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int i,j,k;
cout<<"Enter range "<<k;
cin>>k;
for(i=2;i<=k;i++)
{
for(j=2;j<=i;j++)
{
if(i%j!=0)
cout<<i;
}
}
getch();
}

Edited 6 Years Ago by WaltP: Added CODE Tags -- learn to use them!

This is a common assignment. I'll bet if you had searched for examples of prime number generator programs, you would have found many to which you could compare your code and see where you went wrong.

Recall the definition of a prime number, one that is evenly divisible only by 1 and itself. The line where you start your inner loop, 'for (j = 2; j <= i; j++)' guarantees that a number will be divisible by itself and be output as a prime number. You can eliminate that by having instead 'for (j = 2; j < i; j++)' where the 'less than or equal to' is just 'less than'.

You can put one number per line if you cout statement is 'cout << i << endl;'

That still doesn't solve the problem. My program is NOT printing prime numbers. It's just printing all the numbers from 2 to 10. Infact it's printing those numbers 3-4 times!

Because you are using % on its own in every single number, all iterating number is a prime number for you. Try to change j=2;j<=i;j++ to j=2;j<i;j++... Also, you are printing out the number when it is not divisible in your loop which is wrong... What you need is a flag to check whether it is divisible. If it is divisible, turn the flag on and don't print it out AFTER the second loop (but still inside the first loop). The algorithm is extremely slow, but may be good enough for you right now.

Edited 6 Years Ago by Taywin: n/a

1) Use CODE Tags
2) Format your code -- see this
3) main() is not a void function -- see this
4) Execute your loop with pencil and paper to see what it's doing. Concentrate on the IF statement as you do.

Taywin, can you pleeease show me how to do that flag thing! I seriously know nothing about this language.

Actually there were some silly mistakes in my coding. Even though it's generating prime numbers now, it's still not printing '2'. It's also repeating each of the prime numbers 3-4 times. Can anyone please tell me why and what to do about it? I changed this part og my code:

if(i%j!=0)
cout<<i;

to this:
if(i%j==0)
break;
else
cout<<i<<endl;

Edited 6 Years Ago by Nick Evan: Added code-tags

Because you are missing the flag...

#include <iostream.h>
#include <conio.h>
void main() {
  clrscr();
  int i,j,k;
  int prime;  // may declare the variable here
  cout<<"Enter range "<<k;
  cin>>k;
  for(i=2;i<=k;i++) {
    prime = i;  // initiate expected prime number here
    for(j=2;j<i;j++) {
      // instead of print out all primes along the way,
      // reset the number if it can be divisible -> it is not prime
      // and break the loop here
      if (i%j==0) {
        prime = 0;
        break;
      }
    }
    if (prime>0) {
      cout<<prime<<endl;  // now display what you found
    }
  }
}
getch();
}
This article has been dead for over six months. Start a new discussion instead.