PLEASE HELP ME WITH MY HOMEWORK !

BIT CODES.
Create a program that will ask the user to enter a decimal value (1-999999) then display its
corresponding binary numbers. Repeat this process until the value entered is equal to 0.
Use the following Function Prototype:
void BinCodes(int value);
Sample Input/Output:
Enter a Decimal: 35
Binary: 100011
Enter a Decimal: 184
Binary: 10111000
Enter a Decimal: 0
Forum Guide wrote:
Don't post homework questions
Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions.
http://www.wikihow.com/Convert-from-Decimal-to-Binary

There's an algorithm here on how to do it. I did the same homework problem a long time ago.
Last edited on
I just did your homework in less than 12 minutes.

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
#include <iostream>
#include <string>
#include <algorithm>

void BinCodes(int value){
	enum boolean {FALSE, TRUE};
	boolean condition = TRUE;
	int rollover_value = value;
	std::string str1;	
		while(condition){
			str1.push_back(rollover_value % 2);
			rollover_value = rollover_value / 2;	
				if(0 == rollover_value){
					condition = FALSE;
				}
		}
		for(int i = 0; i < str1.length();i++){
			str1[i] += 48;
		}
	std::reverse(str1.begin(),str1.end());
	//http://www.cplusplus.com/forum/general/110608/
	std::cout << "Binary: " << str1 << std::endl;
	
}
int main(){
	int user_input;
	std::cout << "Enter a number: ";
	std::cin >> user_input;
	BinCodes(user_input);
}
Last edited on
Thank u sir !
Topic archived. No new replies allowed.