Sunday, 17 July 2011

Write a program to convert a binary number into decimal using inheritance

public class Binpow
{
public int pow(int n)
{
int res=1;
if(n!=0)
{
for(int i=1;i<=n;i++)
res=2*res;
n++;

}
return res;
}
}

import java.io.*;
public class Binary extends Binpow
{
int g=1;
int bin;
int dec;
int rem=1;
public void input()
throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER the binary number");
bin=Integer.parseInt(br.readLine());
}
public void output()
{

}
public void cal()
{
int n=0;
int h=0;System.out.println("check");
while(rem!=0||bin!=0)
{

rem=bin%10;
bin=bin/10;
h=pow(n);
dec=rem*h+dec;
n++;
}
System.out.println("decimal number ="+dec);
}
}
import java.io.*;
public class Bimain
{
public static void main(String[] arg)
throws IOException
{
Binary c=new Binary();
c.input();
c.cal();

}
}

No comments:

Post a Comment

 

About The Author


I am a B.Tech Computer Science Student. I love Programming and blogging. I am Oracle certified Java Professional. Read More
Indian Blogger

Teachers Jobs

Footer 3

Twitter Facebook RSS YouTube Google
​

All Rights Reserved ©Sun Java Masters 2013