C++ program to print factors of a number using class.

Note: Use new compiler (Code blocks or Dev C++)  to run these C++ programs. Old  Turbo C compiler does not supports some new features of C++.

If you want to run these programs on old Turbo C++ compiler use <iostream.h> instead of <iostream> , do not use ( using namespace std; ) , use { getch(); } to hold run-time screen , use void main() instead of int main() .



#include<iostream>

using namespace std;

class factors
{
  public:

  void display(int u)
  {    int num = u;
     int i=2,j,fact;
   
       while(num!=0)
       {
       
           if (num%i==0)
           {
              cout << i;
              num/=i;
                  if(num != 1)
                    cout << "  ";
             }
         else
          i++;

             if(num==1 || num==0)
                break;
        }
      }
  };

int main()
{
    factors x;
    int n;
    cout << "Enter any number \n";
    cin >> n;
    x.display(n);
    return 0;
 }

Comments

Popular posts from this blog

C++ program to print number pyramid 1 121 12321 1234321 using class.

C++ program to print 0 10 010 1010 pattern using class

A simple C++ program using class