A number system with a base 10 is known as decimal number system. Hence, decimal numbers are denoted with a base 10.
This number system consists of 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Each digit in the decimal system has a position and every digit is ten times more significant than the previous digit.
For example: (461)10 , (370)10 , (890)10 , (400)10 , etc.
A number system with a base 8 is known as octal number system. Hence, octal numbers are denoted with a base 8. It consists of the values: 0,1,2,3,4,5,6,7.
For example: (462)8, (27)8, (500)8, (3462)8, etc.
We will start off by taking the decimal number to be converted as user input. We will convert the decimal number into octal by dividing the decimal number by 8 until we get 0. In each division, the remainder becomes the most significant bit of the octal number accordingly.
Hence, by the end, the first remainder is the least significant bit(LSB) and the last remainder is the most significant bit(MSB).
In this algorithm, we are starting a loop which runs until the decimal number becomes 0. The remainder is extracted with the help of modulus operator. After that, the remainder is multiplied by the place (starting from ones) in the octal number.
The place where the next remainder is to be inserted is tracked by the variable i, which is incremented after each division.
Once the number becomes 0, the division is stopped and the equivalent octal number is displayed.
Let us take a look at an example for better understanding:
Given decimal number: (461)10 461/8=57 and remainder = 5 57/8=7 and remainder = 1 7/8=0 and remainder = 7 So the octal number starts from MSD to LSD, i.e. 715 Therefore, (461)10 = (715)8