1 Write a recursive method called descend that prints the n

1 ) Write a recursive method called descend that prints the numbers 1…n in descending order. Your main method will need to read n from the user, then call method descend with the number read.

2) Write a recursive method called exp to perform exponentiation return x, assuming n >= 0. Your main method will need to read x and n from a file, then call method exp with the input read. Print the result of x after each recursive call.

Solution

1)
/*
* The java program that prompts user to enter
* enter n value and prints the descendant values
* from n to 1.
* */
//DescendTester.java
import java.util.Scanner;
public class DescendTester
{
   public static void main(String[] args) {
       //Create a instance of Scanner classs
       Scanner scanner=new Scanner(System.in);
       //declare n as integer
       int n;
       //prompt for n valule
       System.out.println(\"Enter n value ...\");
       //read n value
       n=scanner.nextInt();

       System.out.println(\"Numbers in descending order \ \");
       descend(n);

   }
   /**
   Static method Name :descend
   Input : int n
   The function that takes n as integer
   and prints the n value in descending order
   calling recursive
   */
   public static void descend(int n)
   {
       if(n==1)
           System.out.printf(\"%-5d\",n);
       else
       {    
           System.out.printf(\"%-5d\",n);
           //calling descend
           descend(n-1);
       }
   }
}

Sample Output:
Enter n value ...
10
Numbers in descending order

10   9    8    7    6    5    4    3    2    1  
----------------------------------------------------------------------------------------
2)

//ExponentialTester.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ExponentialTester {
  
   public static void main(String[] args) {
      
       //set file name
       String fileName=\"data.txt\";
       Scanner filescanner=null;
       try
       {
           filescanner=new Scanner(new File(fileName));
          
           //read x and n values from file
           int x=filescanner.nextInt();
           int n=filescanner.nextInt();
          
           //calling exp method
           System.out.printf(\"%d^ %d = %d\",x,n,exp(x, n));
          
           //close filescanner object
           filescanner.close();
          
       }
       catch (FileNotFoundException e) {
      
           System.out.println(e.getMessage());
       }
      
   }
  
   /**
   static function Name :exp
   Input : int x, int n
   The function exp ,that takes x and n as integer
   and returns the exponenation.
   */
   public static int exp(int x,int n)
   {
       if (n != 0)
            return (x*exp(x, n-1));
        else
            return 1;

   }

}

Sample input file :
data.txt
2 1o

Ouptut:
2^ 10 = 1024

1 ) Write a recursive method called descend that prints the numbers 1…n in descending order. Your main method will need to read n from the user, then call metho
1 ) Write a recursive method called descend that prints the numbers 1…n in descending order. Your main method will need to read n from the user, then call metho

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site