Write a program for Octal to binary conversion

17 May 2022

Write a program for Octal to binary 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 an octal number as the input from the use then convert it into equivalent binary number.

Input

10

Output

1000

 

C Program

#include<stdio.h>

#include<math.h>

void OBConvert(int oct)

{

    int i = 0, dec = 0;

    while (oct!=0)

    {

        int digit = oct % 10;

        dec += digit * pow(8, i);

        oct /= 10;

        i++;

    }

    long long bin = 0;

    int rem;

    i = 1;

    while(dec!=0)

    {

        rem = dec % 2;

        dec /= 2;

        bin += rem * i;

        i *= 10;

    }

    printf("Binary Value: %lld",bin);

}

int main()

{

    int oct;

    printf("Enter an octal alue: ");

    scanf("%d", &oct);

    OBConvert(oct);

    return 0;

}

 

C++ Program

#include <iostream>

#include<math.h>

using namespace std;

int OBConvert(int oct)

{

    int i = 0, dec = 0;

    while (oct!=0)

    {

        int digit = oct % 10;

        dec += digit * pow(8, i);

        oct /= 10;

        i++;

    }

    long long bin = 0;

    int rem;

    i = 1;

    while(dec!=0)

    {

        rem = dec % 2;

        dec /= 2;

        bin += rem * i;

        i *= 10;

    }

    cout<<"Binary Value: "<<bin;

    return 0;

}

int main()

{

    int oct;

    cout<<"Enter an octal value: ";

    cin>>oct;

    OBConvert(oct);

 

    return 0;

}

 

Java Program

import java.util.Scanner;

public class Main

{

                public static void main(String args[])

                {     

                                Scanner sc = new Scanner(System.in);   

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

                                int oct = sc.nextInt();

                                int dec = 0;

                                int n = 0; 

                                while(oct > 0)

                                {

                                                int temp = oct % 10; 

                                                dec += temp * Math.pow(8, n); 

                                                oct = oct/10; 

                                                n++; 

                                }

                                int bin[] = new int[20];

                                int i = 0;

                                while(dec > 0)

                                {      

                                                int r = dec % 2;

                                                bin[i++] = r;

                                                dec = dec/2;

                                }

                               

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

                                System.out.print(bin[j]);

                }

 

Python Program

import math

def OBConvert(octal):

    dec = 0

    i = 0

    bin = 0

    while(octal != 0):

        dec += (octal%10) * math.pow(8,i)

        i = i + 1

        octal = int (octal / 10)

    i = 1

    while (dec != 0):

        bin = bin + (dec % 2) * i;

        dec = int(dec / 2);

        i *= 10;

    return bin

octal = int(input("Enter an octal number : "))

print(int(OBConvert(octal)))


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 !