Implement a method that takes any binary number in binary as well as in in decimal notation and use this to output your results. string parse Binary And Decimal (string binary code example

Example 1: convert binary to decimal c++ stl

string bin_string = "10101010";
int number =0;
number = stoi(bin_string, 0, 2);
// number = 170

Example 2: Java program to convert decimal to binary using toBinaryString and stack

Convert decimal to binary using stack in java
import java.util.*;
public class DecimalBinaryExample
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);                
      Stack<Integer> numStack = new Stack<Integer>();     
      System.out.println("Please enter a decimal number : ");
      int number = sc.nextInt();
      while(number != 0)
      {
         int a = number % 2;
         numStack.push(a);
         number /= 2;
      }
      System.out.println("Binary number : ");
      while(!(numStack.isEmpty()))
      {
         System.out.print(numStack.pop());
      }
      System.out.println();
      sc.close();
   }
}

Tags:

Cpp Example