给定一个十进制数作为输入,我们需要编写一个程序,将给定的十进制数转换成等效的八进制数。i、 e将基值为10的数字转换为基值8。数字系统的基值决定用于表示数值的位数。例如,二进制系统使用两位数字0和1,八进制系统使用0-7之间的8位数字,十进制系统使用10位数字0-9来表示任何数值。
null
例如:
Input : 16Output: 20Input : 10Output: 12Input : 33Output: 41
算法 :
- 在数组中,当数字除以8时,存储余数。
- 现在把这个数字除以8
- 重复上述两个步骤,直到数字不等于0。
- 现在按相反顺序打印阵列。
例如:
如果给定的十进制数是16。
第一步 :16除以8的余数为0。因此,arr[0]=0。 第二步 :16除以8。新的数字是16/8=2。 第三步 :余数,当2除以8时,为2。因此,arr[1]=2。 第四步 :2除以8。新的数字是2/8=0。 第五步 :因为数字变为=0。
停止重复步骤,按相反顺序打印阵列。因此,等效的八进制数是20。
下图显示了将十进制数33转换为等效八进制数的示例。
下面是上述想法的实施。
C++
// C++ program to convert a decimal // number to octal number #include <iostream> using namespace std; // function to convert decimal to octal void decToOctal( int n) { // array to store octal number int octalNum[100]; // counter for octal number array int i = 0; while (n != 0) { // storing remainder in octal array octalNum[i] = n % 8; n = n / 8; i++; } // printing octal number array in reverse order for ( int j = i - 1; j >= 0; j--) cout << octalNum[j]; } // Driver Code int main() { int n = 33; // Function Call decToOctal(n); return 0; } |
JAVA
// Java program to convert a decimal // number to octal number import java.io.*; class GFG { // Function to convert decimal to octal static void decToOctal( int n) { // array to store octal number int [] octalNum = new int [ 100 ]; // counter for octal number array int i = 0 ; while (n != 0 ) { // storing remainder in octal array octalNum[i] = n % 8 ; n = n / 8 ; i++; } // Printing octal number array in reverse order for ( int j = i - 1 ; j >= 0 ; j--) System.out.print(octalNum[j]); } // Driver Code public static void main(String[] args) { int n = 33 ; // Function Call decToOctal(n); } } // Contributed by Pramod Kumar |
Python3
# Python3 program to convert # a decimal number to # octal number # function to convert # decimal to octal def decToOctal(n): # array to store # octal number octalNum = [ 0 ] * 100 # counter for octal # number array i = 0 while (n ! = 0 ): # storing remainder # in octal array octalNum[i] = n % 8 n = int (n / 8 ) i + = 1 # printing octal number # array in reverse order for j in range (i - 1 , - 1 , - 1 ): print (octalNum[j], end = "") # Driver Code n = 33 # Function Call decToOctal(n) # This code is contributed # by mits |
C#
// C# program to convert a decimal // number to octal number using System; class GFG { // Function to convert decimal to octal static void decToOctal( int n) { // array to store octal number int [] octalNum = new int [100]; // counter for octal number array int i = 0; while (n != 0) { // storing remainder in octal array octalNum[i] = n % 8; n = n / 8; i++; } // Printing octal number array in // reverse order for ( int j = i - 1; j >= 0; j--) Console.Write(octalNum[j]); } // Driver Code public static void Main() { int n = 33; // Function Call decToOctal(n); } } // This code is contributed by nitin mittal. |
PHP
<?php // PHP program to convert // a decimal number to // octal number // function to convert // decimal to octal function decToOctal( $n ) { // array to store // octal number $octalNum ; // counter for octal // number array $i = 0; while ( $n != 0) { // storing remainder // in octal array $octalNum [ $i ] = $n % 8; $n = (int)( $n / 8); $i ++; } // printing octal number // array in reverse order for ( $j = $i - 1; $j >= 0; $j --) echo $octalNum [ $j ]; } // Driver Code $n = 33; // Function Call decToOctal( $n ); // This code is contributed // by ajit ?> |
Javascript
<script> // JavaScript program to convert a decimal // number to octal number // function to convert decimal to octal function decToOctal(n) { // array to store octal number let octalNum = new Array(100); // counter for octal number array let i = 0; while (n != 0) { // storing remainder in octal array octalNum[i] = n % 8; n = Math.floor(n / 8); i++; } // printing octal number array in reverse order for (let j = i - 1; j >= 0; j--) document.write(octalNum[j]); } // Driver Code let n = 33; // Function Call decToOctal(n); // This code is contributed by Surbhi Tyagi </script> |
输出
41
时间复杂性: O(对数N)
另一种方法:(O(1)空间复杂性)
此问题也可以通过以下算法在不使用阵列的情况下解决:
- 将八进制数初始化为0,countVal初始化为1,十进制数初始化为n
- 当十进制数除以8时求余数
- 用八进制+更新八进制数(余数*countval)
- 将countval增加countval*10
- 将十进制数除以8
- 重复第二步,直到小数为零
以下是上述理念的实施情况:
C++
// C++ program to convert decimal // number to octal number #include <iostream> using namespace std; // function to calculate the octal value of the given // decimal number void decimaltoOctal( int deciNum) { // initializations int octalNum = 0, countval = 1; int dNo = deciNum; while (deciNum != 0) { // decimals remainder is calculated int remainder = deciNum % 8; // storing the octalvalue octalNum += remainder * countval; // storing exponential value countval = countval * 10; deciNum /= 8; } cout << octalNum << endl; } // Driver Code int main() { int n = 33; // Function Call decimaltoOctal(n); return 0; } |
C
// C program to convert decimal // number to octal number #include <stdio.h> // function to calculate the octal value of the given // decimal number void decimaltoOctal( int deciNum) { int octalNum = 0, countval = 1; int dNo = deciNum; while (deciNum != 0) { // decimals remainder is calculated int remainder = deciNum % 8; // storing the octalvalue octalNum += remainder * countval; // storing exponential value countval = countval * 10; deciNum /= 8; } printf ( "%d" , octalNum); } // Driver Code int main() { int n = 33; // Function Call decimaltoOctal(n); return 0; } |
JAVA
// JAVA program to convert decimal // number to octal number import java.io.*; class GFG { // function to calculate the octal value of the given // decimal number static void octaltodecimal( int deciNum) { int octalNum = 0 , countval = 1 ; int dNo = deciNum; while (deciNum != 0 ) { // decimals remainder is calculated int remainder = deciNum % 8 ; // storing the octalvalue octalNum += remainder * countval; // storing exponential value countval = countval * 10 ; deciNum /= 8 ; } System.out.println(octalNum); } // Driver Code public static void main(String[] args) { int n = 33 ; // Function Call octaltodecimal(n); } } |
Python3
# Python3 program to convert decimal # number to octal number # function to calculate the octal value of the given # decimal number def decimaltoOctal(deciNum): # initializations octalNum = 0 countval = 1 dNo = deciNum while (deciNum ! = 0 ): # decimals remainder is calculated remainder = deciNum % 8 # storing the octalvalue octalNum + = remainder * countval # storing exponential value countval = countval * 10 deciNum / / = 8 print (octalNum) # Driver Code if __name__ = = '__main__' : n = 33 # Function Call decimaltoOctal(n) # This code is contributed by pratham76 |
C#
// C# program to convert decimal // number to octal number using System; class GFG { // function to calculate // the octal value of the given // decimal number static void octaltodecimal( int deciNum) { int octalNum = 0, countval = 1; while (deciNum != 0) { // decimals remainder is // calculated int remainder = deciNum % 8; // storing the octalvalue octalNum += remainder * countval; // storing exponential value countval = countval * 10; deciNum /= 8; } Console.Write(octalNum); } // Driver Code public static void Main( string [] args) { int n = 33; // Function Call octaltodecimal(n); } } // This code is contributed by rutvik_56 |
Javascript
<script> // Javascript program to convert decimal // number to octal number // function to calculate the octal value of the given // decimal number function decimaltoOctal(deciNum) { // initializations let octalNum = 0, countval = 1; let dNo = deciNum; while (deciNum != 0) { // decimals remainder is calculated let remainder = Math.floor(deciNum % 8); // storing the octalvalue octalNum += remainder * countval; // storing exponential value countval = countval * 10; deciNum = Math.floor(deciNum/8); } document.write(octalNum + "<br>" ); } // Driver Code let n = 33; // Function Call decimaltoOctal(n); //This code is contributed by Mayank Tyagi </script> |
输出
41
时间复杂性: O(对数N)
辅助空间: O(1)
使用预定义的函数
JAVA
// JAVA program to convert decimal // number to octal number import java.io.*; class GFG { public static void decToOct( int n) { System.out.println(Integer.toOctalString(n)); } public static void main(String[] args) { int n = 33 ; decToOct(n); } } |
Python3
# Python program to convert decimal # number to octal number def decToOct(n): print ( oct (n)); if __name__ = = '__main__' : n = 33 ; decToOct(n); # This code is contributed by Amit Katiyar |
C#
// C# program to convert decimal // number to octal number using System; public class GFG { public static void decToOct( int n) { Console.WriteLine(Convert.ToString(n, 8)); } public static void Main(String[] args) { int n = 33; decToOct(n); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // Javascript program to convert decimal // number to octal number function decToOct(n) { document.write(n.toString(8)); } var n = 33; decToOct(n); // This code contributed by Princi Singh </script> |
C++
#include <bits/stdc++.h> using namespace std; string intToOctal( int n) { stringstream st; st << oct << n; return st.str(); } int main() { int n = 43; cout << intToOctal(n); return 0; } |
输出
41
本文由Saurabh Sharma撰稿。如果你喜欢GeekSforgeks并想贡献自己的力量,你也可以用write写一篇文章。极客。组织或邮寄你的文章进行评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END