This C++ Program which displays the factors of the given number. The program takes number from the input and runs a while loop until the temporary variable reaches the given number. The temporary number is printed on the screen if the given number is completely divisible by the temporary variable.
Here is source code of the C++ program which displays the factors of the given number. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ program to display the factors of the entered number
*/
#include<iostream>
using namespace std;
int main()
{
int number, temp = 1;
cout << "Enter the number to determine its factors : " << endl;
cin >> number;
cout << "The factors of " << number << " are : " << endl;
while (temp <= number)
{
if (not(number % temp))
cout << temp << " ";
temp++;
}
cout << endl;
}
$ g++ main.cpp $ ./a.out Enter the number to determine its factors : 6 The factors of 6 are : 1 2 3 6
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Apply for C++ Internship
- Check Programming Books
- Practice Computer Science MCQs
- Check C++ Books
- Practice Programming MCQs