Back to: C++ Tutorials For Beginners and Professionals

**Factors of a Number using Loop in C++**

In this article, I am going to discuss Program to Print **Factors of a Number using Loop in C++** with Examples. Please read our previous articles, where we discussed the **Factorial of a Number using Loop in C++** with Examples.

**Factors of a Number:**

First, we will explain what is meant by a factor. Then we’ll see the procedure and then a flowchart and program. Letâ€™s take a number â€˜n = 8â€™ and now we will find the factors of 8. If we divide â€˜8â€™ by some number it is exactly getting divided or the remainder is â€˜0â€™ then it is called a Factor.

Now, who can be the factors? Factors can be from â€˜1â€™ to â€˜8â€™. Some numbers from â€˜1â€™ to â€˜8â€™ can be the factors of â€˜8â€™. Factors are â€˜1â€™, â€˜2â€™, â€˜4â€™ and â€˜8â€™. Non-factors are â€˜3â€™, â€˜5â€™, â€˜6â€™ and â€˜7â€™.

We have already discussed the modulo operator in the arithmetic operation article. Modulo operator is used to getting the remainder of the division between two numbers. Here we will use the modulo operator. Those will be the factors whose modulo will be â€˜0â€™ and the number whose modulo is other than â€˜0â€™ then that will not a factor of that number.

So, if the mod will be zero then we will print â€˜it is a factorâ€™ and if the mod is not zero then we will print â€˜it is not a factorâ€™. Now let us do some paperwork to explain in a better way.

In the above table, the left-hand side column is for counter â€˜iâ€™ means we have to check factors from 1 to that number which we want the factors, in this case, â€˜8â€™, so from â€˜1â€™ to â€˜8â€™. The mid column will check for the condition, â€˜if (n % i == 0)â€™, here we are checking which number will exactly divide â€˜8â€™. For exact division, the remainder must be 0.

So, in the right-hand side column, we are printing the numbers that exactly divide â€˜8â€™ or leaving the remainder â€˜0â€™. So, we will print â€˜1â€™, â€˜2â€™ â€˜4â€™, and â€˜8â€™ are the factors of â€˜8â€™. Now let us look at the flowchart.

**Factors of a Number Flowchart:**

Let us explain the flowchart. First, we will take a number from the user, it is the number for which we want to find the factors. Next, we will initialize our counter, in this case, it is â€˜iâ€™. We will assign â€˜iâ€™ to 1. Next, we will check for the condition â€˜if (i <= n)â€™, it is the condition to run our loop. If it is false then exit the loop.

Now, if the condition â€˜if (i <= n)â€™ will true then we will check for another condition which is â€˜if (n % i == 0)â€™, this condition will check for, will counter â€˜iâ€™ number give 0 remainders on division with the given number.

If the condition is true, means the number is exactly divisible by another number then print that number means print â€˜iâ€™. If it is false then increment the counter and again control goes to the first condition and it will continue until the loop condition â€˜if (i <= n)â€™ fails. So, this was the procedure to find the factors for the given number. Now let us look at the program.

**Program to find the factors of given number using loop in C++:**

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

**Output:**

In the next article, I am going to discuss **Perfect Number using Loop in C++ **with examples. Here, in this article, I try to explain **Factors of a Number using Loop in C++** with examples. I hope you enjoy this Program to print Factors of a Number using Loop in C++ article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

**About the Author: Pranaya Rout**

Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.