Algorithm to perform Decimal to Octal Conversion

[582 views]


What is Decimal Number System?

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.

What is Octal Number System?

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.

Algorithm to perform Decimal to Octal Conversion:

Step 1: Start Step 2: Read the decimal number from the user, say ā€˜dā€™ Step 3: Initialize the octal number, octal=0 Step 4: Initialize i=1 Step 5: Repeat while d != 0: Step 5.1: Extract the remainder by: remainder = d % 8 Step 5.2: octal = octal+ (remainder * i) Step 5.3: d = d/8 Step 5.4: i = i * 10 Step 6: Display the octal number Step 7: Stop

Explanation:

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

Flowchart to perform Decimal to Octal Conversion:

Algorithm to perform Decimal to Octal conversion
        

Don't Understand Algorithm and Flowchart? Learn quickly using our Expert's Notes



Want to get in-depth understanding of Algorithms?


Are You Good enough in Algorithms? Test it now?


Comments



Search
Recommended Deals End in










Java Interview Questions For Beginner Notes
Quiz for Learners
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags