Binary to Decimal Conversion in Java [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to write a program in java which takes decimal number as an int parameter and prints to the screen the binary equivalent of that decimal number ...
In short decimal to binary conversion using only int parameters ...
Solution will be much appreciated ... i have tried some steps but i am not successful ..
Thanks

Integer.toString(input, 2);
OR
Integer.toBinaryString(input);
You might also want checkout:
Integer.toHexString
Integer.toOctalString
INPUT : 10 (decimal)
RESULT: 1010 (binary)
Hope this helps :)

Or you can write program like this (modify it):
import java.util.Scanner;
public class DecToBinary {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int broj;
System.out.print("Enter number that you want to conver to binary: ");
broj = input.nextInt();
convert(broj);
}
public static void convert(int a) {
String obrnuti = "";
while (a > 0) {
int x = a % 2;
obrnuti += x;
a /= 2;
}
System.out.println(reverse(obrnuti));
}
public static String reverse(String a) {
String novi = "";
for (int i = a.length() - 1; i >= 0; i--) {
char c = a.charAt(i);
novi += c;
}
return novi;
}
}

conversion from decimal to binary :
Integer.toBinaryString(Int_variable)
conversion from binary to decimal:
Integer.parseInt(string_binary_variable,2)

public class DecimalToBinary
{
// 5 = 101
public static void main(String[] args)
{
int d2b = dec2Bin(5);
System.out.println(d2b);
}
public static int dec2Bin(int num){
int a = num;
int binary=0;
int i=1;
while(a != 0){
binary = binary+i*(a%2);
a = a/2;
i=i*10;
}
return binary;
}
}

public class BinaryToDecimal {
public static void main(String[] args) {
String binary = "110";
System.out.println(binToDec(binary));
}
public static int binToDec(String binary){
int decimal=0;
for(int i=0;i<binary.length();i++){
decimal = 2*decimal + Integer.parseInt("" + binary.charAt(i));
}
return decimal;
}
}

Related

java extended class inheritance [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Sum s = new Sum();
Sum.SetToZero z = new Sum.SetToZero();
Scanner input = new Scanner(System.in);
String read = input.nextLine();
while (!read.equals("end")) {
if (read.equals("add")) {
s.add()
}
else if (read.equals("get")) {
System.out.println(s.returnTotal());
}
else if (read.equals("zero")) {
z.zero();
}
read = input.nextLine();
}
class:
public class Sum {
int total = 0;
public void add() {
total += 1;
}
public int returnTotal() {
return total;
}
public static class SetToZero extends Sum {
public void zero() {
total = 0;
}
}
}
input:
add
add
zero
add
get
add
get
end
output:
3
4
output wanted:
1
2
Shouldn't the subclass inherit the total and set it to zero? What am I doing wrong? I know I could just move the zero into the main class but I want it to be in a separate class. thx for your help.
By making your total variable static, you can get the desired output.
class Sum {
static int total = 0;
public void add() {
total += 1;
}
public int returnTotal() {
return total;
}
public static class SetToZero extends Sum {
public void zero() {
total = 0;
}
}
}
Apart from things pointed out in names like not using lowecase letters to start your class name; I think the reason why it's not working is because you're using two different variable instances for Sum and Sum.SetToZero. You don't need to create a new variables since SetToZero has all the attributes of Sum. I think you should change this:
Sum s = new Sum();
Sum.SetToZero z = new Sum.SetToZero();
Sum.SetToZero s = new Sum.SetToZero(); // use s for all operations
Here is how your modified main method would look:
public static void main(String[] args) {
Sum.SetToZero s = new Sum.SetToZero();
Scanner input = new Scanner(System.in);
String read = input.nextLine();
while (!read.equals("end")) {
if (read.equals("add")) {
s.add();
}
else if (read.equals("get")) {
System.out.println(s.get());
}
else if (read.equals("zero")) {
s.zero();
}
read = input.nextLine();
}
}
When I ran this, I saw expected output:
src : $ java Sum
add
add
zero
add
get
1
add
get
2
end

Create an array checking previous items in another array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to create an array of numbers, starting from a big number, which is a string, and then splited to create an array.
This array has numbers from 1 to 6.
I want to create a new array of 29 items, checking the last 3 items. For instance, if the current item to add is number 4, it will check for item 3, 2, and 1. I want it to check if any of the other 3 items are the same as the current, and if is the same, it will skip to next number.
The idea, is not to have repeated numbers in a "range" of 4 items.
I also want to do another thing, and is that if the current item is number 1, it would check the last 6 items, instead of just the last 3. Only when the item is number 1. I just haven't found a way to do it.
I hope someone could help me.
Here's the current code:
import java.io.*;
import java.util.*;
public class Randomizer {
public static String numbers = "1632544362511653244213652164535316243164251654235231465312645164233462153465124361522316545326412354165412633214654531626513246124351423565431623162453625412564325134634562136145256142342365161423542563151426325146335241613645225431656324164351215263453642123416561345264215321536436451243156263542125631415624314536235124662145152436352164436512645312264315432651216345624315421563516423465213614352621543342516352614";
public static void main(String[] args) {
show("\n");
String[] nums = numbers.split("(?!^)"), finalNums = new String[29];
for (int i = 0; i < finalNums.length; i++) {
finalNums[i] = newnumber(nums, i);
}
for (String num : finalNums) {
show(num + "-");
}
}
public static String newnumber(String[] array, int start) {
String num = "x";
String act = array[start];
for (int i = start; i < array.length; i++) {
int a = start - 1, b = start - 2, c = start - 3, d = start - 4;
if (a >= 0 && b >= 0 && c >= 0 && d >= 0) {
String na = array[a], nb = array[b], nc = array[c], nd = array[d];
if (act != na && act != nb && act != nc && act != nd) {
num = act;
} else {
}
} else {
num = act;
}
}
return num;
}
public static void show(String s) {
System.out.print(s);
}
public static void show(int s) {
System.out.print(s);
}
public static void show(float s) {
System.out.print(s);
}
public static void show(double s) {
System.out.print(s);
}
}
The output is:
1-6-3-2-5-4-4-3-6-2-5-1-1-6-5-3-2-4-4-2-1-3-6-5-2-1-6-4-5
What I expect to get is something like:
1-6-3-2-5-4-6-1-2-5-3-4-6-5-1-2-5-4-6-3-2-1-2-4-5-6-3-2-1

How do I convert decimal 75.95 to 7595 in Java?

I have a program in which i need to convert double 75.95 to normal integer 7595. How do I write the actual program?
My code is:
class test5 {
public static void main(String args[]) {
double d = 75.95;
System.out.println("Price before converting = "+d);
int i = (int)d;
System.out.println("(float)d = "+i);
}
}
Any particular reason why multiplying by 100 wouldn't work?
class test5 {
public static void main(String args[]) {
double d = 75.95;
System.out.println("Price before converting = "+d);
long i = Math.round(d * 100);
System.out.println("(float)d = "+i);
}
}
You have to be careful with rounding errors. See: Losing precision converting from int to double in java
Now my code is this and its working :) thanks all of you :)
class test5
{
public static void main(String args[])
{
double d = 75.95;
{
System.out.println("Price before converting = "+d);
int i = (int)(d*100);
System.out.println("Answer after converting = "+i);
}
}
}
You can multiply by a constant 100 (if there are only, and always two decimal points). If there are more, you could use String.valueOf(double) and remove the '.' and call a double a double (not a float). Something like
double d = 75.95;
// int val = (int) (d * 100); // <-- or this.
String str = String.valueOf(d).replace(".", "");
int val = Integer.valueOf(str);
System.out.printf("(double)%f = (int)%d%n", d, val);
Which outputs
(double)75.950000 = (int)7595

