-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinary_decimal.cpp
More file actions
60 lines (59 loc) · 1.49 KB
/
Copy pathbinary_decimal.cpp
File metadata and controls
60 lines (59 loc) · 1.49 KB
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
54
55
56
57
58
59
60
#include <stdio.h>
#include <math.h>
//Function to convert binary to decimal
int convertB2D(long long n)
{
int decimalNumber = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimalNumber += remainder*pow(2,i);
++i;
}
return decimalNumber;
}
//Function to convert decimal to binary
long long convertD2B(int n)
{
long long binaryNumber = 0;
int remainder, i = 1, step = 1;
while (n!=0)
{
remainder = n%2;
printf("Step %d: %d/2, Remainder = %d, Quotient = %d\n", step++, n, remainder, n/2);
n /= 2;
binaryNumber += remainder*i;
i *= 10;
}
return binaryNumber;
}
int main()
{
long long n;
int choice,num;
char ch;
printf ("**** MENU ****\n");
printf ("\n1.Convert from Binary to Decimal");
printf ("\n2.Convert from Decimal to Binary\n");
do
{
printf ("\nEnter your choice: ");
scanf ("%d",&choice);//Entering choice
switch(choice)
{
case 1: printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convertB2D(n));
break;
case 2: printf("Enter a decimal number: ");
scanf("%d", &num);
printf("%d in decimal = %lld in binary", num, convertD2B(num));
break;
default:printf("Wrong choice");
}
printf ("\n\nPress '1' to continue and any other integer to exit: ");
scanf ("%d",&ch);
}while(ch==1);
return 0;
}