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:

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.

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.

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;

#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();
}

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:

try
{
Runtime run = Runtime.getRuntime();
Process pr = ...

Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...