How to convert big int number to Binary ? - Java

I am trying to convert int to binary and i am doing below code.
public static String intToBinary16Bit(String strInt) {
String bin = Integer.toBinaryString(strInt);
return String.format("%016d", Integer.parseInt(bin));
}
So, if i am giving strInt = 0211 than it is working fine and giving the output
0000001000010001.
But, if i am giving strInt = 4527 than it is throwing NumberFormateException.
How can I resolved this issue ?
Try what eznme suggests here:
public class A {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
System.out.println(a);
int bit=1;
for(int i=0; i<32; i++) {
System.out.print(" "+(((a&bit)==0)?0:1));
bit*=2;
}
}
}
Try the following method, it uses recursion for conversion.
private static void toBinary(int number) {
int remainder;
if (number <= 1) {
System.out.print(number);
return;
}
remainder = number % 2;
toBinary(number >> 1);
System.out.println(remainder);
}
You try:
Long.toBinaryString(2199023255552L);
Java long to binary

Smallest and Largest integers using if/else only [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Here is my code to display smallest and largest integer by taking 5 inputs from user...it works for smallest values but not for largest and I cant figure out the problem...please help
import java.util.Scanner;
public class LargestAndSmallestIntegers {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int a,b,c,d,e;
int small,large;
System.out.println("Enter five integer values...");
a=input.nextInt();
small=a;
large=a;
b=input.nextInt();
if (small<b)
{
large=b;
}
else
{
small=b;
}
c=input.nextInt();
if (small<c)
{
large=c;
}
else
{
small=c;
}
d=input.nextInt();
if (small<d)
{
large=d;
}
else
{
small=d;
}
e=input.nextInt();
if (small<e)
{
large=e;
}
else
{
small=e;
}
input.close();
System.out.printf("%d is smallest and %d is largest", small,large);
}
}
private int a = input.nextInt(),
b = input.nextInt(),
c = input.nextInt(),
d = input.nextInt(),
e = input.nextInt();
private int small, large;
small = min(a,b);
small = min(small,c);
small = min(small,d);
small = min(small,e);
large = max(a,b);
large = max(large,c);
large = max(large,d);
large = max(large,e);
private int min(int a, int b) {
if (a < b) return a else return b;
}
private int max(int a, int b) {
if (a > b) return a else return b;
}
I think this works ;)
it works for smallest values but not for largest
As lared pointed out, comparing against small to determine large is flawed. You must include something like this in your comparison logic:
if (large < b)
{
large = b;
}
in addition to your existing comparison:
if (small > b)
{
small = b;
}
you always just check for the smallest value. you also need to check
if (small<e)
{
if(large < e)
{
large=e;
}
}

Categories

Resources