Question -
Given an integer number, you have to find the all factor of this number.
Example -
Input : num = 5
Output : 1, 5
Input : num = 12
Output : 1, 2, 3, 4, 6, 12
Approach -
We divide num with all numbers from 1 to number, if modulus is 0 then it's factor of number.
Implementation -
// to find factor of any integer number
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter the number : ";cin>>num;
cout<<"Factors are : ";
for(int i=1;i<num;i++){
if(num%i==0)
cout<<i<<", ";
}
cout<<num;
return 0;
}
Time complixity - O(n)
Comments
Post a Comment