# C++ program to find factors of a number using while loop and for loop

In this article, you will learn how to find factors of a number using for loop and while loop in the c++ programming language.

### Examples

**Input: 60**

**The factors of the 60 are: 1 2 3 4 5 6 10 12 15 20 30 60**

**Input: 70**

**The factors of the 70 are: 1 2 5 7 10 14 35 70**

## What are the factors of a number?

The factors of a number are defined as numbers that divided the original number without leaving any remainder **(left reminder = 0)**.

You should have knowledge of the following topics in c++ programming to understand these programs:

- C++

function**main()** - C++

loop statement**for** - C++

loop statement**while** - C++

statement**if** - C++

object**cin** - C++

object**cout**

### 1. C++ program to find factors of a number using for loop

```
// C++ program to find factors of a number using for loop
#include <iostream>
using namespace std;
int main() {
int x, i;
cout << "-----Enter the positive integer number-----\n";
cin >> x;
cout << "\nThe factors of the " << x << " are: ";
for (i = 1; i <= x; ++i) {
if (x % i == 0) {
cout << i << " ";
}
}
cout << "\n";
return 0;
}
```

### Output

`-----Enter the positive integer number-----`

86

`The factors of the 86 are: 1 2 43 86`

### 2. C++ program to find factors of a number using while loop

```
// C++ program to find factors of a number using while loop
#include <iostream>
using namespace std;
int main() {
int x, i = 1;
cout << "-----Enter the positive integer number-----\n";
cin >> x;
cout << "\nThe factors of the " << x << " are: ";
while (i <= x) {
if (x % i == 0) {
cout << i << " ";
}
++i;
}
cout << "\n";
return 0;
}
```

### Output

`-----Enter the positive integer number-----`

86

`The factors of the 86 are: 1 2 43 86`

### Explanation

In these given programs, we have taken input

a random number then applied the **86**

loop and makes a calculation on this random number.**for**

With It self reminder zero to find the possible factors of this random number.

The same calculation applied to the second program with

loop.**while**