Print Divisors of a Number

Easy Loops
Print all divisors of a number using a loop.

Sample input

12

Sample output

1 2 3 4 6 12

Solution

n = 12
divisors = [i for i in range(1, n + 1) if n % i == 0]
print(*divisors)
const n = 12;
const divisors = [];
for (let i = 1; i <= n; i++) {
  if (n % i === 0) divisors.push(i);
}
console.log(divisors.join(" "));
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        int n = 12;
        System.out.println(IntStream.rangeClosed(1, n).filter(i -> n % i == 0)
            .mapToObj(String::valueOf).collect(Collectors.joining(" ")));
    }
}
fun main() {
    val n = 12
    val divisors = (1..n).filter { n % it == 0 }
    println(divisors.joinToString(" "))
}
let n = 12
let divisors = (1...n).filter { n % $0 == 0 }
print(divisors.map(String.init).joined(separator: " "))
void main() {
  int n = 12;
  final divisors = <int>[];
  for (var i = 1; i <= n; i++) {
    if (n % i == 0) divisors.add(i);
  }
  print(divisors.join(' '));
}
#include <iostream>
using namespace std;

int main() {
    int n = 12;
    bool first = true;
    for (int i = 1; i <= n; i++) {
        if (n % i == 0) {
            if (!first) cout << " ";
            cout << i;
            first = false;
        }
    }
    cout << endl;
    return 0;
}
#include <stdio.h>

int main() {
    int n = 12, first = 1;
    for (int i = 1; i <= n; i++) {
        if (n % i == 0) {
            if (!first) printf(" ");
            printf("%d", i);
            first = 0;
        }
    }
    printf("\n");
    return 0;
}