Saturday, 14 November 2015

Source Code to Display Factors of a Number

* C++ to find and display all the factors of a number entered by an user.. */

#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

No comments:

Post a Comment