Smallest number to multiply to convert floating point to natural

Given a positive floating point number n, the task is to find the smallest integer k, such that when we multiply k with n, we get a natural number.

Examples:

Input : 30.25
Output : 4
30.25 * 4 = 321, there is no number less than 4
which can convert 30.25 into natural number.

Input : 5.5
Output : 2
5.5 * 2 = 11, there is no number less than 2
which can convert 5.5 into natural number.

Input : 5.33
Output : 100



The idea is to convert given floating point number into a fraction (not necessarily in reduced form) and find the GCD of numerator and denominator. For example, if input floating point number is 30.25, we convert into fraction as 3025/100. This can be easily done by finding the position of dot.
Finally to get the answer, we divide the denominator of the converted fraction by GCD of denominator and numerator. For example, GCD of 3025 and 100 is 25. We divide 100 by 25 and get the answer as 4.

Below is implementation of this approach:

C/C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the smallest number to multiply
// to convert a floating point number into natural number.
#include<bits/stdc++.h>
using namespace std;
  
// Finding GCD of two number
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a%b);
}
  
// Returns smallest integer k such that k * str becomes
// natural. str is an input floating point number
int findnum(string &str)
{
    // Find size of string representing a
    // floating point number.
    int n = str.length();
  
    // Below is used to find denominator in
    // fraction form.
    int count_after_dot = 0;
  
    // Used to find value of count_after_dot
    bool dot_seen = false;
  
    // To find numerator in fraction form of
    // given number. For example, for 30.25,
    // numerator would be 3025.
    int num = 0;
    for (int i = 0; i < n; i++)
    {
        if (str[i] != '.')
        {
            num = num*10 + (str[i] - '0');
            if (dot_seen == true)
                count_after_dot++;
        }
        else
            dot_seen = true;
    }
  
    // If there was no dot, then number
    // is already a natural.
    if (dot_seen == false)
       return 1;
  
    // Find denominator in fraction form. For example,
    // for 30.25, denominator is 100
    int dem = (int)pow(10, count_after_dot);
  
    // Result is denominator divided by
    // GCD-of-numerator-and-denominator. For example, for
    // 30.25, result is 100 / GCD(3025,100) = 100/25 = 4
    return (dem / gcd(num, dem));
}
  
