-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbase-conversion.cpp
50 lines (37 loc) · 876 Bytes
/
base-conversion.cpp
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
/*
Copyright (C) Deepali Srivastava - All Rights Reserved
This code is part of DSA course available on CourseGalaxy.com
*/
#include<iostream>
using namespace std;
void toBinary(int n);
void convertBase(int n, int base);
int main()
{
int num;
cout << "Enter a positive decimal number : ";
cin >> num;
cout << "Binary form "; toBinary(num); cout<<"\n";
cout << "Binary form "; convertBase(num, 2); cout<<"\n";
cout << "Octal form "; convertBase(num, 8); cout<<"\n";
cout << "Hexadecimal form "; convertBase(num, 16); cout<<"\n";
}
void toBinary(int n)
{
if( n == 0 )
return;
toBinary(n/2);
cout << n%2;
}
void convertBase(int n, int base)
{
int remainder;
if( n == 0 )
return;
convertBase(n/base, base);
remainder = n % base;
if( remainder < 10 )
cout << remainder;
else
cout << remainder - 10 + 'A' ;
}