-
Notifications
You must be signed in to change notification settings - Fork 1
/
[Array]Binary.cpp
53 lines (53 loc) · 923 Bytes
/
[Array]Binary.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
49
50
51
52
53
/*
*File name:
*Source file name:
*Author: Tamkien Cao
*Last modified: Fri, Mar 22nd, 2019
*Other info:
-Exercise 5.8
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[16], n;
printf("This program can convert decimal number into binary number (enter 0 to exit)\n");
while (1)
{
printf("Enter your number here:\t");
scanf_s("%d", &n);
if (n < 0 || n>65535)
{
printf("Out of the limit!");
}
else
{
printf("Your binary is ");
if (n == 0)
{
printf("0000 0000 0000 0000\n");
break;
}
else
{
for (int i = 15; i >= 0; i--)
{
a[i] = n % 2;
n = n / 2;
}
for (int i = 0; i <= 15; i++)
{
printf("%d", a[i]);
if (i % 4 == 3)
{
printf(" ");
}
}
}
}
printf("\n");
}
printf("\n=================================\n");
printf("Written by Tamkien Cao. Thank you for using my application!\n");
system("pause");
}