Convert Octal to decimal using recursion

 Problem statement:- Program to Convert Octal to a decimal using recursion.

Data requirement:-

   Input Data:- n

  Output Data:-
OctalToDecimal(n)

Program in C

Here is the source code of the C Program to Convert Octal to a decimal using recursion.

Code:

#include<stdio.h>
#include<math.h>
int OctalToDecimal(int n)
{
    static int decimal = 0, sem = 0;
    if(n!=0)
    {
    decimal=decimal+(n%10)*pow(8,sem);
    sem++;
    OctalToDecimal(n/10);
    }
   return decimal;
}
int main()
{
    int n;
    printf("Enter the Octal Value:");
    scanf("%d",&n);
    printf("Decimal Value of Octal number is: %d",OctalToDecimal(n));
}

Input/Output:
Enter the Octal Value:45
Decimal Value of Octal number is: 37

Program in C++

Here is the source code of the C++ Program to Convert Octal to decimal using recursion.

Code:

#include<iostream>
#include<cmath>
using namespace std;
int OctalToDecimal(int n)
{
    static int decimal = 0, sem = 0;
    if(n!=0)
    {
    decimal=decimal+(n%10)*pow(8,sem);
    sem++;
    OctalToDecimal(n/10);
    }
   return decimal;
}
int main()
{
    int n;
    cout<<"Enter the Octal Value:";
    cin>>n;
    cout<<"Decimal Value of Octal number is: "<<OctalToDecimal(n);
}

Input/Output:
Enter the Octal Value:165
Decimal Value of Octal number is: 117

Program in Java

Here is the source code of the Java Program to Convert Octal to a decimal using recursion.

Code:

import java.util.Scanner;
public class ConvertOctalToDecimal {
int decimal = 0, sem = 0;
int OctalToDecimal(int n)
{
    if(n!=0)
    {
    decimal=decimal+(int)((n%10)*Math.pow(8,sem));
    sem++;
    OctalToDecimal(n/10);
    }
   return decimal;
}
public static void main(String[] args) {
           Scanner cs=new Scanner(System.in);
            int n;
    System.out.print("Enter the Octal Value:");
    n=cs.nextInt();
    ConvertOctalToDecimal ob=new  ConvertOctalToDecimal();
    System.out.print("Decimal Value of Octal number is: "+ob.OctalToDecimal(n));
        cs.close();
}
}

Input/Output:
Enter the Octal Value:30
Decimal Value of Octal number is: 24

Program in Python

Here is the source code of the Python program to convert Octal to decimal using recursion.

Code:

decimal=0
sem=0
def OctalToDecimal(n):
    global sem,decimal
    if(n!=0):
        decimal+=(n%10)*pow(8,sem)
        sem+=1
        OctalToDecimal(n // 10)
    return decimal
n=int(input("Enter the Octal Value:"))
print("Decimal Value of Octal number is:",OctalToDecimal(n))

Input/Output:

Post a Comment

0 Comments