-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseConversion.java
53 lines (43 loc) · 1.09 KB
/
BaseConversion.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
Copyright (C) Deepali Srivastava - All Rights Reserved
This code is part of DSA course available on CourseGalaxy.com
*/
import java.util.Scanner;
public class BaseConversion
{
BaseConversion() {} //this class is not for instantiation
public static void toBinary(int n)
{
if(n==0)
return;
toBinary(n/2);
System.out.print(n%2);
}
public static void convertBase(int n,int base)
{
if(n==0)
return;
convertBase(n/base,base);
int remainder=n%base;
if(remainder<10)
System.out.print(remainder);
else
System.out.print((char) (remainder-10+'A'));
}
public static void main(String[] args)
{
int n;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a positive decimal number : ");
n=scan.nextInt();
System.out.print("Binary form : "); toBinary(n);
System.out.println();
System.out.print("Binary form : "); convertBase(n,2);
System.out.println();
System.out.print("Octal form : "); convertBase(n,8);
System.out.println();
System.out.print("Hexadecimal form : "); convertBase(n,16);
System.out.println();
scan.close();
}
}