I'm taking my first C++ class at school. and our first project is to write a program that can convert a whole number between 1 and 8 to binary. I wrote a code that I feel should work and it does compile, however when I run it, it will only give me "0000" for the answer. Can somebody please help!
[code]
Put the code you need help with here.
#include <iostream>
using namespace std;
int main()
{
int whole_number, Remainder1, Remainder2, Remainder3, Remainder4, First, Second, Third, Fourth;
cout << "Enter a whole number between 1 and 8: ";
cin >> whole_number;
First = whole_number / 8;
Remainder1 = whole_number % 8;
Second = Remainder1 / 4;
Remainder2 = Second % 4;
Third = Remainder2 / 2;
Remainder3 = Third % 2;
Fourth = Remainder3 / 1;
cout << "The binary equivalent is " << First << Second << Third << Fourth << endl;
thanks for the reply. However when i put your program in DEV C++ and ran it, it still gave me all zeros for the final cout statement. Did it work for you?
I just added a bunch of cout's, and the modulus is not working correctly. when i input 7, it is correct when divided by 8. but then when it does 7 divided by 4, it says it is 1 with a remainder of 1, and then everything after that is giving zeros.. ive been trying to figure this out for hours and am extremely lost as to why this wont work