DEC to BIN converter

What is bad in this for cycle?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
int main(){
int rem;
int num;
int i;
cout << "Type in decimal number";
cin >> num;
cout << endl;
for (i = num; i > 0; i / 2)
	{
		rem = i % 2;
		cout << "Binary:" << rem;
	}
}
return 0;
A couple of problems:
Line 10: You're not modifying i for each iteration.

Line 13: You're going to repeat "Binary: " for each binary digit.

Lines 15 and 16 are reversed.

and when i try to use cycle without i

1
2
3
4
5
for (num; num > 0; num / 2)
	{
		rem = num % 2;
		cout << rem;
	}

Still not working
num / 2 is doing a calculation, but there is not assignment so the value isn't changing. You need to use num/= 2 instead.

Your program also outputs the number backwards, so you may want to fix that.
Last edited on
Topic archived. No new replies allowed.