C++
x
23
1
2
using std::cin;
3
using std::cout;
4
5
int main(void) {
6
// convert dec to bin, but throws error for i:
7
// must be pointer, but is of type unsigned int
8
int a[10], n;
9
unsigned int i;
10
cout <<"Enter the number to convert: ";
11
cin >> n;
12
for(i = 0; n > 0; i++)
13
{
14
a[i] = n%2; // stores remainder of each loop in array <a>
15
n = n/2;
16
}
17
cout << "Binary of the given number: 0b";
18
for(i = i-1 ;i >= 0 ;i--) // "reassambles" items of <a> to binary
19
{
20
cout << a[i];
21
}
22
}
23
$ g++ prog.cc -Wall -Wextra -std=c++11 -pedantic
Stdin
1000
Start
prog.cc: In function 'int main()': prog.cc:18:19: warning: comparison of unsigned expression in '>= 0' is always true [-Wtype-limits] 18 | for(i = i-1 ;i >= 0 ;i--) // "reassambles" items of <a> to binary | ~~^~~~
Enter the number to convert:
Bus error
Finish