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 .
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.
#include <iostream>
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