Problem statement:- Program to Convert Decimal to Octal using recursion.
Data requirement:-
Input Data:- n
Output Data:-DecimalToOctal(n)
Output Data:-DecimalToOctal(n)
Program in C
Here is the source code of the C Program to Convert Decimal to Octal using recursion.
Code:
#include<stdio.h>
int DecimalToOctal(int n)
{
static int sem=1,octal=0;
if(n!=0)
{
octal=octal+(n%8)*sem;
sem=sem*10;
DecimalToOctal(n/8);
}
return octal;
}
int main()
{
int n;
printf("Enter the Decimal Value:");
scanf("%d",&n);
printf("Octal Value of Decimal number is: %d",DecimalToOctal(n));
}
Enter the Decimal Value:8
Octal Value of Decimal number is: 10
Program in C++
Here is the source code of the C++ Program to Convert Decimal to Octal using recursion.
Code:
#include<iostream>
using namespace std;
int DecimalToOctal(int n)
{
static int sem=1,octal=0;
if(n!=0)
{
octal=octal+(n%8)*sem;
sem=sem*10;
DecimalToOctal(n/8);
}
return octal;
}
int main()
{
int n;
cout<<"Enter the Decimal Value:";
cin>>n;
cout<<"Octal Value of Decimal number is: "<<DecimalToOctal(n);
}
Enter the Decimal Value:165
Octal Value of Decimal number is: 245
Program in Java
Here is the source code of the Java Program to Convert Decimal to Octal using recursion.
Code:
import java.util.Scanner;
public class ConvertDecimalToOctal {
int sem=1,octal=0;
int DecimalToOctal(int n)
{
if(n!=0)
{
octal=octal+(n%8)*sem;
sem=sem*10;
DecimalToOctal(n/8);
}
return octal;
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int n;
System.out.print("Enter the Decimal Value:");
n=cs.nextInt();
ConvertDecimalToOctal ob=new ConvertDecimalToOctal();
System.out.print("Octal Value of Decimal number is: "+ob.DecimalToOctal(n));
cs.close();
}
}
Enter the Decimal Value:10
Octal Value of Decimal number is: 12
Program in Python
Here is the source code of the Python program to convert Decimal to Octal using recursion.
Code:
sem=1
octal=0
def DecimalToOctal(n):
global sem,octal
if(n!=0):
octal = octal + (n % 8) * sem
sem = sem * 10
DecimalToOctal(n // 8)
return octal
n=int(input("Enter the Decimal Value:"))
print("Octal Value of Decimal number is: ",DecimalToOctal(n))
Enter the Decimal Value:45
Octal Value of Decimal number is: 55
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box