Concepts Used

Strings

Difficulty Level

Hard

Problem Statement (Simplified):

Print the decimal output of fraction of two given numbers and enclose the repeating part in decimal places in brackets. For example, for a given input of 2 and 3, it's fraction is 2/3. It's decimal representation would be 0.6666 where 6 repeats indefinitely, so we cover 6 in brackets and puts output as 0.(6).

See original problem statement here

Solving Approach :

1) If a fraction is non-repeating, any of it’s remainder will come 0 on repetitive division according to the fundamentals of data structures in c++.
> 1) We know decimal portion repeats itself when a remainder repeats.
> 2) So we'll check if any remainder repeats and will note it's last position in quotient.
> 3) After we note it, we finish the process of division as now quotient will repeat itself if it goes on.
> 4) Now print the integer part, a decimal point, non-repeating decimal part, an opening bracket, repeating decimal part and an closing bracket if there is any recurring decimal places.
> 5) If there is no recurring decimal portion, just print the integer part, decimal point and fraction.

Solutions

 #include 

    int main()
    {
          int num,den;
          scanf("%d%d", &num,&den);

          int firstValue = num/den;
          num -= firstValue * den;

          int remPos[1000000] = {0};
          char quotient[15];
          int recurr = 0;

          int rem = num % den ;
          int quoPos = 0;
          remPos[rem] = 1;


          int i = 0;

          if(rem!=0)
            i = 1;

          for( ; rem && !recurr; i++){
            int temp = rem * 10;
            while(temp 0){
                    rem = temp;
                    quotient[quoPos++] = '0';
                    recurr = 1;
                    break;
              }
              temp *= 10;
              quotient[quoPos++] = '0';
            }

            if(recurr)
                break;

            num = temp;
            rem = num % den;
            quotient[quoPos++] = num/den + '0';
            if(rem == 0){
              break;
            }

            else{
                if(remPos[rem] > 0){
                  recurr = 1;
                  break;
                }
                else{
                  remPos[rem] = i+1;
                }
            }
          }
          quotient[quoPos] = '\0';
          if(!recurr){
            if(i == 0 )
              printf("%d",firstValue);
            else
              printf("%d.%s",firstValue,quotient);
          }else{
              printf("%d.",firstValue);
            for(int k=0; k

						 
#include 
    using namespace std;

    int main()
    {
        int num,den;
          cin>>num>>den;

          char dec[100000];
          int firstValue = num/den;
          num -= firstValue * den;

          int remPos[1000000] = {0};
          int remainders[1000000] = {0};
          char quotient[15];
          bool recurr = false;

          int rem = num % den ;
          int quoPos = 0;
          remPos[rem] = 1;


          int i = 0;

          if(rem!=0)
            i = 1;

          for( ; rem && !recurr; i++){
            int temp = rem * 10;
            while(temp 0){
                    rem = temp;
                    quotient[quoPos++] = '0';
                    recurr = true;
                    break;
              }
              temp *= 10;
              quotient[quoPos++] = '0';
            }

            if(recurr)
                break;

            num = temp;
            rem = num % den;
            quotient[quoPos++] = num/den + '0';
            if(rem == 0){
              break;
            }

            else{
                if(remPos[rem] > 0){
                  recurr = true;
                  break;
                }
                else{
                  remPos[rem] = i+1;
                }
            }
          }
          quotient[quoPos] = '\0';
          if(!recurr){
            if(i == 0 )
              cout<

import java.util.*;
    import java.io.*;
    import java.lang.Math;

    public class Main {
      public static void main(String args[]) throws IOException {

        Scanner sc= new Scanner(System.in);

          int num = sc.nextInt();
          int den = sc.nextInt();

          int firstValue = 0;
          int rem = 0;

          if(den != 0){
            firstValue = num/den;
            rem = num % den ;
          }

          num -= firstValue * den;

          int remPos[] = new int[1000000];
          String quotient = "";
          int recurr = 0;

          remPos[rem] = 1;


          int i = 0;

          if(rem!=0)
            i = 1;

          for( ; rem!=0 && recurr==0 ; i++){
            int temp = rem * 10;
            while(temp 0){
                    rem = temp;
                    quotient += '0';
                    break;
                  }
                temp *= 10;
                quotient += '0';
            }

            if(recurr == 1)
                break;

            num = temp;
            rem = num % den;
            quotient += (char)(num/den + (int)('0'));
            if(rem == 0){
              break;
            }

            else{
                if(remPos[rem] > 0){
                  recurr = 1;
                  break;
                }
                else{
                  remPos[rem] = i+1;
                }
            }
          }

          if(recurr==0){
            if(i == 0 )
              System.out.print(firstValue );
            else
                System.out.print(firstValue + "." + quotient);
          }else{
            System.out.print(firstValue + ".");

            for(int k=0; k

						 

Previous post Hero Villian
Next post Total numbers divisible by a number in a given range

Leave a Reply

Your email address will not be published. Required fields are marked *