// Driven Program
int main()
{
    string str = "5.125";
    cout << findnum(str) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the smallest number to multiply
// to convert a floating point number into natural number.
class GFG {
  // Finding GCD of two number
  static int gcd(int a, int b) {
    if (b == 0)
      return a;
    return gcd(b, a % b);
  }
  
  // Returns smallest integer k such that k * str becomes
  // natural. str is an input floating point number
  static int findnum(String str) {
    // Find size of string representing a
    // floating point number.
    int n = str.length();
  
    // Below is used to find denominator in
    // fraction form.
    int count_after_dot = 0;
  
    // Used to find value of count_after_dot
    boolean dot_seen = false;
  
    // To find numerator in fraction form of
    // given number. For example, for 30.25,
    // numerator would be 3025.
    int num = 0;
    for (int i = 0; i < n; i++) {
      if (str.charAt(i) != '.') {
        num = num * 10 + (str.charAt(i) - '0');
        if (dot_seen == true)
          count_after_dot++;
      } else
        dot_seen = true;
    }
  
    // If there was no dot, then number
    // is already a natural.
    if (dot_seen == false)
      return 1;
  
    // Find denominator in fraction form. For example,
    // for 30.25, denominator is 100
    int dem = (int)Math.pow(10, count_after_dot);
  
    // Result is denominator divided by
    // GCD-of-numerator-and-denominator. For example, for
    // 30.25, result is 100 / GCD(3025, 100) = 100/25 = 4
    return (dem / gcd(num, dem));
  }
  
  // Driver code
  public static void main(String[] args) {
    String str = "5.125";
    System.out.print(findnum(str));
  }
}
// This code is contributed by Anant Agarwal.

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find the smallest number to multiply
# to convert a floating point number into natural number.
# Finding GCD of two number
import math
def gcd(a, b):
  
    if (b == 0):
        return a
    return gcd(b, a%b)
   
# Returns smallest integer k such that k * str becomes
# natural. str is an input floating point number
def findnum(str):
      
    # Find size of string representing a
    # floating point number.
    n = len(str)
    # Below is used to find denominator in
    # fraction form.
    count_after_dot = 0
   
    # Used to find value of count_after_dot
    dot_seen = 0
   
    # To find numerator in fraction form of
    # given number. For example, for 30.25,
    # numerator would be 3025.
    num = 0
    for i in range(n):
        if (str[i] != '.'):
            num = num*10 + int(str[i])
            if (dot_seen == 1):
                count_after_dot += 1
        else:
            dot_seen = 1
   
    # If there was no dot, then number
    # is already a natural.
    if (dot_seen == 0):
       return 1
   
    # Find denominator in fraction form. For example,
    # for 30.25, denominator is 100
    dem = int(math.pow(10, count_after_dot))
   
    # Result is denominator divided by
    # GCD-of-numerator-and-denominator. For example, for
    # 30.25, result is 100 / GCD(3025,100) = 100/25 = 4
    return (dem / gcd(num, dem))
   
# Driver Program
  
str = "5.125"
print findnum(str)
  
# Contributed by: Afzal Ansari

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the smallest
// number to multiply to convert a 
// floating point number into
// natural number.
using System;
  
class GFG {
      
// Finding GCD of two number
static int gcd(int a, int b)
{
    if (b == 0)
    return a;
    return gcd(b, a % b);
}
  
// Returns smallest integer k
// such that k * str becomes
// natural. str is an input 
// floating point number
static int findnum(String str) 
{
  
    // Find size of string representing
    // a floating point number.
    int n = str.Length;
  
    // Below is used to find denominator
    // in fraction form.
    int count_after_dot = 0;
  
    // Used to find value of count_after_dot
    bool dot_seen = false;
  
    // To find numerator in fraction form of
    // given number. For example, for 30.25,
    // numerator would be 3025.
    int num = 0;
    for (int i = 0; i < n; i++)
    {
        if (str[i] != '.')
        {
            num = num * 10 + (str[i] - '0');
            if (dot_seen == true)
            count_after_dot++;
        }
        else
            dot_seen = true;
    }
  
    // If there was no dot, then 
    // number is already a natural.
    if (dot_seen == false)
    return 1;
  
    // Find denominator in fraction form. 
    // For example, for 30.25, 
    // denominator is 100
    int dem = (int)Math.Pow(10, count_after_dot);
  
    // Result is denominator divided by
    // GCD-of-numerator-and-denominator. 
    // For example, for 30.25, result is
    // 100 / GCD(3025, 100) = 100/25 = 4
    return (dem / gcd(num, dem));
}
  
// Driver code
public static void Main() 
{
    String str = "5.125";
    Console.Write(findnum(str));
}
}
  
// This code is contributed by Nitin Mittal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the smallest
// number to multiply to convert a 
// floating point number into 
// natural number.
  
// Finding GCD of two number
function gcd($a, $b)
{
    if ($b == 0)
        return $a;
    return gcd($b, $a % $b);
}
  
// Returns smallest integer k 
// such that k * str becomes
// natural. str is an input 
// floating point number
function findnum( $str)
{
      
    // Find size of string
    // representing a floating
    // point number.
    $n = strlen($str);
  
    // Below is used to 
    // find denominator in
    // fraction form.
    $count_after_dot = 0;
  
    // Used to find value 
    // of count_after_dot
    $dot_seen = false;
  
    // To find numerator in 
    // fraction form of
    // given number. For 
    // example, for 30.25,
    // numerator would be 3025.
    $num = 0;
    for($i = 0; $i < $n; $i++)
    {
        if ($str[$i] != '.')
        {
            $num = $num*10 + ($str[$i] - '0');
            if ($dot_seen == true)
                $count_after_dot++;
        }
        else
            $dot_seen = true;
    }
  
    // If there was no dot,
    // then number is 
    // already a natural.
    if ($dot_seen == false)
    return 1;
  
    // Find denominator in
    // fraction form. For
    // example, for 30.25, 
    // denominator is 100
    $dem = pow(10, $count_after_dot);
  
    // Result is denominator 
    // divided by GCD-of-
    // numerator-and-denominator.
    // For example, for 30.25, 
    // result is 100 / GCD(3025,100)
    // = 100/25 = 4
    return ($dem / gcd($num, $dem));
}
  
// Driver Code
{
    $str = "5.125";
    echo findnum($str) ;
    return 0;
}
  
// This code is contributed by nitin mittal
?>

chevron_right



Output:

8

This article is contributed by Anuj Chauhan(anuj0503). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : nitin mittal



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.