Tuesday, June 10, 2014

Source Code Rekursif Sederhana ( Faktorial, Trianguler Dll )

package TugasFadli;
public class rekursif {         
    double pr(double x, int n) {
        if (n == 0) {         
            return 1.0;   
        } else if (n > 0) {               
            return (x * pr(x, n - 1)); 
        } else {      
            return (1 / pr(x, -n)); 
        }                           
    }
}
class triangle {
    public static int t (int n) {    
        if (n == 1) {            
            return 1;                
        } else {                   
            return (n + t (n - 1));   
        }
    }
}
class other {
    public static int f(int x) {
        if (x == 0) {              
            return 0;               
        }                           
        return 2 * f(x - 1) + x * x;
    }
}
class cetak {
    public static void main(String[] args) {
        int x = 2, n = 3;
        rekursif hr = new rekursif();
        System.out.println("<+> HASIL <+>\n"
        + "Pngankat rekursif dari " + "" + x + "^" + n + " = " + hr.pr(x, n));
        triangle ht = new triangle();
        System.out.println("Bilangan triangle dari " + n + " = " + ht.t(n));
        other hf = new other();
        System.out.println( "Bilangan "+ x + " = " + hf.f(x));
       
    }
}

0 comments:

Post a Comment