Answers for "recursive function in java"

1

java recursion

public static long factorial(int n) { 
    if (n == 1) return 1; //base case
    return n * factorial(n-1); 
}
Posted by: Guest on October-12-2021
0

java recursion

import java.util.Scanner;

public class SumDigit {
    public static void main(String[] argh){
        System.out.println("Enter int");
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int sum = 0 ;
        int i = 0;
        print( i =Sum_of_digits(sum,n));

    }

    public static int Sum_of_digits(int sum,int n){
        if(n<=0)
            return sum ;
        sum += n%10;

        return Sum_of_digits(sum,n/10);
    }

    public static void  print(int n){
        System.out.print(n);
    }
}
Posted by: Guest on July-13-2021
0

recursion in java

Recursion is a basic programming technique
you can use in Java, in which a method
calls itself to solve some problem. 
  A method that uses this technique
  is recursive. Many programming problems
  can be solved only by recursion,
and some problems that can be solved by
other techniques are better solved by recursion.

EXP-1: Factorial
	
	public static long factorial(int n){
        if (n == 1)
            return 1;
        else
            return n * factorial(n-1);
    }

EXP-2: Fibonacci
	
	static int n1=1, n2=2, n3=0;
    public static void printFibo(int count){
        if (count > 0) {
            n3 = n1 + n2;
            n1 = n2;
            n2 = n3;
            System.out.print(" " + n3);
            printFibo(count-1);
        }
    }
Posted by: Guest on January-14-2021
0

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"
}
Posted by: Guest on May-21-2020
0

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 ;
    }
Posted by: Guest on July-30-2021
0

Recursion in Java

public class Main {
  public static void main(String[] args) {
    int result = sum(10);
    System.out.println(result);
  }
  public static int sum(int k) {
    if (k > 0) {
      return k + sum(k - 1);
    } else {
      return 0;
    }
  }
}
Posted by: Guest on July-08-2021
1

java recursion

public static long factorial(int n) { 
    if (n == 1) return 1; //base case
    return n * factorial(n-1); 
}
Posted by: Guest on October-12-2021
0

java recursion

import java.util.Scanner;

public class SumDigit {
    public static void main(String[] argh){
        System.out.println("Enter int");
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int sum = 0 ;
        int i = 0;
        print( i =Sum_of_digits(sum,n));

    }

    public static int Sum_of_digits(int sum,int n){
        if(n<=0)
            return sum ;
        sum += n%10;

        return Sum_of_digits(sum,n/10);
    }

    public static void  print(int n){
        System.out.print(n);
    }
}
Posted by: Guest on July-13-2021
0

recursion in java

Recursion is a basic programming technique
you can use in Java, in which a method
calls itself to solve some problem. 
  A method that uses this technique
  is recursive. Many programming problems
  can be solved only by recursion,
and some problems that can be solved by
other techniques are better solved by recursion.

EXP-1: Factorial
	
	public static long factorial(int n){
        if (n == 1)
            return 1;
        else
            return n * factorial(n-1);
    }

EXP-2: Fibonacci
	
	static int n1=1, n2=2, n3=0;
    public static void printFibo(int count){
        if (count > 0) {
            n3 = n1 + n2;
            n1 = n2;
            n2 = n3;
            System.out.print(" " + n3);
            printFibo(count-1);
        }
    }
Posted by: Guest on January-14-2021
0

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"
}
Posted by: Guest on May-21-2020
0

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 ;
    }
Posted by: Guest on July-30-2021
0

Recursion in Java

public class Main {
  public static void main(String[] args) {
    int result = sum(10);
    System.out.println(result);
  }
  public static int sum(int k) {
    if (k > 0) {
      return k + sum(k - 1);
    } else {
      return 0;
    }
  }
}
Posted by: Guest on July-08-2021

Code answers related to "recursive function in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language