Closed ErSKS closed 7 years ago
package decimalbinaryconv;
/*
@author m-lan */ public class DecimalBinaryConv {
/**
static void binary2Decimal(int n) { int d = 0, power = 0; while (true) { if (n == 0) { break; } else { int any = n % 10; d = (int) (d + any * Math.pow(2, power)); n = n / 10; power++; } } System.out.printf("%d", d); }
static void decimal2Binary(int n) { int[] binary = new int[25]; int i = 0; while (n > 0) { binary[i++] = n % 2; n = n / 2; } for (int j = i - 1; j >= 0; j--) { System.out.printf("%d", binary[j]); }
}
}
package binaryanddecimal;
import static java.lang.Math.pow; import java.util.Scanner;
/*
@author Jagdish Duwal */ public class BinaryAndDecimal {
/**
static int binary2Decimal(int x){
int power =0,dec=0;
while(x>0){
int a = x%10;
x /= 10;
dec = (int) (dec + a*pow(2,power));
power++ ;
}
return dec;
}
static String decimal2Binary(int x){ String revBin="",bin = ""; int r; while(x>0){ r = x %2; x /= 2; revBin = revBin.concat(Integer.toString(r));
//bin = Integer.toString(parseInt(bin) *10).concat(Integer.toString(r));
}
for(int i = revBin.length(); i>0; i--){
bin = bin.concat(revBin.substring(i-1,i));
}
return bin;
}
public static void main(String[] args) { int x ,param; String again; Scanner input = new Scanner(System.in); do{ System.out.println("Enter a binary no or decimal no: "); x= input.nextInt();
System.out.println("Enter your choice: \n1.binary2Decimal \n 2.decimal2Binary \n");
int choice = input.nextInt(); switch(choice){
case 1:
int binCheck=0;
param= x;
while(x>0){
int a = x%10;
x /= 10;
if( a!=0 && a!=1){
binCheck++;
}
}
if(binCheck >0){
System.out.println("Sorry. Not a binary no.");
}else{
System.out.println("the decimal equivalent to the input binary is "+binary2Decimal(param));
}
break;
case 2:
int decCheck=0;
if( x<0){
decCheck++;
}
if(decCheck >0){
System.out.println("Sorry. Not a decimal no.");
}else{
System.out.println("the binary equivalent to the input decimal is "+decimal2Binary(x));
}
break;
default:
System.out.println("Invalid choice");
}
System.out.println("type 'y' if you want to run again");
again = input.next();
}while(again.equalsIgnoreCase("y"));
}
}
/*
import java.util.Scanner;
/*
@author Anish */ public class BtoD_DtoB {
public static void decimal2Binary(int num) { int binary[] = new int[40]; int i = 0; while (num > 0) { binary[i] = num % 2; num = num / 2; i++; } for (int j = i - 1; j >= 0; j--) { System.out.print(binary[j]); } }
public static void binary2Decimal(int num) { int decimal = 0, p = 0; while (true) { if (num == 0) { break; } else { decimal += (num % 10) * Math.pow(2, p); num = num / 10; p++; } } System.out.print(decimal); }
/**
}
/*
import java.util.Scanner;
/*
@author DELL */ public class Conversioni {
static int binary2Decimal(int n) { int rem, d = 0, b = 1; while (n > 0) {
rem = n % 10;
d = d + rem * b;
n = n / 10;
b = b * 2;
}
System.out.println("Its decimal equivalent is: " + d);
return n;
}
static int decimal2Binary(int n) { int rem, b = 0, i = 1; while (n > 0) {
rem = n % 2;
b = b + rem * i;
n = n / 2;
i = i * 10;
}
System.out.println("Its binary equivalent is: " + b);
return n;
}
public static void main(String[] args) { Scanner input = new Scanner(System.in); int bi, de, n, check, temp; System.out.print("1. Binary to Decimal conversion.\n2. Decimal to Binary conversion.\n"); n = input.nextInt(); switch (n) { case 1: { System.out.println("Enter the valid binary number"); bi = input.nextInt(); binary2Decimal(bi); break; } case 2: { System.out.println("Enter the valid decimal number"); de = input.nextInt(); decimal2Binary(de); break;
}
}
}
}
OUTPUT
/*
import java.util.Scanner;
/*
@author nissus */ public class Task103 { static int binary2Decimal(int n) { int r, k = 0, b = 1; while (n > 0) {
r = n % 10; k = k + r b; n = n / 10; b = b 2;
} System.out.println("Its decimal equivalent is: " + k); return n; }
static int decimal2Binary(int n) { int r, k = 0, i = 1; while (n > 0) {
r = n % 2;
k = k + r * i;
n = n / 2;
i = i * 10;
} System.out.println("Its binary equivalent is: " + k); return n; }
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String check = "y";
while (check.equalsIgnoreCase("y")) { Scanner input=new Scanner(System.in); System.out.println("Enter 1:Binary to Decimal\n \t2:Decimal to Binary");
int x=input.nextInt();
switch (x)
{
case 1:
System.out.println("Enter a binary number");
input=new Scanner(System.in);
int bin=input.nextInt();
binary2Decimal(bin);
break;
case 2:
System.out.println("Enter a decimal number");
input=new Scanner(System.in);
int dec=input.nextInt();
decimal2Binary(dec);
break;
default:
System.out.print("Invalid input");
break;
}
System.out.print("\nDo you want to try again?(Y/N):");
check = input.next();
}
}
}
/*
import java.util.Scanner;
/*
@author kajal */ public class BinToDec_DecToBin {
static int binToDec(int n) { int r, d = 0, b = 1; while (n > 0) { r = n % 10; d = d + r b; n = n / 10; b = b 2; } System.out.println("the decimal equivalent is: " + d); return n; }
static int decToBin(int n) { int r, b = 0, i = 1; while (n > 0) {
r = n % 2;
b = b + r * i;
n = n / 2;
i = i * 10;
}
System.out.println("Its binary equivalent is: " + b);
return n;
}
/**
@param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner s = new Scanner(System.in); int b, d, n; String a = "y"; while (a.equalsIgnoreCase("y")) { System.out.print("Enter your choice\n1.Binary to Decimal\n2.Decimal to Binary\n"); n = s.nextInt(); switch (n) { case 1: System.out.print("\nEnter the binary number:"); b = s.nextInt(); binToDec(b); break;
case 2:
System.out.print("\nEnter the decimal number:");
d = s.nextInt();
decToBin(d);
break;
default:
System.out.print("Invalid input");
break;
}
System.out.print("\nPress Y to continue\n");
a = s.next();
}
}
}
/*
import static java.lang.Integer.parseInt; import java.util.Scanner;
/*
@author Dragon15423 */ public class Tasks2 {
/**
@param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); boolean stat = true, st = true; System.out.println("Enter any binary number(eg:101011): ");
while (stat == true) {
String number = input.nextLine();
stat = !number.matches("[0-1]+");
if (stat == true) {
System.out.println("Please enter a valid binary number (eg: 101101) = ");
} else {
binary2Decimal(number);
}
}
System.out.println("Enter any decimal number(eg:123456): ");
while (st == true) {
String number = input.nextLine();
st = !number.matches("[0-9]+");
if (st == true) {
System.out.println("Please enter a valid decimal number (eg: 123456) = ");
} else {
decimal2Binary(number);
}
} }
static void binary2Decimal(String number) { int a = number.length(), s, b = 0, c = number.length() - 1;
for (int i = 0; i < a; i++) {
s = Character.getNumericValue(number.charAt(i));
s *= Math.pow(2, c);
b += s;
c--;
}
System.out.println("The Decimal equivalent of (" + number + ") is : " + b);
}
static void decimal2Binary(String number) { int a = number.length(), r; int d = parseInt(number); System.out.print("The Binary equivalent of (" + number + ") is : ");
while (d != 0) {
r = d % 2;
d = d / 2;
System.out.print(r + "");
}
}
}
/*
/*
@author Kiran */ public class BinaryConversion {
/**
@param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here System.out.println("Converting decimal to binary..."); System.out.println("Binary of 2 \t : " + toBinary(2)); System.out.println("Binary of 5 \t : " + toBinary(5)); System.out.println("Binary of 27 \t : " + toBinary(27));
System.out.println("-------------------------------\nConverting binary to decimal..."); System.out.println("Decimal of 10 \t : " + toDec(10)); System.out.println("Decimal of 101 \t : " + toDec(101)); System.out.println("Decimal of 11011 : " + toDec(11011)); }
static int toBinary(int n) { int r = 0, i = 0, ans = 0, t = n; while (t > 0) { if (t % 2 == 0) { i++; t /= 2; } else { break; } } while (n > 0) { r = r 10 + n % 2; n /= 2; } while (r > 0) { ans = ans 10 + r % 10; r /= 10; } ans *= Math.pow(10, i); return ans; }
static int toDec(int n) { int r, d = 0, i = 0; while (n > 0) { r = n % 10; d += r * Math.pow(2, i); n /= 10; i++; } return d; } }
Output:
/*
import java.util.Scanner;
/*
@author Dell */ public class Convertor {
/**
@param args the command line arguments / static int binarytodecimal(int n) { int c, b = 1, d = 0; while (n > 0) { c = n % 10; d = d + c b;
n = n / 10;
b = b * 2;
} System.out.println("decimal eqivalent:" + d); return n; }
static int decimaltobinary(int n) { int b = 1, c, d = 0; while (n > 0) { c = n % 2; d = d + c b; n = n / 2; b = b 10; } System.out.println("Eqivalent binary:" + d); return n; }
public static void main(String[] args) { // TODO code application logic here String check = "y"; Scanner in = new Scanner(System.in); while (check.equalsIgnoreCase("y")) { System.out.println("Enter choice\n"
}
/*
import java.util.Scanner;
/*
@author LORDsajan */ public class BinDec {
/**
static int BinToDec(int n) { int dec = 0, i = 0; while (n > 0) { dec = (int) (dec + (n % 10) * Math.pow(2, i)); n = n / 10; i++; } return dec; }
public static void main(String[] args) { Scanner inp = new Scanner(System.in); String ch; do { System.out.println("Enter your choice\n1.Binary to Decimal \n2.Decimal to Binary"); int n = inp.nextInt(); switch (n) { case 1: System.out.println("enter a binary number:"); int bno = inp.nextInt(); System.out.println("The decimal of " + bno + " is :" + BinToDec(bno)); break; case 2: System.out.println("enter a decimal number:"); int dno = inp.nextInt(); DecToBin(dno); break; default: System.out.println("invalid"); break; } System.out.println("\nContinue?(y/n)"); ch = inp.next(); } while (ch.equalsIgnoreCase("y"));
}
}
/*
import static java.time.Clock.system; import java.util.Scanner;
/*
@author dell */ public class BinaryTodecimalANdDEcimalToBinary {
/**
@param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int choice; String check = "y"; while (check.equalsIgnoreCase("y")) {
System.out.println("Enter your choice \n 1: binary o decimal \n 2:decimal to binary");
Scanner ch = new Scanner(System.in);
choice = ch.nextInt();
switch (choice) {
case 1: {
Scanner s = new Scanner(System.in);
System.out.println("Enter a binary number:");
int n = s.nextInt();
int decimal = 0, p = 0;
while (n != 0) {
decimal += ((n % 10) * Math.pow(2, p));
n = n / 10;
p++;
}
System.out.println("Decimal value is: " + decimal);
}
break;
case 2: {
Scanner s = new Scanner(System.in);
System.out.println("Enter a decimal number:");
int n = s.nextInt();
int r, b = 0, i = 1;
while (n > 0) {
r = n % 2;
b = b + r * i;
n = n / 2;
i = i * 10;
}
System.out.println("Binary value is: " + b);
}
break;
default:
System.out.println("Invalid");
break;
}
System.out.println("Do you want to continue if yes pres y else press anything");
check = ch.next();
} }
}
Develop a program consisting two methods: binary2Decimal() & decimal2Binary().