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() .
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;
}
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
Post a Comment