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

Please include extra credit if possible All of these programs will use recursion

ID: 3734664 • Letter: P

Question

Please include extra credit if possible

All of these programs will use recursion. You will NOT use loops for these methods. You'll have one main method that sends the recursive method what it needs. The recursive method will return the results to the main You will print the results to the terminal window and to an output file (except for Program 4) All input for this program will be stored in input files. You will create an input file for testing each program. Be aware that unknown input files will be used to grade your programs Pick 3 of the following 4 programs to implement in Java. If you want extra credit, be sure to pick Program 4. Create a menu and let the user choose the Program to complete. Your menu should have at least 3 items to choose

Explanation / Answer

Hi friend, Program is long. Please post one program each post.

I have implemented Program 3.

Please repost others in separate post.

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class CountDigits {

  

   public static int countDigits(int n, int d) {

      

       // base case

       if(n <= 0)

           return 0;

      

       // recursive case

       else{

           int r = n%10;

           if(r == d) {

               return 1+ countDigits(n/101, d);

           }else{

               return countDigits(n/101, d);

           }

       }

   }

  

   public static void main(String[] args) throws FileNotFoundException {

      

       // opening file

       Scanner sc = new Scanner(new File("countDigits.txt"));

      

       // reading an number

      

       int n = sc.nextInt();

      

       // reading digit

      

       int d = sc.nextInt();

      

       sc.close();

      

       // calling countDigit function

      

       int count = countDigits(n, d);

      

       System.out.println("Number of times "+d+" is in "+n+" : "+count);

   }

}