Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is the code I am trying to use for the question below. import java.util.Sca

ID: 3690858 • Letter: T

Question

This is the code I am trying to use for the question below.

import java.util.Scanner;
public class Recursive {

   //main
   public static void main(String args[]) {
  
   Scanner sc = new Scanner(System.in);
   System.out.println("Enter the value of n");
   int n = sc.nextInt();
       System.out.println(fact(n));
   }


   //fact
   public static long fact(long n){
       if(n <=1)
           return 1;
       else
           return n * fact (n-1);
   }
}

Consider a simple recursive method (written by yourself or taken from Web) such as calculating the sum of the first n natural numbers, calculating the number of digits of an integer number, calculating 2 to the power of n, etc. Try choosing one different from that of any posted thus far. (1) Show method code and indicate what it does and what represent its parameter(s) and the return value; (2) Identify the base case(s) and indicate how each recursive call simplifies the problem and makes progress towards the base case; (3) For the chosen method, identify the circumstances of infinite method calls.

(If this code does not meet the requirements please change it so it will. )

Explanation / Answer

import java.util.Scanner;

public class Recursive {

    public static void main(String args[]) {

   

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the value of n");

    int n = sc.nextInt();

        System.out.println(fact(n));

                System.out.println("SUM OF FIRST " + n + " NATURAL NUMBERS IS: " + sum(n));

                System.out.println("NUMBER OF DIGITS IN " + n + " ARE: " + num(n));

                System.out.println("2 TO THE POWER OF   " + n + " IS: " + power(n));

    }

    //fact

/*           1) this method takes a long value as parameter and returns a long value as the factorial of the number passed as parameter

                2) The base case is to iterate until the number comes to equal 1 or less than 1. Because the factorial of 1 and zero is always 1.

                3) Infinite method call occurs when in the return statement, n is passed as parameter to fact() instead of decrementing it by 1,i.e, fact(n) passed instead of fact(n-1)

*/

    public static long fact(long n){

        if(n <=1)

            return 1;

        else

            return n * fact (n-1);

    }

// sum of first n natural numbers

/*

                1) This method takes an integer value as parameter and returns an integer value as the sum of the first n natural number.

                2) The base case is to iterate until the number comes to equal to zero. Because when the number is zero, there will be no more numbers to be added.

                3) Infinite method call occurs when in the return statement, n is passed as parameter to sum() instead of decrementing it by 1,i.e, sum(n) passed instead of sum(n-1)

*/

static int sum(int n)

    {

        if(n==0)

            return 0;

        else

            return n+sum(n-1);

    }

// calculate number of digits

/*

                1) This method takes an integer value as parameter and returns an integer value as the count of number of digits in the given number.

                2) The base case is to iterate until the number comes to equal to zero. Because when the number is zero, there will be no more digits to count.

                3) Infinite method call occurs when in the return statement, n is passed as parameter to num() instead of decrementing it by 1,i.e, num(n) passed instead of num(n-1)

*/

static String output = " ";

static int num(int x)

{

    output+=x+" ";

    if (x<10)

        return 1;

        else

            output+=1+num(x/10)+" ";

            return 1+num(x/10);

}

// 2 raised to a power of n

/*

                1) This method takes an integer value as parameter and returns an integer value as the value of 2 raised to a power of the given number n.

                2) The base case is to iterate until the number comes to equal to zero. Because when the number is zero, 2 power zero returns to 1 and no need to calculate further.

                3) Infinite method call occurs when in the return statement, n is passed as parameter to power() instead of decrementing it by 1,i.e, power(n) passed instead of power(n-1)

*/

static int power(int n)

{

if(n==0)

return 1;

else if(n==1)

return 2;

else

return 2*power(n-1);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote