You are here:   HomeJava ProgrammingJava ArrayConvert Binary integer to Decimal Number Example | Java Examples
by Dinesh Thakur Category: array

Explanation of Math.pow():

Pow()method is a static method of math class present in java.lang package.

This method takes two double type argument and returns the value if the 1st argument which is raised to the power of the 2nd argument.

Syntax:

Public static double pow(double dl,double d2)

Algorithm for Convert Binary integer to Decimal Number:

step 1: set p=0, r=0

step 2: read num

step 3: create an integer array a[] of size 20

step 4: repeat through step-7 while num greater than 0

step 5: a[p]=num%l0

step 6: num= num/ 10

step 7: p=p+ 1

step 8: set q=p-l

step 9: set r=0

step 10: repeat through step-12 while q greater than or equals to 0

step 11: r=r+(a[q]*Math.pow(2, q))

step 12: q=q-1

step 13: print r

step 14: Exit

 

Here is the Java Example for Convert Binary integer to Decimal Number:

import java.util.Scanner;

public class ConvertBintoDec
{
                 public static void main(String args[])
         {
                 int num,p=0,q,r=0;
                 Scanner sl=new Scanner(System.in);
                 System.out.print("Enter Binary Number : ");
                 num=sl.nextInt();
                 int a[]=new int[20];
                    while(num>0)
                           {
                              a[p]=num%10;
                              num=num/10;
                              p++;
                           }
                                 q=p-1;
                                 r=0;
                                  while(q>=0)
                                         {
                                             r=(int)(r+(a[q]*Math.pow(2,q)));
                                             q--;
                                          }
                                 System.out.print("Decimal Number is :-> "+r);
              }
}

Convert Binary integer to Decimal Number Example





Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article