prime numbers

Feb 20, 2014 at 10:02pm
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 #include <iostream>
#include <iomanip>


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;
}
Feb 20, 2014 at 10:07pm
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.
Feb 20, 2014 at 10:19pm
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.
Feb 20, 2014 at 10:31pm
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.
Feb 20, 2014 at 10:33pm
can you give me an example on how to do it.i am very lost and i just started programming.like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <iomanip>


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 Feb 20, 2014 at 10:46pm
Feb 20, 2014 at 11:07pm
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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

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 Feb 20, 2014 at 11:09pm
Feb 20, 2014 at 11:57pm
i use your code but i cant get it to give the results all the way to 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <iomanip>
#include <cmath>

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 Feb 21, 2014 at 2:35am
Topic archived. No new replies allowed.