Conversion of decimal to binary

string octal = “”;

while(n!=0){
    int r = n%8;
    n/=8;
    char c = r+'0';
    octal = c+octal;
}
cout << octal <<endl;

i can’t understand the code please explain the same code

thanking you

Hey @AKSHET just like in conversion to binary we modulo and divide with 2, in the octal we modulo and divide with 8 because instead of 2 digits we can have 8 digits.

Here is detailed process for converting decimal numbers into octal numbers

Step 1: If the given decimal number is less than 8, the octal equivalent is the same. If the given number is greater than 7, divide the number by 8.
Step 2: Write down the remainder.
Step 3: Divide the part before the decimal point of your quotient by 8 again.
Step 4: Write down the remainder.
Step 5: Continue this process of dividing by 8 and noting the remainders until the last decimal digit you are left with is less than 8.
Step 6: When the last decimal digit is less than 8, the quotient will be less than 0 and the remainder will be the digit itself.
Step 7: The last remainder you get will be the most significant digit of your octal value while the first remainder from Step 3 is the least significant digit. Therefore, when you write the remainders in reverse order - starting at the bottom with the most significant digit and going to the top- you will reach the octal value of the given decimal number.

And you can see that we are taking remainder in r and dividing the number by 8 and then adding the character in front of the string making it more significant than the rest.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.