Factorial Program Using DP

package org.wesome.dsalgo;

public class Factorial {
    public static int factorial(int n) {
        int factorial[] = new int[n + 1];
        factorial[0] = 1;
        for (int i = 1; i <= n; i++) {
            factorial[i] = factorial[i - 1] * i;
        }
        return factorial[n];
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.wesome.dsalgo.Factorial.factorial;

public class factorialTest {
    @Test
    void factorialTest1() {
        int factorial = factorial(5);
        System.out.println("factorial = " + factorial);
        Assertions.assertEquals(120, factorial);
    }

    @Test
    void factorialTest2() {
        int factorial = factorial(10);
        System.out.println("factorial = " + factorial);
        Assertions.assertEquals(3628800, factorial);
    }

    @Test
    void factorialTest3() {
        int factorial = factorial(0);
        System.out.println("factorial = " + factorial);
        Assertions.assertEquals(1, factorial);
    }
}
plugins {
    id 'java'
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

test {
    useJUnitPlatform()
}

follow us on