Write a program for Decimal to octal conversion

17 May 2022

Write a program for Decimal to octal conversion


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get a octal number from user and find the corresponding decimal number.

Input

8

Output

10

 

C Program

#include<stdio.h>

void DOconvert(int num)

{

    int octArr[32];

    int i = 0;

    while (num > 0) {

        octArr[i] = num % 8;

        num = num / 8;

        i++;

    }

    for (int j = i - 1; j >= 0; j--)

        printf("%d",octArr[j]);

}

int main()

{

    int n;

    printf("Enter a decimal number: ");

    scanf("%d",&n);

    DOconvert(n);

    return 0;

}

 

C++ Program

#include <iostream>

using namespace std;

int DOconvert(int num)

{

    int octArr[32];

    int i = 0;

    while (num > 0) {

        octArr[i] = num % 8;

        num = num / 8;

        i++;

    }

    for (int j = i - 1; j >= 0; j--)

        cout<<octArr[j];

    return 0;

}

int main()

{

    int n;

    cout<<"Enter a decimal number: ";

    cin>>n;

    DOconvert(n);

    return 0;

}

 

Java Program

import java.util.Scanner;

import java.io.*;

class Main {

                public static String ODConvert(int dec){   

    int rem; 

    String oct=""; 

    char octalchars[]={'0','1','2','3','4','5','6','7'}; 

    while(dec>0) 

    { 

       rem=dec%8;  

       oct=octalchars[rem]+oct;  

       dec=dec/8; 

    } 

    return oct; 

}

                public static void main(String[] args)

                {

                                Scanner sc = new Scanner(System.in);   

                                System.out.print("Enter an decimal number: ");

                                int num = sc.nextInt();

                                System.out.println(ODConvert(num));

                }

}

 

Python Program

dec = int(input("Enter a decimal number: "))

Oct = oct(dec)

print('Number after conversion is :' + str(Oct))


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !