{"id":19162,"date":"2021-08-29T15:13:51","date_gmt":"2021-08-29T09:43:51","guid":{"rendered":"https:\/\/python-programs.com\/?p=19162"},"modified":"2021-11-22T18:37:08","modified_gmt":"2021-11-22T13:07:08","slug":"python-program-to-convert-octal-to-decimal-using-while-loop","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-convert-octal-to-decimal-using-while-loop\/","title":{"rendered":"Python Program to Convert Octal to Decimal using While Loop"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Convert Binary 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 octal number into decimal :<\/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 Binary to Decimal using While Loop Given an octal number and the task is to get the respective decimal number using the while loop in Python. Examples: Example1: Input: Given Octal Number = 15 Output: The Decimal value of the given Octal number { …<\/p>\n
\nGiven an octal number and the task is to get the respective decimal number using the while loop in Python.<\/p>\nGiven Octal Number = 15<\/pre>\n
The Decimal value of the given Octal number { 15 } is: \r\n13<\/pre>\n
Given Octal Number = 32<\/pre>\n
The Decimal value of the given Octal number { 32 } is: \r\n26<\/pre>\n
Program to Convert Octal to Decimal in Python<\/h2>\n
\n
Method #1: Using While Loop (Static Input)<\/h3>\n
\n
# Give the octal number as static input and store it in a variable.\r\ngvn_octl_numb = 15\r\n# Take a variable say 'a 'and initialize its value with 0.\r\na = 0\r\n# Take another variable say deciml_numb and initialize its value with 0.\r\ndeciml_numb = 0\r\n# Loop till the given octal number is not equal to zero using the while loop.\r\nprint(\"The Decimal value of the given Octal number {\", gvn_octl_numb, \"} is: \")\r\nwhile(gvn_octl_numb != 0):\r\n # Inside the loop, calculate the value of the given octal number modulus 10\r\n # (to get the last digit) and store it in a variable say 'b'.\r\n b = (gvn_octl_numb % 10)\r\n# Calculate the value of 8 raised to the power 'a' using the pow() function and multiply\r\n# it with the above-obtained 'b'.\r\n# Store it in another variable 'c'.\r\n c = b*pow(8, a)\r\n # Add the above variable 'c' with the\u00a0deciml_numb and store it in the same variable\r\n # deciml_numb.\r\n deciml_numb = deciml_numb+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 octal number by 10 (to remove the last digit) and store it in the\r\n# same variable gvn_octl_numb.\r\n gvn_octl_numb = gvn_octl_numb \/\/ 10\r\n# Print the deciml_num to get the decimal value of the given Octal number.\r\nprint(deciml_numb)\r\n<\/pre>\n
The Decimal value of the given Octal number { 15 } is: \r\n13<\/pre>\n
Method #2: Using While loop (User Input)<\/h3>\n
\n
# Give the octal number as user input using the int(input()) function and\r\n# store it in a variable.\r\ngvn_octl_numb = int(input(\"Enter some Random Number = \"))\r\n# Take a variable say 'a 'and initialize its value with 0.\r\na = 0\r\n# Take another variable say deciml_numb and initialize its value with 0.\r\ndeciml_numb = 0\r\n# Loop till the given octal number is not equal to zero using the while loop.\r\nprint(\"The Decimal value of the given Octal number {\", gvn_octl_numb, \"} is: \")\r\nwhile(gvn_octl_numb != 0):\r\n # Inside the loop, calculate the value of the given octal number modulus 10\r\n # (to get the last digit) and store it in a variable say 'b'.\r\n b = (gvn_octl_numb % 10)\r\n# Calculate the value of 8 raised to the power 'a' using the pow() function and multiply\r\n# it with the above-obtained 'b'.\r\n# Store it in another variable 'c'.\r\n c = b*pow(8, a)\r\n # Add the above variable 'c' with the\u00a0deciml_numb and store it in the same variable\r\n # deciml_numb.\r\n deciml_numb = deciml_numb+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 octal number by 10 (to remove the last digit) and store it in the\r\n# same variable gvn_octl_numb.\r\n gvn_octl_numb = gvn_octl_numb \/\/ 10\r\n# Print the deciml_num to get the decimal value of the given Octal number.\r\nprint(deciml_numb)\r\n<\/pre>\n
Enter some Random Number = 32\r\nThe Decimal value of the given Octal number { 32 } is: \r\n26<\/pre>\n
\n