Question: hello please help me as soon as , please put two code in one code (main), put two code in two functions please Means ,
hello please help me as soon as , please put two code in one code (main), put two code in two functions please
Means , one main ... and two functions (the first function converts From binary to decimal) (the second function converts from decimal to binary )
(DON'T USE CLASS JUST USED FUNCTION)
One program contains 2 function one main
THIS CODE..
//binary --|> decimal import java.util.Scanner; public class bin2dec { public static void main(String[] args) {
Scanner input = new Scanner(System.in); long binaryNumber, decimalNumber = 0, j = 1, remainder; System.out.print("Please input a binary number: "); binaryNumber = input.nextLong(); input.close(); //closes scanner. while (binaryNumber != 0) { remainder = binaryNumber % 10; decimalNumber = decimalNumber + remainder * j; j = j * 2; binaryNumber = binaryNumber / 10; } System.out.println("Its decimal Number: is " + decimalNumber); } }
//Extra: decimal --|> binary import java.util.Scanner; public class dec2bin { public static void main(String args[]) {
int dec_num, quot, i = 1, j; int bin_num[] = new int[100]; Scanner input = new Scanner(System.in); System.out.print("Please input a Decimal Number: "); dec_num = input.nextInt(); input.close(); quot = dec_num; while(quot != 0) { bin_num[i++] = quot%2; quot = quot/2; } System.out.print("Its binary number is: "); for(j=i-1; j>0; j--) System.out.print(bin_num[j]); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
