### prime numbers

im trying to figure out how to out the prime in the third loop.
but not sure of the correct way.not sure how to do third loop either.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` `````` #include #include using namespace std; int main() { const unsigned N = 20; int i, j; bool prime[N]; prime[0] = false; prime[1] = true; for (i = 0; i < N; i++) // start of outer loop { for (j = 0; j < N; j++) //start of inner loop { for (int k = 2; k <= (i + j) / 2; k++) { } cout << "X"; } cout << endl; } cout << endl; system("pause"); return 0; }``````
What exactly are you trying to do with the prime numbers?

You also don't need 3 loops to solve for prime numbers. 2 loops would work just fine.
your task this week is to write a C++ program to build a
table similar to one below. The table has an equal number "N" of rows and cells. Each cell is either a 0 or a 1value depending on whether the corresponding sum of the column and row indices is a prime number. For example, the entry at column 1 and row 1, has a 1 because 1+1=2, and 2 is a prime number.Sample run with N=16 (your program must work for any N .

| 0 1 2 3 4 5
----------------------------------------
0 |1 1 1 1 01
1 |1 1 1 0 1 0
2 |1 1 0 1 0 1

1.The table will show up to āNā columns/rows (in the sample run above, N=16). Define āNā at the beginning of your program as
a const unsigned
.
2.You must use two sets of
loops. One is a nested loop.

3.Format the output to display the table as shown above

this is what my assignment is but i dont understand it quite.
You'd need 3 loops for that then. I would suggest you make a program to solve for prime numbers from a user input range of numbers first.

Then modify that code to print out the chart above.
can you give me an example on how to do it.i am very lost and i just started programming.like this:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` ``````#include #include using namespace std; int main() { const unsigned N = 20; int i, j, n; bool prime[N]; prime[0] = false; prime[1] = true; cout << "Please enter a number " << endl; cin >> n; for (i = 0; i < N; i++) // start of outer loop { for (j = 0; j < N; j++) //start of inner loop { for (n = 2; n <= (i+j)/(2); n++) { } cout << "X"; } cout << endl; } cout << endl; system("pause"); return 0; }``````
Last edited on
This might give you something to work with.

It prints out a single row of prime number truth values with a header.

You probably want to add in checks for proper formatting for numbers above 1 digit but this can point you in the right direction.

 ``123456789101112131415161718192021222324252627282930313233`` ``````#include int main() { int user_input, x; std::cout << "This program will check for prime numbers\n"; std::cout << "Please enter an integer upper bound: "; std:: cin >> user_input; //print out the number columns for (int i = 0; i < user_input + 1; i++){ std::cout << i << " "; } std::cout << "\n"; //cout the prime truth value for 0, and 1. My loop isn't checking for those. std::cout << "1 1 "; //this for loop goes through each number up to the user input upper boundary for (int i = 2; i < user_input + 1; i++){// i starts at 2 because the numbers 1 and 2 are by default prime numbers. x = 0;//a variable to check if a number is prime. If it gets set to 1 the number is not prime. for (int j = 2; j < i; j++){//this loop divides the number by 1 up to itself and if the remainder is 0 sets x = 1. if (i%j == 0){ x = 1; break; } } if (x == 0){//if the x = 1 flag is never set the number is prime. std::cout << "1 "; } else { std::cout << "0 "; } } return 0; }``````

Output:
 This program will check for prime numbers Please enter an integer upper bound: 10 0 1 2 3 4 5 6 7 8 9 10 1 1 1 1 0 1 0 1 0 0 0
Last edited on
i use your code but i cant get it to give the results all the way to 20
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````#include #include #include using namespace std; int main() { const unsigned N = 20; int i, j, n, x; cout << "Please enter an integer: " << endl; cin >> n; cout << "--------------------------------------------" << endl; for (i = 0; i <= N; i++) // start of outer loop { cout << i << " "; } cout << "\n"; for (i= n;i<=N ; i++) { x = 0; for ( j = 2; j < i; j++) { if (i%j == 0) { x = 1; break; } } if (x == 0) { cout << "1 "; } else { cout << "0 "; } } system("pause"); return 0;``````
Last edited on
Topic archived. No new replies allowed.