Prime Numbers

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
#include <iostream>
using namespace std;

int main() {

	//input output

	cout << "please input a number" <<endl;
	int inputtedNumber;
	cin>> inputtedNumber;
	cout << "The prime number between 3 and that number are:" << endl;
	int candidate = inputtedNumber; // temporary

	//eventually going to put outer loop
	// all numbers between 3 and the inputted numbers
			//this code here tests if canidates is prime
		//loop between the 3  and  inputted 
		// to see if they divide evenly.
		//for looop here betweeen 3 and a canidate



	int i; 
	bool prime = true;


			for (i = 2; i < candidate; i++) {

				cout << candidate << "%" << i << "is";

				cout << candidate %i  << endl;

				cout << "PRIME IS " << ((prime)?"true" : "false") <<endl;

				if (0 !=candidate%i )

					prime = true;

				else 

					prime = false;

				if (prime)

I don't understand what I am doing wrong...its supposed to tell whether the inputted number is prime or not
Here's my algorithm

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
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;

int main(int nNumberofArgs,char* pszArgs[])
{
    int number;
    int counter = 0;

    cout << "Enter an Integer: ";
    cin >> number;
    int divisible[number];

    for(int a = 1;a <= number;a++)
    {
        if(number % a == 0)
        {
            counter++;
            divisible[counter - 1] = a;
        }
    }
    if(counter == 2)
    {
        cout << number << " is prime!" << endl;
    }
    else
    {
        cout << number << " is not prime!" << endl;
        cout << number << " is divisible by: ";
        for(int i = 0;i < number;i++)
        {
            cout << divisible[i];
            if(divisible[i + 1] != NULL)
            {
                cout << ", ";
            }
            else
            {
                break;
            }
        }
        cout << endl;
    }
    system("PAUSE");
    return(0);
}
Topic archived. No new replies allowed.