IN JAVA Write a Java method with the following header public

(IN JAVA)

Write a Java method with the following header:

public static void binaryPrint (int n)

The number n is non-negative. The method prints the value of n as a binary number. If n is zero, then a single zero is printed; otherwise, no leading zeros are printed in the output. The \'\ \' character is NOT printed at the end of the output. Your implementation must be recursive and not use any local variables.

Examples:

n=0 Output: 0

n=4 Output: 100

n=27 Output: 11011

Solution

   public static void binaryPrint(int n){   // n - non negative integer
       if(n == 0 || n==1) {   // Base case : to stop the recursion
           System.out.print(n);
           return;
       }
       binaryPrint(n/2);   // Recursively print the binary number with the quotient
       System.out.print(n%2); // Print the remainder at the end
   }

(IN JAVA) Write a Java method with the following header: public static void binaryPrint (int n) The number n is non-negative. The method prints the value of n a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site