# First occurrence of a digit in a given fraction

Given three integers a, b and c, find the first occurrence of c in a/b after the decimal point. If it does not exists, print -1.

Examples:

Input : a = 2 b = 3 c = 6 Output : 1 Explanation: 0.666666.. so 6 occurs at first place of a/b after decimal point Input : a = 1 b = 4 c = 5 Output : 2 Explanation: 1 / 4 = 0.25 which gives 5's position to be 2.

A **naive approach** will be to perform the division and keep the decimal part and iterate and check if the given number exists or not. This will not work well when divisions such as 2/3 is done as it yields 0.666666666, but in programming language it will round it off to 0.666667 so we get a 7 also which does not exists in the original a/b

An **efficient approach** will be the mathematical one, if we modulate every-time a by b and multiply it with 10 we get the integers after the decimal part every-time. The number of modulations required will be b as it will have a maximum of b integers after decimal point. So, we compare it with c and get our desired value if it is present.

Below is the implementation of the above approach :

## C++

`// CPP program to find first occurrence` `// of c in a/b` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// function to print the first digit` `int` `first(` `int` `a, ` `int` `b, ` `int` `c)` `{` ` ` `// reduce the number to its mod` ` ` `a %= b;` ` ` `// traverse for every decimal places` ` ` `for` `(` `int` `i = 1; i <= b; i++)` ` ` `{` ` ` `// get every fraction places` ` ` `// when (a*10/b)/c` ` ` `a = a * 10;` ` ` ` ` `// check if it is equal to` ` ` `// the required integer` ` ` `if` `(a / b == c)` ` ` `return` `i;` ` ` ` ` `// mod the number` ` ` `a %= b;` ` ` `}` ` ` `return` `-1;` `}` `// driver program to test the above function` `int` `main()` `{` ` ` `int` `a = 1, b = 4, c = 5;` ` ` `cout << first(a, b, c);` ` ` `return` `0;` `}` |

## Java

`// Java program to find first occurrence` `// of c in a/b` `import` `java.util.*;` `import` `java.lang.*;` `public` `class` `GfG{` ` ` `// Function to print the first digit` ` ` `public` `static` `int` `first(` `int` `a, ` `int` `b, ` `int` `c)` ` ` `{` ` ` `// Reduce the number to its mod` ` ` `a %= b;` ` ` `// Traverse for every decimal places` ` ` `for` `(` `int` `i = ` `1` `; i <= b; i++)` ` ` `{` ` ` `// Get every fraction places` ` ` `// when (a*10/b)/c` ` ` `a = a * ` `10` `;` ` ` ` ` `// Check if it is equal to` ` ` `// the required integer` ` ` `if` `(a / b == c)` ` ` `return` `i;` ` ` ` ` `// Mod the number` ` ` `a %= b;` ` ` `}` ` ` `return` `-` `1` `;` ` ` `}` ` ` ` ` `// Driver function` ` ` `public` `static` `void` `main(String argc[]){` ` ` `int` `a = ` `1` `, b = ` `4` `, c = ` `5` `;` ` ` `System.out.println(first(a, b, c));` ` ` `}` ` ` `}` `/* This code is contributed by Sagar Shukla */` |

## Python3

`# Python3 program to find first occurrence` `# of c in a/b` `# function to print the first digit` `def` `first( a , b , c ):` ` ` `# reduce the number to its mod` ` ` `a ` `%` `=` `b` ` ` ` ` `# traverse for every decimal places` ` ` `for` `i ` `in` `range` `(` `1` `, b ` `+` `1` `):` ` ` `# get every fraction places` ` ` `# when (a*10/b)/c` ` ` `a ` `=` `a ` `*` `10` ` ` `# check if it is equal to` ` ` `# the required integer` ` ` `if` `int` `(a ` `/` `b) ` `=` `=` `c:` ` ` `return` `i` ` ` ` ` `# mod the number` ` ` `a ` `%` `=` `b` ` ` ` ` `return` `-` `1` `# driver code to test the above function` `a ` `=` `1` `b ` `=` `4` `c ` `=` `5` `print` `(first(a, b, c))` `# This code is contributed by "Sharad_Bhardwaj".` |

## C#

`// C# program to find first occurrence` `// of c in a/b` `using` `System;` `public` `class` `GfG{` ` ` ` ` `// Function to print the first digit` ` ` `public` `static` `int` `first(` `int` `a, ` `int` `b, ` `int` `c)` ` ` `{` ` ` `// Reduce the number to its mod` ` ` `a %= b;` ` ` `// Traverse for every decimal places` ` ` `for` `(` `int` `i = 1; i <= b; i++)` ` ` `{` ` ` `// Get every fraction places` ` ` `// when (a*10/b)/c` ` ` `a = a * 10;` ` ` ` ` `// Check if it is equal to` ` ` `// the required integer` ` ` `if` `(a / b == c)` ` ` `return` `i;` ` ` ` ` `// Mod the number` ` ` `a %= b;` ` ` `}` ` ` `return` `-1;` ` ` `}` ` ` ` ` `// Driver function` ` ` `public` `static` `void` `Main() {` ` ` ` ` `int` `a = 1, b = 4, c = 5;` ` ` ` ` `Console.WriteLine(first(a, b, c));` ` ` `}` `}` `/* This code is contributed by vt_m */` |

## PHP

`<?php` `// PHP program to find first` `// occurrence of c in a/b` `// function to print` `// the first digit` `function` `first( ` `$a` `, ` `$b` `, ` `$c` `)` `{` ` ` ` ` `// reduce the number` ` ` `// to its mod` ` ` `$a` `%= ` `$b` `;` ` ` `// traverse for every` ` ` `// decimal places` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$b` `; ` `$i` `++)` ` ` `{` ` ` ` ` `// get every fraction places` ` ` `// when (a*10/b)/c` ` ` `$a` `= ` `$a` `* 10;` ` ` ` ` `// check if it is equal to` ` ` `// the required integer` ` ` `if` `(` `$a` `/ ` `$b` `== ` `$c` `)` ` ` `return` `$i` `;` ` ` ` ` `// mod the number` ` ` `$a` `%= ` `$b` `;` ` ` `}` ` ` `return` `-1;` `}` ` ` `// Driver Code` ` ` `$a` `= 1; ` `$b` `= 4; ` `$c` `= 5;` ` ` `echo` `first(` `$a` `, ` `$b` `, ` `$c` `);` `// This code is contributed by anuj_67.` `?>` |

## Javascript

`<script>` `// JavaScript program to find first occurrence` `// of c in a/b` ` ` `// Function to print the first digit` ` ` `function` `first(a, b, c)` ` ` `{` ` ` `// Reduce the number to its mod` ` ` `a %= b;` ` ` ` ` `// Traverse for every decimal places` ` ` `for` `(let i = 1; i <= b; i++)` ` ` `{` ` ` `// Get every fraction places` ` ` `// when (a*10/b)/c` ` ` `a = a * 10;` ` ` ` ` `// Check if it is equal to` ` ` `// the required integer` ` ` `if` `(a / b == c)` ` ` `return` `i;` ` ` ` ` `// Mod the number` ` ` `a %= b;` ` ` `}` ` ` `return` `-1;` ` ` `}` `// Driver Code` ` ` `let a = 1, b = 4, c = 5;` ` ` `document.write(first(a, b, c));` `// This code is contributed by chinmoy1997pal.` `</script>` |

Output:

2