Write a program for Octal to decimal conversion

17 May 2022

Write a program for Octal to decimal 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

10

Output

8

 

C Program

#include<stdio.h>

#include<math.h>

int DOConvert(long long num)

{

    int i = 0, dec = 0;

    int base = 8;

    while (num!=0)

    {

        int digit = num % 10;

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

 

        num /= 10;

        i++;

    }

    return dec;

}

 

int main()

{

    long long oct;

    printf("Enter an octal number: ");

    scanf("%lld", &oct);

    printf("%lld", DOConvert(oct));

    return 0;

}

 

C++ Program

#include <iostream>

#include<math.h>

using namespace std;

int DOConvert(long long num)

{

    int i = 0, dec = 0;

    int base = 8;

    while (num!=0)

    {

        int digit = num % 10;

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

 

        num /= 10;

        i++;

    }

    return dec;

}

int main()

{

    long long oct;

    printf("Enter an octal number: ");

    scanf("%lld", &oct);

    printf("%lld", DOConvert(oct));

    return 0;

}

 

Java Program

import java.util.Scanner;

import java.io.*;

class Main {

                static int ODConvert(int n)

                {

                                int num = n;

                                int dec = 0;

                                int base = 1;

                                int temp = num;

                                while (temp > 0) {

                                                int rem = temp % 10;

                                                temp = temp / 10;

 

                                                dec += rem * base;

 

                                                base = base * 8;

                                }

                                return dec;

                }

 

                public static void main(String[] args)

                {

                                Scanner sc = new Scanner(System.in);   

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

                                int num = sc.nextInt();

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

                }

}

 

Python Program

def ODConvert(num):

    dec = 0

    base = 1

    while num:

        rem = num % 10

        num = int(num / 10)

        dec += rem * base

        base = base * 8

    return dec

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

print(ODConvert(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 !