| | |
Convert Binary to Decimal and from Decimal to Binary
Thread Solved
![]() |
•
•
Join Date: Nov 2008
Posts: 3
Reputation:
Solved Threads: 0
Hi, I have a program problem with converting decimals to binary and back again. My teacher said that the program is to be like a menu and convert decimals or binary from the user's input. This is part of my code:
I know my problem is with the binary(int number) function because it is runnable but dosen't call to the function. So, my question is how can I modify this to run correctly? Also, how would I go about reversing the code to have it convert binary to decimal?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; void binary(int number) { int remainder; if(number <= 1) cout << number; remainder = number % 2; binary(number >> 1); cout << remainder; } int main(){ int number; int choice; cout << "1. Binary to Decimal\n"; cout << "2. Decimal to Binary\n"; cout << "3. Quit Program\n"; cout << "Enter your Choice: "; cin >> choice; switch (choice) { case '1': cout << "Please enter an integer: "; cin >> number; if (number < 0) cout << "Error enter positive integer: "; else { cout << "The number in Binary is: "; binary(number); cout << endl; } } system("pause"); return 0; }
Last edited by Narue; Nov 20th, 2008 at 6:51 pm. Reason: added code tags
Nice thought to use the recursive function...
recursive function should have the terminating point which in the conversion always turn to 1 at last.
recursive function should have the terminating point which in the conversion always turn to 1 at last.
cpp Syntax (Toggle Plain Text)
void binary(int num) { if(num!=1) { int rem=num%2; binary(num/2); cout<<rem; } else cout<<num; }
Last edited by Rhohitman; Nov 20th, 2008 at 6:59 pm.
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
•
•
•
•
Thanks Rohitman. So, How do I reverse it as in binary to decimal? I know it has something to do with diffrent powers of a number like 2x^i. So, do I use a for loop for that or something else?
In any number system is represented by the general expression
a[n]*r^[n] + a[n-1]*r^[n-1] +........+ a[2]*r^[2] + a[1]*r^[1]+a[0] //integer part
+ a[-1]*r^[-1] + + a[-1]*r^[-2].......+ a[-m]*r^[-m] //fraction part
where,
a[j] is the coefficient
r is radix or base
For decimal no system r = 10; and coefficient can have the value from 0-9
Similarly the binary no system base will be 2, and can have only 2 values 0 and 1.
I guess concept would be clear...
best of luck for programming..
Last edited by Rhohitman; Nov 20th, 2008 at 9:50 pm.
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
![]() |
Similar Threads
- Help With Decimal to Binary conversion (IT Professionals' Lounge)
- Converting Decimal Value to Binary (MIPS) (Assembly)
- Convert decimal to binary and with base the 8 (C)
- Number System:Conversion from binary to decimal (C)
- i need code for binary to decimal (it should be written in 'C') (C)
Other Threads in the C++ Forum
- Previous Thread: Install Directory of another program
- Next Thread: Linked List - Need help deleting!
| Thread Tools | Search this Thread |
api array auto based binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count delay-loading delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets





