Hello I have been at this for a few days, and I have not figured it out but feel as if i am very close. I must create A program that takes a users input, then tells the user if that number is prime or composite, if composite it must display all of the composite numbers prime factors. Someone please take a look at it!

#include <iostream>
using namespace std;
#ifndef __TRUE_FALSE__
#define __TRUE_FALSE__
#define TRUE 1
#define FALSE 0
#endif

 int main () {

	int number = 0;
	int x = 0;
	int z = 0;
	bool NotPrime = FALSE;

	cout << "Enter an Integer" << endl;
	cin >> number;
//Im a bit confused right here, this is where im having trouble
	for (int x=2; x<number; ++x)
		{
			if (number % x == 0)
			{
				bool NotPrime=FALSE;
				cout << " This is a Composite" << endl;
			}
		
			else
				 cout << " This is a Prime" << endl;
		}
//This while find the prime factors of the composite, but i cant get it too work with //the other code
			while(NotPrime=FALSE){
				for(int x=2; x <= number; x++) {
				if(number % x ==0){
					cout << x << endl; 
						number = number/x;
				}
				else
				x++;
				}
			}	
return 0;

		}

Recommended Answers

All 5 Replies

Take a look for what? Just to read it?

If you're looking for help, try explaining what help you need.

if (number % x == 0)
{
bool NotPrime=FALSE;
cout << " This is a Composite" << endl;
}

You don't need to re declare NotPrime. This will create a new variable in a new scope and destroy it as you leave if block. Basically, you don't need to re assign it to false also. Initializing it to false eventually means that you are assuming that number is composite , so you don't need to do that again

while(NotPrime=FALSE){

If will do here since you just have to check the condition once. Also NotPrime=FALSE assigns NotPrime ,the value false. For equality checking , use ==


Logically, if a number is not prime (i.e. composite) set your variable NotPrime to true not to false, here :

if (number % x == 0)
{
bool NotPrime=FALSE;                    // It must be set to true and again don't re declare NotPrime here 
cout << " This is a Composite" << endl;
}

So I set NotPrime =1; , got rid of the other NotPrime like I was mentioned too. It displays the the prime factors for the composites, but now it will display "this is composite" mulitple times, and also "This is a prime" multiple times, even when its a composite, I dont know how to fix that, does anyone know the problem?

#include <iostream>
using namespace std;
#ifndef __TRUE_FALSE__
#define __TRUE_FALSE__
#define TRUE 1
#define FALSE 0
#endif

 int main () {

	int number = 0;
	int x = 0;
	bool NotPrime = TRUE;

	cout << "Enter an Integer" << endl;
	cin >> number;

		for (int x=2; x<number; ++x)
		{
			if (number % x == 0)
				
			{
				NotPrime = 1;
				cout << " This is a Composite" << endl;
				cout << endl;
			}
		
			else
				 cout << " This is a Prime" << endl;
				cout << endl;
		}
			
				for(int x=2; x <= number; x++) {
				if(number % x ==0){
					cout << x << endl; 
						number = number/x;
				}
				else
				x++;
				}
				
return 0;

		}

Try printing out a copy of your program, grab some paper and a pencil. Sit down at a desk or a table.

Start at the first executable statement in main() and follow the program statement by statement. Write down the values of all variables. Write down everything that is displayed. IOW, you be the computer and desk-check your program.

This is a fundamental part of programming -- checking your work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.