Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

C++ program to find the factors of a number

Program to find Factors of a number

Here we will discuss how to find the factors of a number using C++ programming language.

Factor is a number that when multiplied with another number produces a given number.

To find the Factor of a number we will use a loop in which we start dividing the number with 1 up to the number itself and the numbers which perfectly divides the number are the factors.

For Example: 21

Factors are: 1, 3, 7, 21

Program to find the factors of a number

Algorithm:-

  1. Take input number to factorize.
  2. Input is stored in an int type variable say num.
  3. A factor of num can be found in range 1 to num
  4. Initialize factor=1
  5. Run a loop from factor=1 to num
    1. if num%factor==0 (if num is divisible by factor)
      1. print factor
    2. increment factor , factor ++

C++ Code:-

   
    //C++ Program
    //Factors of a number
    #include <iostream>
    using namespace std;
    //main Program
    int main()
    {
        int num;
        cout << “Enter a positive number: “;
        //user input
        cin >> num;
        cout << “Factors of “ << num << ” are: “ << endl;  
        //finding and printing factors
        for(int i = 1i <= numi++)
        {
            if(num % i == 0)
                cout << i << \t;
        }
        return 0;
    }

    Output

    Enter a positive number: 36
    Factors of 36 are: 
    1    2    3    4    6    9    12    18    36