Skip to main content

A Simple Fraction

Given a fraction. Convert it into a decimal. So simple :P
eg.
10/2 = 5
3/5 = 0.6
(The Question Begins Now)  :D
If the decimals are repeating recursively, then enclose them inside  ().
eg.
8/3 = 2.(6)
as 8/3 = 2.66666666.......  infinitly.   

Input:
The first line of each test case is an integer N denoting the numerator of fraction.
The second line of each test case is an integer D denoting the denominator of fraction.

Output:
Print decimal of that fraction in separate line for each test case.

Constraints:
1 ≤ N,D ≤ 2000

Example:
Input
4
2

8
3
Output
2

2.(6)

//JAVA CODE
import java.util.Scanner;
public class Main {
    static String simple_fraction(int a,int b){
        String s=a/b+"";
        if(a%b==0)
            return s;
        a%=b;
        String ad="";
        int[]n=new int[b];int count=0;
        while(a>0 && n[a]==0){
            n[a]=++count;
            a*=10;
            int temp=a/b;
            ad+=temp;
            a%=b;
        }
        count--;
        if(a>0){
            int temp=n[a]-1;
            ad=ad.substring(0,temp)+"("+ad.substring(temp)+")";
        }
        return s+"."+ad;
    }
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        int a=sc.nextInt();
        int b=sc.nextInt();
        System.out.println(simple_fraction(a,b));
    }
}

Comments

Popular posts from this blog

Inser 0 after Consecutive (K times) Of 1 is Found

Example: Input: Number of bits: 12 Bits: 1 0 1 1 0 1 1 0 1 1 1 1 Consecutive K: 2 Output: 1 0 1 1 0 0 1 1 0 0 1 1 0 1 1 0 //JAVA CODE import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc= new Scanner(System. in ); int n = sc.nextInt(); int k=sc.nextInt(); int []a = new int [n]; int temp= 0 ; //Inputs for ( int i= 0 ;i<n;i++) a[i]=sc.nextInt(); //print for ( int i= 0 ;i<n;i++){ System. out .print(a[i]+ " " ); if (a[i]== 1 ){ temp++; if (temp==k) { System. out .print( "0 " ); temp= 0 ; } } else temp= 0 ; } } }

zoho taxi question (3rd Round)

Taxi application Design a Call taxi booking application -There are n number of taxi’s. For simplicity, assume 4. But it should work for any number of taxi’s. -The are 6 points(A,B,C,D,E,F) -All the points are in a straight line, and each point is 15kms away from the adjacent points. -It takes 60 mins to travel from one point to another -Each taxi charges Rs.100 minimum for the first 5 kilometers and Rs.10 for the subsequent kilometers. -For simplicity, time can be entered as absolute time. Eg: 9hrs, 15hrs etc. -All taxi’s are initially stationed at A. -When a customer books a Taxi, a free taxi at that point is allocated -If no free taxi is available at that point, a free taxi at the nearest point is allocated. -If two taxi’s are free at the same point, one with lower earning is allocated -Note that the taxi only charges the customer from the pickup point to the drop point. Not the distance it travels from an adjacent point to pickup the customer. -If no taxi is free a...

ZOHO INTERVIEW QUESTION-11

QUESTION You’re given an even number n. If n=4, you have to print the following pattern : 4444 4334 4334 4444 If n=6, then the pattern should be like this : 666666 655556 654456 654456 655556 666666 PROGRAM : #include <stdio.h> #include<stdlib.h> int main() { int a,i,j,k,l,m; scanf("%d",&a); k=a; for(i=1;i<=a;i++) {         l=a;     m=k+1;     for(j=1;j<=a;j++)     {       if((j>=i && j<=(a-i+1)&& i<=(a/2)) || (j>=(a-i+1) && j<=i && i>(a/2)))       {       printf("%d",k);       }       else if(j<i)       {       printf("%d",l);       l--;       }     ...