FACTORS FINDING - CODECHEF SOLUTION

Problem - FACTORS FINDING

Problem Code - DIFACTRS

You are given a number N and find all the distinct factors of N.

Input:

  • First-line will contain the number N.

Output:

  • In the first line print number of distinct factors of N.
  • In the second line print all distinct factors in ascending order separated by space.

Constraints

  • 1 <= N <= 10^6

Sample Input 1:

4

Sample Output 1:

3
1 2 4

Sample Input 2:

6

Sample Output 2 :

4
1 2 3 6

EXPLANATION:

  • In the first example, all factors of 4 are 1, 2, 4.
  • In the second example, all factors of 6 are 1, 2, 3, 6.

SOLUTION :
C++:
 
#include<iostream>
using namespace std;

int main() {
int n;
cin >> n;
int a[n], j=1;
for (int i=1; i<=n; i++) {
    if (n%i==0) {
        a[j] = i;
        j++;
    }
}

cout << j-1 << endl;
for (int i=1; i<j; i++) {
    cout << a[i] << " ";
}
return 0;
}

0 Comments

Let's Learn Something New

We are a passionate developers constantly working to create solution of Competetive Coding Programming problems that's accurate and easier to understand.