Programa para conversión de decimal a octal

Dado un número decimal como entrada, necesitamos escribir un programa para convertir el número decimal dado en un número octal equivalente. es decir, convertir el número con el valor base 10 al valor base 8. El valor base de un sistema numérico determina el número de dígitos utilizados para representar un valor numérico. Por ejemplo, el sistema numérico binario usa dos dígitos 0 y 1, el sistema numérico octal usa 8 dígitos del 0 al 7 y el sistema numérico decimal usa 10 dígitos del 0 al 9 para representar cualquier valor numérico.

Ejemplos: 

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>

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>

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;
}

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

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *