By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 2,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

Binary representatio of unsigned integer

P: 1
#include <iostream>

using namespace std;
int ConvertToBinary (int);
int main()
int num;
cout << "Input Number";
cin >> num;
cout << ConvertToBinary(num);
return 0;

int ConvertToBinary(int n)
int binary;
for(int i = n; i > 0;i = int (i/2))
binary =i % 2;
cout << binary;
return binary;
// it should output 111 but it ouput 1111
Oct 4 '15 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 9,197
The output of your function is 111.

But then you go on to display the return value of ConvertToBinary, which is 1 from the last iteration of your loop.

So you see 1111.
Oct 4 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.