Program to convert float decimal to Octal number
Last Updated :
11 Jan, 2024
Write a program that converts a floating-point decimal number into its octal format. The program should take a number and the precision for the octal fractional part as inputs from the user. It should calculate and provide its correct octal representation as output.
Examples:
Input: Number = 123.45, No. of decimal places (Precision): 5
Output: 173.34631
Input: 7.1234, No. of decimal places (Precision): 7
Output: 7.0771344
Approach: To solve the problem follow the below idea:
To convert a float decimal number into an octal number, first you should focus on separating the integer and decimal parts. Then, convert the integer part by repeatedly dividing it by 8. Now, start converting the decimal part by multiplying it by 8 and extracting the digits. Finally, combine both the calculations to get your correct octal representation.
Let us dry-run a simple example to understand this conversion in a better way:
Consider a float number: 123.45
- At first, separate the integer and decimal parts.
- For the integer part:
- The integer part of 123 in base 10 is directly converted to base 8 for octal
- 123 (base 10) = 173 (base 8)
- For the fractional part:
- First iteration: 0.45*8 = 3.6. Hence, the first octal digit is ‘3’.
- Second iteration: 0.6* 8 = 4.8. The second digit is ‘4’.
- Combine these two calculations:
- 123.45 (base 10) = 173.34 (base 8) …(approximate value).
Steps to solve the problem:
- Initialize two input variables: number and its precision value to be considered for output.
- As discussed in the approach above, convert the integer part first.
- Create an empty string ‘str’. Loop until the integer part is greater than 0.
- Calculate the remainder and convert it to string. Then, add it to the beginning of ‘str‘.
- Now, for the decimal part, create an empty string with a “.” at the start.
- Multiply the fractional part by 8, extract the integer part and append it to the string.
- Update the ‘num’ to be the remaining fractional part.
- At the end, concatenate both the strings ‘s1’ and ‘s2’ to obtain the answer.
Below is the Code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string convertInteger( int num)
{
string str = "" ;
while (num > 0) {
int rem = num % 8;
str = to_string(rem) + str;
num /= 8;
}
return str;
}
string convertFractional( double num, int p)
{
string str = "." ;
while (p > 0) {
num *= 8;
int integer = ( int )(num);
str += to_string(integer);
num -= integer;
p--;
}
return str;
}
string decimalToOctal( double num, int p)
{
int integer = ( int )(num);
double dec = num - integer;
string s1 = convertInteger(integer);
string s2 = convertFractional(dec, p);
return s1 + s2;
}
int main()
{
double num = 123.45;
int p = 2;
string ans = decimalToOctal(num, p);
cout << "Octal Number: " << ans << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
static String convertInteger( int num)
{
StringBuilder str = new StringBuilder();
while (num > 0 ) {
int rem = num % 8 ;
str.insert( 0 , rem);
num /= 8 ;
}
return str.toString();
}
static String convertFractional( double num, int p)
{
StringBuilder str = new StringBuilder( "." );
while (p > 0 ) {
num *= 8 ;
int integer = ( int )num;
str.append(integer);
num -= integer;
p--;
}
return str.toString();
}
static String decimalToOctal( double num, int p)
{
int integer = ( int )num;
double dec = num - integer;
String s1 = convertInteger(integer);
String s2 = convertFractional(dec, p);
return s1 + s2;
}
public static void main(String[] args)
{
double num = 123.45 ;
int p = 2 ;
String ans = decimalToOctal(num, p);
System.out.println( "Octal Number: " + ans);
}
}
|
Python3
def convert_integer(num):
str_val = ""
while num > 0 :
rem = num % 8
str_val = str (rem) + str_val
num / / = 8
return str_val
def convert_fractional(num, p):
str_val = "."
while p > 0 :
num * = 8
integer = int (num)
str_val + = str (integer)
num - = integer
p - = 1
return str_val
def decimal_to_octal(num, p):
integer = int (num)
dec = num - integer
s1 = convert_integer(integer)
s2 = convert_fractional(dec, p)
return s1 + s2
if __name__ = = "__main__" :
num = 123.45
p = 2
ans = decimal_to_octal(num, p)
print ( "Octal Number:" , ans)
|
C#
using System;
class Program {
static string ConvertInteger( int num)
{
string str = "" ;
while (num > 0) {
int rem = num % 8;
str = rem + str;
num /= 8;
}
return str;
}
static string ConvertFractional( double num, int p)
{
string str = "." ;
while (p > 0) {
num *= 8;
int integer = ( int )(num);
str += integer;
num -= integer;
p--;
}
return str;
}
static string DecimalToOctal( double num, int p)
{
int integer = ( int )(num);
double dec = num - integer;
string s1 = ConvertInteger(integer);
string s2 = ConvertFractional(dec, p);
return s1 + s2;
}
static void Main()
{
double num = 123.45;
int p = 2;
string ans = DecimalToOctal(num, p);
Console.WriteLine( "Octal Number: " + ans);
}
}
|
Javascript
function convertInteger(num) {
let str = '' ;
while (num > 0) {
const rem = num % 8;
str = rem.toString() + str;
num = Math.floor(num / 8);
}
return str;
}
function convertFractional(num, p) {
let str = '.' ;
while (p > 0) {
num *= 8;
const integer = Math.floor(num);
str += integer.toString();
num -= integer;
p--;
}
return str;
}
function decimalToOctal(num, p) {
const integer = Math.floor(num);
const dec = num - integer;
const s1 = convertInteger(integer);
const s2 = convertFractional(dec, p);
return s1 + s2;
}
const num = 123.45;
const p = 2;
const ans = decimalToOctal(num, p);
console.log( "Octal Number: " + ans);
|
Output
Octal Number: 173.34
Time Complexity: O(N), there will n iterations depending upon the number of digits in the float decimal number.
Auxiliary Space: O(N), We use an extra space for strings here, hence we require O(N) space.
Share your thoughts in the comments
Please Login to comment...