Showing posts with label conversion. Show all posts
Showing posts with label conversion. 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

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