Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

recursion in java

class scratch{
    public static long factorial(int n){
        if ( n == 1 ){
            return 1;
        }
        else{
            return n * factorial( n - 1 );
        }
    }
    public static void main(String[] args) {
        System.out.println( factorial(5) );
        //Print 120 (5!) to the console
    }
}
Comment

Recursion examples Java

public static void countDown(int number){
        if (number == 0) {
            System.out.println(number);
        } else {
            System.out.println(number);
            countDown(number - 1);
        }
 }
Comment

java recursion

public static long factorial(int n) { 
    if (n == 1) return 1; //base case
    return n * factorial(n-1); 
}
Comment

java recursion

// prints x number of $
public static void recursion(int x) {
  if(x == 0) { // base case
    System.out.println();
  } else { // recursive case
    System.out.print("$");
    recursion(x - 1);
  }
}
Comment

Recursion in java

public class RecursionExample2 {  
static int count=0;  
static void p(){  
count++;  
if(count<=5){  
System.out.println("hello "+count);  
p();  
}  
}  
public static void main(String[] args) {  
p();  
}  
}  
Comment

recursive in java

void f() {
    ... g() ...
}
	
void g() {
   ... f() ...
}
Comment

recursive in java

void f() {
    ... g() ...
}
	
void g() {
   ... f() ...
}
Comment

java recursive method

/*Test*/
Comment

recursion java

//recursion 

void setup() {
  size(500,500);
  background(#0F0E04);
  noLoop();
  fill(#FF5500);
  circles(width/2 , height/2, 200,1.0/3);
  
}
void circles(float x , float y, float diam, float factor){
  circle(x,y,diam);
  if (diam>=3){
    //right circles
    circles(x+.5*diam,y,factor*diam,factor);
    //left circles
    circles(x-.5*diam,y,factor*diam,factor);
    
    //top circles
    circles(x,y+.5*diam,factor*diam,factor);
    //bottom circles
    circles(x,y-.5*diam,factor*diam,factor);    
  }
}
Comment

recursion java

    public static  int exponent(int n){
        int ans;
        if(n == 0)
            return 1;
        else if(n%2 == 0){
            int k = exponent(n/2);
            ans = k*k;
        }
        ans= 2 * exponent(n-1);

        return ans ;
    }
Comment

java recursion

public void removeDigits(String string) {
  if(string.length == 1) {
    if(Character.isDigit(string.CharAt(0))) {
      return "";
    } else {
      return string;
    }
  } else {
    if(Character.isDigit(string.CharAt(0))) {
      return removeDigits(string.substring(1));
    } else {
      return string.substring(0, 1) + removeDigits(string.substring(1));

public static void main(String[] args) {
  String string = "1a2b3c"
  System.out.println(removeDigits(string)); //will print "abc"
}
Comment

PREVIOUS NEXT
Code Example
Java :: session-less control using java 
Java :: java multiple implements 
Java :: java intercambiar la posicion de valores de un array 
Java :: 1 2 1 3 2 1 4 3 2 1 3 2 1 2 1 1 java 
Java :: open google maps cycling navigation 
Java :: min_value java 
Java :: binomial heap implementation java 
Java :: java 8 validate based on pair of strings 
Java :: how to run javac xlint 
Java :: Spring AOP 
Java :: java fx custom cell factory for combo box 
Java :: java throw exception without method signature 
Java :: The larger and the smaller of the character “E” and the integer 71 java 
Java :: java pattern matching 
Java :: java filter list of dupllicate netries 
Java :: Dhttps.protocols=TLSv1.2 
Java :: java program finish event 
Java :: Uri/beecrowd problem no 1118 solution in Java 
Java :: what is abstract class 
Java :: setting up javafx in eclipse 
Java :: install java 11 
Java :: multidimensional arrays java 
Java :: java check if instance of subclass 
Java :: extract html tag using regex 
Java :: how to call child class method from parent class in java 
Java :: is string a primitive data type/use of getclass() 
Java :: button height is not increase in android studio 
Java :: selenium firefox to foreground -python java 
Sql :: uninstall mysql ubuntu 18.04 
Sql :: oracle sql drop sequence 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =