# (C++ programming Example for Beginners)

# C++ Program to Display Factors of a Number

#### Example to find all factors of an integer (entered by the user) using for loop and if statement.

This program takes a positive integer from an user and displays all the factors of that number.

## Example: Display all Factors of a Number

```
#include <iostream>
using namespace std;
int main(){
int n, i;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factors of " << n << " are: " << endl;
for(i = 1; i <= n; ++i)
{
if(n % i == 0)
cout << i << endl;
}
return 0;
}
```

**Output**

Enter a positive integer: 60 Factors of 60 are: 1 2 3 4 5 6 12 15 20 30 60

In this program, an integer entered by user is stored in variable `n`.

Then, for loop is executed with an initial condition `i = 1`

and checked whether `n` is perfectly divisible by `i` or not. If `n` is perfectly divisible by `i` then, `i` will be the factor of `n`.

In each iteration, the value of `i` is updated (increased by 1).

This process goes until test condition `i <= n`

becomes false,i.e., this program checks whether number entered by user `n` is perfectly divisible by all numbers from 1 to `n` and all displays factors of that number.

## C++ for Beginners: C++ Program to Display Factors of a Number

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause.The information presented here could also be found in public knowledge domains.