Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Print all factors of the number

C++ For Loop: Exercise-63 with Solution

Write a program in C++ to enter any number and print all factors of the number.

Pictorial Presentation:

C++ Exercises: Print all factors of the number

Sample Solution:-

C++ Code :

#include <iostream>
using namespace std;
int main()
{
    int num, i;
    cout << "\n\n Print all factors of a number:\n";
    cout << "-----------------------------------\n";
    cout << " Input a number: ";
    cin >> num;
    cout << "The factors are: ";
    for (i = 1; i <= num; i++) 
    {
        if (num % i == 0) 
        {
            cout << i << " ";
        }
    }
    cout << endl;
}

Sample Output:

 Print all factors of a number:                                        
-----------------------------------                                    
 Input a number: 63                                                    
The factors are: 1 3 7 9 21 63

Flowchart:

Flowchart: Print all factors of the number

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to find power of any number using for loop.
Next: Write a program in C++ to find one's complement of a binary number.

What is the difficulty level of this exercise?