Dado un número octal como entrada, necesitamos escribir un programa para convertir el número octal dado en un número decimal equivalente.
Ejemplos:
C++
// C++ program to convert octal to decimal #include <iostream> using namespace std; // Function to convert octal to decimal int octalToDecimal(int n) { int num = n; int dec_value = 0; // Initializing base value to 1, i.e 8^0 int base = 1; int temp = num; while (temp) { // Extracting last digit int last_digit = temp % 10; temp = temp / 10; // Multiplying last digit with appropriate // base value and adding it to dec_value dec_value += last_digit * base; base = base * 8; } return dec_value; } // Driver program to test above function int main() { int num = 67; cout << octalToDecimal(num) << endl; }
Java
// Java program to convert octal to decimal import java.io.*; class GFG { // Function to convert octal to decimal static int octalToDecimal(int n) { int num = n; int dec_value = 0; // Initializing base value to 1, i.e 8^0 int base = 1; int temp = num; while (temp > 0) { // Extracting last digit int last_digit = temp % 10; temp = temp / 10; // Multiplying last digit with appropriate // base value and adding it to dec_value dec_value += last_digit * base; base = base * 8; } return dec_value; } // driver program public static void main(String[] args) { int num = 67; System.out.println(octalToDecimal(num)); } } // This code is contributed // by Pramod Kumar
Python3
# Python3 program to convert # octal to decimal # Function to convert # octal to decimal def octalToDecimal(n): num = n dec_value = 0 # Initializing base value # to 1, i.e 8^0 base = 1 temp = num while (temp): # Extracting last digit last_digit = temp % 10 temp = int(temp / 10) # Multiplying last digit # with appropriate base # value and adding it # to dec_value dec_value += last_digit * base base = base * 8 return dec_value # Driver Code num = 67 print(octalToDecimal(num)) # This code is contributed by mits
C#
// C# program to convert octal to // decimal using System; class GFG { // Function to convert octal // to decimal static int octalToDecimal(int n) { int num = n; int dec_value = 0; // Initializing base value // to 1, i.e 8^0 int b_ase = 1; int temp = num; while (temp > 0) { // Extracting last digit int last_digit = temp % 10; temp = temp / 10; // Multiplying last digit // with appropriate base // value and adding it to // dec_value dec_value += last_digit * b_ase; b_ase = b_ase * 8; } return dec_value; } // driver program public static void Main() { int num = 67; Console.WriteLine(octalToDecimal(num)); } } // This code is contributed by vt_m.
PHP
<?php // PHP program to convert octal to decimal // Function to convert // octal to decimal function octalToDecimal($n) { $num = $n; $dec_value = 0; // Initializing base value // to 1, i.e 8^0 $base = 1; $temp = $num; while ($temp) { // Extracting last digit $last_digit = $temp % 10; $temp = $temp / 10; // Multiplying last digit // with appropriate base // value and adding it // to dec_value $dec_value += $last_digit * $base; $base = $base * 8; } return $dec_value; } // Driver Code $num = 67; echo octalToDecimal($num); // This code is contributed by anuj_67 ?>
Javascript
<script> // JavaScript program to convert octal to decimal // Function to convert octal to decimal function octalToDecimal(n) { let num = n; let dec_value = 0; // Initializing base value to 1, i.e 8^0 let base = 1; let temp = num; while (temp) { // Extracting last digit let last_digit = temp % 10; temp = Math.floor(temp / 10); // Multiplying last digit with appropriate // base value and adding it to dec_value dec_value += last_digit * base; base = base * 8; } return dec_value; } // Driver program to test above function let num = 67; document.write(octalToDecimal(num) + "<br>"); // This code is contributed by Surbhi Tyagi </script>
C++
// C++ program to convert octal to decimal #include <iostream> using namespace std; int OctToDec(string n) { return stoi(n, 0, 8); } int main() { string n = "67"; cout << OctToDec(n); return 0; } // This code is contributed by phasing17
Java
// Java program to convert octal to decimal import java.io.*; class GFG { public static int OctToDec(String n) { return Integer.parseInt(n, 8); } public static void main(String[] args) { String n = "67"; System.out.println(OctToDec(n)); } }
Python3
# Python program to convert octal to decimal def OctToDec(n): return int(n, 8); if __name__ == '__main__': n = "67"; print(OctToDec(n)); # This code is contributed by 29AjayKumar
C#
using System; public class GFG{ public static int OctToDec(String n) { return Convert.ToInt32(n, 8); } static public void Main (){ string n = "67"; Console.WriteLine(OctToDec(n)); } } // THIS CODE IS CONTRIBUTED BY RAG2127
Javascript
<script> // javascript program to convert octal to decimal function OctToDec(n) { return parseInt(n, 8); } var n = "67"; document.write(OctToDec(n)); // This code contributed by Princi Singh </script>
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA