# Thread: simple factoring help..

1. ## simple factoring help..

Hi there i can't get this simple factoring program to work it work's but it only show one factor of a number anyone there i can't get it any help there?? tnx and btw tnx salem for the hints and for correcting my code in translate thread tnx... here's the code..

Code:
```#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int num;
cout << "input num\n";
cin >> num;
for(int a=1; a<=num; a++){
for(int b=1; b<=a; b++){
if(a*b==num){
cout << a << " and " << b << " is the factor of " << num;
}
}
}
getch();
}```
I really can't get it.....

2. Code:
`for(int b=1; b<=num; a++)`
that should be b++

3. Sorry i didn't see that but anyway tnx......^^
I have another problem the factor show's the number the same the output is like this.....

Code:
```20

1 and 20 is the factor of 20
2 and 10 is the factor of 20
4 and 5 is the factor of 20
4 and 5 is the factor of 20 ----------------> repeated
2 and 10 is the factor of 20-----------------> repeated
1 and 20 is the factor of 20----------------> repeated```
what should i do so it will not repeat anymore......tnx

--------------------------------------------------------------------------------------------
nvm the question above i think i got it

Code:
```for(int a=1; a<=num; a++){
for(int b=1; b<=a; b++){
}}```
or is there another way to do this?? tnx^^

4. I didnt crack into the core of your code, was just going to offer:
Code:
`if( a*b == num && b <= a )`
But yours is better optimized since it gets "faster" in the end

5. okey tnx...^^

6. Another approach, without the need for two for loops is to use the modulos operator

Code:
```int num;
std::cout << "input num\n";
std::cin >> num;
for (int a=1; a!=num; ++a) {
if( !(num%a) )
std::cout << a << " and " << num/a << " is the factor of " << num << std::endl;
}```
Edit: the only thing you need to take care off is if the number the user inputs is 1.

7. wow tnx^^........Im finding some other way to do it and im very thank you for teaching me the other way......^^ tnx again...

Popular pages Recent additions