Program For Printing Factors Of Any Number Entered By The User
Program For Printing Factors Of Any Number Entered By The User:
#include<iostream>
using namespace std;
int main()
{
int i,n;
cout<<"enter any value";
cin>>n; //stores value entered by user in n
cout<<"factors of "<<n<<" are : ";
for(i=1;i<=n;i++)
{
if(n%i==0)
{
cout<<i<<",";
}
}
return 0;
}
output:
enter any value42
factors of 42 are : 1,2,3,6,7,14,21,42,
Explanation of the above program:
#include<iostream> - is used for the cout and cin objects, it supports the input output operations.
using namespace std; - tells the compiler to use the std(standerd) namespace.
int main() - declares the main() function which is the point from where all C++ program begin their execution.
// this is used to denote single line comment in the C++ program
for multiple line comment we use /* ------ */ . in the dashed symbol we can put multiple line comment.
Comments
Post a Comment