-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntgerToBinary.java
32 lines (26 loc) · 1.02 KB
/
IntgerToBinary.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Scanner;
public class IntegerToBinary {
public static void main(String args[]) {
// Declare variables to store decimal number, quotient, and an array for binary digits
int dec_num, quot, i = 1, j;
int bin_num[] = new int[100];
// Create a Scanner object to read input from the user
Scanner scan = new Scanner(System.in);
// Prompt the user to input a decimal number
System.out.print("Input a Decimal Number: ");
dec_num = scan.nextInt();
// Initialize the quotient with the decimal number
quot = dec_num;
// Convert the decimal number to binary and store binary digits
while (quot != 0) {
bin_num[i++] = quot % 2;
quot = quot / 2;
}
// Display the binary representation of the decimal number
System.out.print("Binary number is: ");
for (j = i - 1; j > 0; j--) {
System.out.print(bin_num[j]);
}
System.out.print("\n");
}
}