-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberConversation.java
More file actions
36 lines (33 loc) · 972 Bytes
/
Copy pathNumberConversation.java
File metadata and controls
36 lines (33 loc) · 972 Bytes
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
33
34
35
36
import java.util.Scanner;
public class NumberConversation{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = input.nextInt();
System.out.println("Binary Number : " + decToBinary(n) + "\nOctal Number : " + decToOct(n));
}
public static String decToBinary(int n){
// Scanner input = new Scanner(System.in);
//System.out.println("Enter a number : ");
// int n = input.nextInt();
String sum = "";
while(n > 0){
sum = "" + n%2 + sum ;
n/=2;
}
// System.out.println(sum);
return sum;
}
public static String decToOct(int n){
// Scanner input = new Scanner(System.in);
//System.out.println("Enter a number : ");
// int n = input.nextInt();
String sum = "";
while(n > 0){
sum = "" + n%8 + sum ;
n/=8;
}
// System.out.println(sum);
return sum;
}
}