-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractical13.java
More file actions
35 lines (29 loc) · 1.07 KB
/
practical13.java
File metadata and controls
35 lines (29 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Write a program that prompts the user to enter a decimal number and displays the number in a fraction.
import java.util.Scanner;
public class practical13 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a decimal number: ");
double decimalNumber = sc.nextDouble();
// Convert the decimal number to a fraction
int denominator = 1;
while (decimalNumber % 1 != 0) {
decimalNumber *= 10;
denominator *= 10;
}
int numerator = (int) decimalNumber;
// Simplify the fraction
int gcd = gcd(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
System.out.println("The fraction representation is: " + numerator + "/" + denominator);
sc.close();
}
// Function to calculate the greatest common divisor (GCD)
public static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
}