{"id":19164,"date":"2021-08-29T15:13:53","date_gmt":"2021-08-29T09:43:53","guid":{"rendered":"https:\/\/python-programs.com\/?p=19164"},"modified":"2021-11-22T18:37:08","modified_gmt":"2021-11-22T13:07:08","slug":"python-program-to-convert-binary-to-octal-using-while-loop","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-convert-binary-to-octal-using-while-loop\/","title":{"rendered":"Python Program to Convert Binary to Octal using While Loop"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Convert Octal to Decimal using While Loop<\/a> Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to convert the given binary number into octal :<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Convert Octal to Decimal using While Loop Given a binary number and the task is to get the respective octal number using the while loop. Examples: Example1: Input: Given Binary Number = 1010 Output: The Octal value of the given binary number { 1010 } …<\/p>\n
\nGiven a binary number and the task is to get the respective octal number using the while loop.<\/p>\nGiven Binary Number = 1010<\/pre>\n
The Octal value of the given binary number { 1010 } is: \r\n12<\/pre>\n
Given Binary Number = 1101<\/pre>\n
The Octal value of the given binary number { 1101 } is: \r\n15<\/pre>\n
Program to Convert Binary to Octal using While Loop in Python<\/h2>\n
\n
Method #1: Using While Loop (Static Input)<\/h3>\n
\n
# Give the binary number as static input and store it in a variable.\r\nbinry_numb = 1010\r\n# Take a variable say octl_num and initialize its value with 0.\r\noctl_num = 0\r\n# Take another variable say deciml_num and initialize its value with 0.\r\ndeciml_num = 0\r\n# Again take another variable say 'a' and initialize its value with 0.\r\na = 0\r\n# Loop till the given binary number is not equal to zero using the while loop.\r\nprint(\"The Octal value of the given binary number {\", binry_numb, \"} is: \")\r\nwhile (binry_numb != 0):\r\n # Inside the loop, calculate the given binary number modulus 10 (to get the last digit).\r\n # store it in a variable 'b'.\r\n b = (binry_numb % 10)\r\n # Calculate the value of 2 raised to the power a using the pow() function and multiply\r\n# it with the above-obtained value 'b'.\r\n # Store it in a variable 'c'.\r\n c = pow(2, a)*b\r\n # Add 'c' to the above-initialized deciml_num and store it in the same variable deciml_num.\r\n deciml_num = deciml_num + c\r\n # Increment the value a by 1 and store it in the same variable 'a'.\r\n a += 1\r\n # Divide the given binary number by 10 and store it in the same variable binry_numb.\r\n binry_numb = binry_numb \/\/ 10\r\n # Come out of the while loop and initialize the value of 'a' with 1.\r\na = 1\r\n# Loop till the decimal number is not equal to zero using the while loop.\r\nwhile (deciml_num != 0):\r\n # Multiply\u00a0'a' with the deciml_num modulus 8 and store it in a variable 'd'.\r\n d = (deciml_num % 8) * a\r\n # Add the value of 'd' with the above-initialized octl_num and store it in the same\r\n # variable octl_num.\r\n octl_num = octl_num + d\r\n # Divide the deciml_num by 8 and store it in the same variable deciml_num.\r\n deciml_num = deciml_num \/\/ 8\r\n # Multiply 'a' with 10 and store it in the same variable 'a'.\r\n a = a * 10\r\n# Print the octl_num to get the octal value of the given binary number.\r\nprint(octl_num)\r\n<\/pre>\n
The Octal value of the given binary number { 1010 } is: \r\n12<\/pre>\n
Method #2: Using While loop (User Input)<\/h3>\n
\n
# Give the binary number as user input using the int(input()) function and store it\r\n# in a variable.\r\nbinry_numb = int(input(\"Enter some random Number = \"))\r\n# Take a variable say octl_num and initialize its value with 0.\r\noctl_num = 0\r\n# Take another variable say deciml_num and initialize its value with 0.\r\ndeciml_num = 0\r\n# Again take another variable say 'a' and initialize its value with 0.\r\na = 0\r\n# Loop till the given binary number is not equal to zero using the while loop.\r\nprint(\"The Octal value of the given binary number {\", binry_numb, \"} is: \")\r\nwhile (binry_numb != 0):\r\n # Inside the loop, calculate the given binary number modulus 10 (to get the last digit).\r\n # store it in a variable 'b'.\r\n b = (binry_numb % 10)\r\n # Calculate the value of 2 raised to the power a using the pow() function and multiply\r\n# it with the above-obtained value 'b'.\r\n # Store it in a variable 'c'.\r\n c = pow(2, a)*b\r\n # Add 'c' to the above-initialized deciml_num and store it in the same variable deciml_num.\r\n deciml_num = deciml_num + c\r\n # Increment the value a by 1 and store it in the same variable 'a'.\r\n a += 1\r\n # Divide the given binary number by 10 and store it in the same variable binry_numb.\r\n binry_numb = binry_numb \/\/ 10\r\n # Come out of the while loop and initialize the value of 'a' with 1.\r\na = 1\r\n# Loop till the decimal number is not equal to zero using the while loop.\r\nwhile (deciml_num != 0):\r\n # Multiply\u00a0'a' with the deciml_num modulus 8 and store it in a variable 'd'.\r\n d = (deciml_num % 8) * a\r\n # Add the value of 'd' with the above-initialized octl_num and store it in the same\r\n # variable octl_num.\r\n octl_num = octl_num + d\r\n # Divide the deciml_num by 8 and store it in the same variable deciml_num.\r\n deciml_num = deciml_num \/\/ 8\r\n # Multiply 'a' with 10 and store it in the same variable 'a'.\r\n a = a * 10\r\n# Print the octl_num to get the octal value of the given binary number.\r\nprint(octl_num)\r\n<\/pre>\n
Enter some random Number = 1101\r\nThe Octal value of the given binary number { 1101 } is: \r\n15<\/pre>\n
\n