Showing posts with label binary. Show all posts
Showing posts with label binary. Show all posts

Convert from Decimal to Binary using Recursion in Java

Problem:

Write a method that converts from Decimal to Binary using Recursion in Java.

Output:

Not applicable.

Solution:

 public static void convert(int number)
  {
    if (number > 0)
    {
      convert(number / 2);
      System.out.print(number % 2);
    }
  }
Read More

Convert from Decimal to Binary using Stacks in Java

Problem:

To convert a number from decimal to binary, you simply divide by two until a quotient of zero is reached, then use the successive remainders in reverse order as the binary representation. Use a stack to store the remainder of the division and finally print the binary representation. The first line of input n is the number test cases. Each test case begins with an integer t representing the number of integers in the list, then t integers follow.

Output:

3
5
7
10

101
111
1010

Solution:

import java.util.Scanner;
import java.util.Stack;

public class ProblemD {

 public static void main(String[] args) {
  
  Stack<Integer> stack = new Stack<Integer>();
  
  Scanner scan = new Scanner(System.in);
  
  int numberOfExpressions = scan.nextInt();
  int[] num = new int[numberOfExpressions];
  
  for (int i = 0;i<numberOfExpressions;i++)
  {
   num[i] = scan.nextInt();
  }
  
  for (int i = 0;i<numberOfExpressions;i++)
  {
   if (num[i] == 0) {
                System.out.println("0000");
   } else if (num[i] == 1) {
                System.out.println("0001");
   } else {
                while (num[i] != 0) {
                        int a = num[i] / 2;
                        int b = num[i] % 2;
                        stack.push(b);
                        num[i] = a;
                }
        }
   
   while (!stack.empty()) {
                System.out.print(stack.pop());
        }

   System.out.println(" ");
  }

 }
}
Read More

Converting from Integer to Binary using Recursion in Java

Problem:

Write a recursive function that converts an integer into its binary number representation.

Output:

Not needed.

Solution:

public class ProblemF {
 
    private static String toBinary(int num)
    {
     if(!(num/2==0))
      return ""+ toBinary(num/2)+""+num%2+"";
      else
      return ""+1+"";
    }   
}
Read More

Finding Heads and Tails of a Number in Java

Problem:

Nine coins are placed in a 3-by-3 matrix with some face up and some face down. You can represent the state of the coins using a 3-by-3 matrix with values 0 (head) and 1 (tail). Here are some examples:

0 0 0       1 0 1       1 1 0        1 0 1        1 0 0
0 1 0       0 0 1       1 0 0        1 1 0        1 1 1
0 0 0       1 0 0       0 0 1        1 0 0        1 1 0

Each state can also be represented using a binary number. For example, the preceding matrices correspond to the numbers:

000010000 101001100 110100001 101110100 100111110

There are a total of 512 possibilities. So, you can use decimal numbers 0, 1, 2, 3,
and 511 to represent all states of the matrix. Write a program that prompts the
user to enter a number between 0 and 511 and displays the corresponding matrix
with characters H and T.


Output:

Enter a number between 0 and 511
458


T T T
H H T
H T H


Solution:

  import java.util.Scanner;
public class HeadsAndTails
{
  public static int[] decimalToBinary(int decimal)
  {
  int[] nums = new int[9];
  for (int i = nums.length-1;i>=0 && decimal >0;i--)
  {
    if ( decimal% 2 == 0)
      nums[i] = 0;
    else 
      nums[i] = 1;
    decimal /= 2;
  }
  return nums;
  }

  public static void printHeadsAndTails(int[] nums)
  {
    for (int i =0; i<9;i++)
    {
      if (nums[i] == 0) System.out.print("H ");
      else  System.out.print("T ");
      if ((i+1) % 3 == 0) System.out.println();
      
    }
  }
  public static void main (String[] args)
  {
    Scanner scan = new Scanner (System.in);
    System.out.println("Enter a number between 0 and 511");
    int number = scan.nextInt();
    int[] nums = decimalToBinary(number);
    printHeadsAndTails(nums);
  }
}
Read More

Converting form Decimal to Binary in Java

Problem:

Write a program that reads a string in binary form from the user (i.e of the form "001010101") and returns the integer equivalent of the string (starting from right to left for the lest significant bit). For example, "0101" is from right to left:
(1* 2^0) + (0* 2^1) + (1* 2^2) + (2* 2^3) which is equal to 5.


Output:

Please enter the binary string:
0001 

The integer value is 1


Solution:

There are two methods to solve this problem. They are listed respectively.
import java.util.Scanner;
public class Problem2version1 
{
 
 public static void main(String[] args)
 {
  Scanner scan=new Scanner(System.in);
  
  System.out.println("Please enter the binary string:");
  String input=scan.next();
  int sum=0;
  int power2=1;
  for(int n=input.length()-1;n>=0;n--)
  {
   if(input.charAt(n)=='1')
    sum += power2;
    
   power2 *= 2;
  }
  System.out.println("The integer value is "+sum);
 }
}

import java.util.Scanner;

public class Problem2
{
 public static void main(String [] args)
 {
  Scanner scan = new Scanner(System.in);
  System.out.println("Please enter the binary string: ");
  String s = scan.next();
  int L = s.length();
  int i = 0;//iterator
      double k = 0;//the value of 1 in the string everytime
  double sum = 0;//the total sum
 
  while (i
Read More

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact