-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRationalDemo.java
More file actions
141 lines (137 loc) · 3.33 KB
/
Copy pathRationalDemo.java
File metadata and controls
141 lines (137 loc) · 3.33 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import java.util.*;
class Rational
{
int numerator,denomenator;
Rational()
{
numerator=0;
denomenator=1;
}
Rational(int n,int d)
{
numerator=n;
denomenator=d;
}
private boolean checkPrime(int n) //Function to check if prime
{
int ctr=0;
for(int i=1;i<=n/2;i++)
{
if(n%i==0) //Checking for divisibility
ctr++; //Increasing counter if divisible..
}
if(ctr==1)
return true;
else
return false;
}
private int gcd(int n1,int n2) //Function to Calculate Greatest Common Divisor
{
int pro=1;
for(int i=1;(i<=n1)&&(i<=n2);i++)
{
if(n1%i==0) //Checking for divisibility of n1
if(n2%i==0) //Checking for divisibility of n2
if(checkPrime(i)) //Checking for Prime
{
pro*=i;
n1/=i;
n2/=i;
i=1;
}
}
return pro;
}
void print()
{
System.out.println("Rational Number="+numerator+"/"+denomenator);
}
Rational add(Rational f)
{
Rational f1=new Rational();
int lcm=denomenator*f.denomenator;
f1.numerator=(numerator*f.denomenator)+(f.numerator*denomenator);
f1.denomenator=lcm;
return f1;
}
Rational pro(Rational f)
{
Rational f1=new Rational();
f1.numerator=numerator*f.numerator;
f1.denomenator=denomenator*f.denomenator;
return f1;
}
Rational simplify()
{
Rational ob=new Rational();
int temp=gcd(numerator,denomenator);
ob.numerator=numerator/temp;
ob.denomenator=denomenator/temp;
return ob;
}
}
public class RationalDemo extends Rational {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the numerator of rational number");
int n=sc.nextInt();
System.out.println("Enter the denomenator of rational number");
int d=sc.nextInt();
Rational ob=new Rational(n,d);
System.out.println("Enter the choice \n 1 to print simplified rational number \n 2 to perform addition \n 3 to perform multiplication");
int ch=sc.nextInt();
switch(ch)
{
case 1:
{
ob.print();
Rational ob1=new Rational();
ob1=ob.simplify();
System.out.println("After Simplification");
ob1.print();
break;
}
case 2:
{
System.out.println("Enter the numerator of rational number");
int a=sc.nextInt();
System.out.println("Enter the denomenator of rational number");
int b=sc.nextInt();
Rational ob1=new Rational(a,b);
Rational ob2=new Rational();
ob.print();
ob1.print();
ob2=ob.add(ob1);
System.out.println("After Additon");
ob2.print();
ob2=ob2.simplify();
System.out.println("Simplified Answer");
ob2.print();
break;
}
case 3:
{
System.out.println("Enter the numerator of rational number");
int a=sc.nextInt();
System.out.println("Enter the denomenator of rational number");
int b=sc.nextInt();
Rational ob1=new Rational(a,b);
Rational ob2=new Rational();
ob.print();
ob1.print();
ob2=ob.pro(ob1);
System.out.println("After Multiplication");
ob2.print();
ob2=ob2.simplify();
System.out.println("Simplified Answer");
ob2.print();
break;
}
default:
{
System.out.println("Wrong choice entered,Program will exit");
System.exit(0);
}
}
}
}