Program to Convert Octal to Decimal

Program to Convert Octal to Decimal

  • Write a program to Convert Octal to Decimal in C
  • Write a program to Convert Octal to Decimal in C++
  • Write a program to Convert Octal to Decimal in Python
  • Write a program to Convert Octal to Decimal in PHP
  • Write a program to Convert Octal to Decimal in Java
  • Write a program to Convert Octal to Decimal in Java Script
  • Write a program to Convert Octal to Decimal in C#

Explanation:

The positional value system is used to translate an octal number (base 8) to its decimal equivalent (base 10). An octal number’s digits each correspond to powers of 8, with the rightmost digit being 80, the second from the right being 81, and so forth.

Steps for Octal to Decimal Conversion:

  1. Start with the octal number.
    • For example: 3458.
  2. Multiply each octal digit by 8n, where nnn is the position of the digit from the right (starting from 0).
  3. Sum the results.

Example Conversion:

Convert 3458 to decimal:

  1. Write the octal digits with their positions (starting from the rightmost):3×82 + 4×81 + 5×80
  2. Calculate each term:(3×64) + (4×8) + (5×1) = 192 + 32 + 5 = 229

Decimal equivalent: 229.

Program to Convert Octal to Decimal

#include <stdio.h>
#include <math.h>

int octalToDecimal(int octal) {
    int decimal = 0, base = 1;
    while (octal > 0) {
        int remainder = octal % 10;
        decimal += remainder * base;
        base *= 8;
        octal /= 10;
    }
    return decimal;
}

int main() {
    int octal;
    printf("Enter an octal number: ");
    scanf("%d", &octal);
    printf("Decimal: %d\n", octalToDecimal(octal));
    return 0;
}

#include <iostream>
#include <cmath>
using namespace std;

int octalToDecimal(int octal) {
    int decimal = 0, base = 1;
    while (octal > 0) {
        int remainder = octal % 10;
        decimal += remainder * base;
        base *= 8;
        octal /= 10;
    }
    return decimal;
}

int main() {
    int octal;
    cout << "Enter an octal number: ";
    cin >> octal;
    cout << "Decimal: " << octalToDecimal(octal) << endl;
    return 0;
}

def octal_to_decimal(octal):
    return int(str(octal), 8)

octal = input("Enter an octal number: ")
print("Decimal:", octal_to_decimal(octal))

<?php
function octalToDecimal($octal) {
    return octdec($octal);
}

$octal = readline("Enter an octal number: ");
echo "Decimal: " . octalToDecimal($octal) . "\n";
?>

import java.util.Scanner;

public class OctalToDecimal {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter an octal number: ");
        String octal = sc.nextLine();
        int decimal = Integer.parseInt(octal, 8);
        System.out.println("Decimal: " + decimal);
    }
}

function octalToDecimal(octal) {
    return parseInt(octal, 8);
}

const octal = prompt("Enter an octal number: ");
console.log("Decimal:", octalToDecimal(octal));

using System;

class OctalToDecimal {
    static void Main() {
        Console.Write("Enter an octal number: ");
        string octal = Console.ReadLine();
        int decimalValue = Convert.ToInt32(octal, 8);
        Console.WriteLine("Decimal: " + decimalValue);
    }
}

List of All Programs