why isn't this code working? (composite numbers)

My assignment is to have a composite number (any number that is not prime) printing program that passes an integer through a bool method. This is the code I have. Could someone tell me why its not working?

edit: the program runs, but the only thing it prints is the cout line "composite numbers between 5 and 155". How can I make the code work to print out all composite numbers between 5 and 155

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

bool is_composite(int);
int main()
{
	cout << "Composite Numbers Between 5 and 155: " << endl;
	
	for (int c = 5; c<=155; c++)
	{
		if (is_composite(c))
			cout << c << endl;
	}

	return 0;
}

bool is_composite(int c)
{
	bool status;
	
	for (int j = 5; j < c; j++)
	{
		if (c%j == 0)
			status = true;
		else 
			status = false;
	
	}
	
	return status;

}
Last edited on
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) What do you mean by "not working"? Is it failing to compile? Are you getting a run-time error? Are you seeing some unexpected behaviour?

We can't read your mind. Withholding information from us is only going to hinder us in helping you.
(1) set status = false before the for loop, not in an else arrangement. Actually, if you find a divisor you might as well return true at that point.

(2) test from j=2, not j=5

As a method, this could get quite expensive. You only need to test for divisors up to the square root of c.
Topic archived. No new replies allowed.