Kodewick
Start learning →
Basic←→0 / 50

Prime factorization

#018Basicwhile (n % i == 0)8 min

Write a positive whole number as a product of primes, like 90=2*3*3*5, or 97=97 (a prime is just itself). Finish factor(n): from 2 up, if it divides, show that factor and divide n by it. The starter shows 90=; sep goes before each factor: nothing first, then *. The program factors 90, 100, 97.

Should show
90=2*3*3*5
100=2*2*5*5
97=97
main.c
#include <stdio.h>

void factor(int n) {
    printf("%d=", n);
    const char *sep = "";
    for (int i = 2; i <= n; i++) {
        // your code here: while i divides n, show i, then divide n by i
    }
    printf("\n");
}

int main(void) {
    factor(90);
    factor(100);
    factor(97);
    return 0;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.