factorize an integer into p and q

i have the following problem to solve.... im unable even to start with an idea....can anybody help me giving an idea how to do this problem?

the problem is:
___*****____
Write a C++ program that factorize an integer n into p and q.
Step1. reads an integer n from the keyboard.
Step2. Write a for loop as below to check if there is any factor j > 1.
1
2
3
4
5
6
7
8
for (int j = 2; j <= square root of n; j ++)
{
If (n % j == 0)
{
factorizable = true;
break;
}
}

Step3. If there is no such factor, print the message n is not factorizable. If there is such a factor j,
check programmatically if j is a prime and n / j is a prime Also check if j and n / j are the same.

___****___
Last edited on
im unable even to start with an idea


It's hard to break it down more than it already is broken down for you.

Start with the basic program stuff... like getting your #includes set up.. and making a main() function.

Then do step 1, then once that's working, move on to step 2, etc.
ok, but now im stuck here with second step..... i don't know what to do with it??
Step 2 is almost entirely done for you. Just put that loop in your program and fill in the blank (get the square root of n and put it in that loop)
done.... step 2 is giving me nothing..... its bool variable in it. whenever the if condition is true, all the loops break.... and there is true in bool variable... now im confused how to use "factorizable"..
(if i sound stupid, sorry me.... im trying to learn programming :P)
What exactly about the bit of code you've been given is confusing? The loop? The variable 'factorizable'? Post carefully what you think it does logically and we can help you correct anything that is wrong.
im confused about the variable "factorizable"..... i dont exactly know how to use bool variables...
A bool is simply a variable that can be either "true" or "false". It cannot have any other value.

Here, your "factorizable" variable is indicating whether or not the number 'n' can be factored. If "factorizable" is true, then that means n can be factored. If false, it means n cannot be factored.
By the way in my opinion the assignment is stupid because j will be always a prime number.:) So what is needed to be done is checking whether n / j is also a prime number.
Last edited on
Topic archived. No new replies allowed.