Basic operations on
Rational Numbers using C, C++ and Java.
The same program is
written in the above languages, helping you to grasp the basic
principles of classes and objects in C++ and Java. Students moving
from C++ to Java will be able to relate the similarities at the same
time the major differences between them.
Code
in C
#include<stdio.h>
#define
TRUE 1
#define
FALSE 0
typedef
struct
{
int
numerator;
int
denominator;
}
RATIONAL;
void
multiply(RATIONAL *frst,RATIONAL *scnd);
void
add(RATIONAL *frst,RATIONAL *scnd);
void
subtract(RATIONAL *frst,RATIONAL *scnd);
int
equality(RATIONAL *frst,RATIONAL *scnd);
void
divide(RATIONAL *frst,RATIONAL *scnd);
void
reduce(RATIONAL *rat);
int
main()
{
RATIONAL
x,y;
int
choice;
printf("\n
Enter the numerator and denominator of the first rational:");
scanf("%d
%d",&x.numerator,&x.denominator);
printf("\n
Enter the numerator and denominator of the second rational:");
scanf("%d
%d",&y.numerator,&y.denominator);
//printf("\n
Reduced form of the input rationals are:");
reduce(&x);
//printf("\n
%d / %d ",x.numerator,x.denominator);
reduce(&y);
//printf("\n
%d / %d ",y.numerator,y.denominator);
printf("\n
Press 1 for addition:");
printf("\n
Press 2 for subtraction:");
printf("\n
Press 3 for multiplication:");
printf("\n
Press 4 for divison:");
printf("\n
Press 5 to check for equatilty");
printf("\n
Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case
1:
add(&x,&y);
break;
case
2:
subtract(&x,&y);
break;
case
3:
multiply(&x,&y);
break;
case
4:
divide(&x,&y);
break;
case
5:
equality(&x,&y)==TRUE
? printf("\n They are equal:\n") : printf("\n They are
unequal:\n");
break;
default:
break;
}
return
0;
}
void
divide(RATIONAL *frst,RATIONAL *scnd)
{
RATIONAL
sum;
sum.numerator
= (frst->numerator)*(scnd->denominator);
sum.denominator
= (frst->denominator)*(scnd->numerator);
reduce(&sum);
printf("\n
After dividing the new rational is: %d / %d
\n",sum.numerator,sum.denominator);
}
void
multiply(RATIONAL *frst,RATIONAL *scnd)
{
RATIONAL
sum;
sum.numerator
= (frst->numerator)*(scnd->numerator);
sum.denominator
= (frst->denominator)*(scnd->denominator);
reduce(&sum);
printf("\n
Product of the rationals is: %d / %d
\n",sum.numerator,sum.denominator);
}
int
equality(RATIONAL *frst,RATIONAL *scnd)
{
return
( (frst->numerator == scnd->numerator &&
frst->denominator == scnd->denominator) ? TRUE : FALSE );
}
void
subtract(RATIONAL *frst,RATIONAL *scnd)
{
int
lcm=1,i;
for(i=1;;i++)
{
if
(frst->denominator % i == 0 && scnd->denominator % i==
0)
break;
}
lcm =
((frst->denominator)*(scnd->denominator))/i;
RATIONAL
sum;
sum.numerator
=
(frst->numerator)*(lcm/frst->denominator)-(scnd->numerator)*(lcm/scnd->denominator);
sum.denominator
= lcm;
reduce(&sum);
printf("\n
Sum of the rationals is: %d / %d \n",sum.numerator,sum.denominator);
}
void
add(RATIONAL *frst,RATIONAL *scnd)
{
int
lcm=1,i;
for(i=1;;i++)
{
if
(frst->denominator % i == 0 && scnd->denominator % i==
0)
break;
}
lcm =
((frst->denominator)*(scnd->denominator))/i;
RATIONAL
sum;
sum.numerator
= (frst->numerator)*(lcm/frst->denominator)+
(scnd->numerator)*(lcm/scnd->denominator);
sum.denominator
= lcm;
reduce(&sum);
printf("\n
Sum of the rationals is: %d / %d \n",sum.numerator,sum.denominator);
}
void
reduce(RATIONAL *rat)
{
int
a,b,rem;
if(rat->numerator
> rat->denominator) {
a =
rat->numerator;
b =
rat->denominator;
}
else
{
a =
rat->denominator;
b =
rat->numerator;
}
while(b!=0)
{
rem
= a%b;
a =
b;
b =
rem;
}
rat->numerator
/= a;
rat->denominator
/= a;
}
Code
in C++
#include<iostream>
using
namespace std;
#define
TRUE 1
#define
FALSE 0
class
Rational {
private:
int
numerator;
int
denominator;
public:
Rational()
{ numerator = 1; denominator = 1;}
void
setValues()
{
cout<<"\n
Enter the numerator:";
cin>>numerator;
cout<<"\n
Enter the denominator:";
cin>>denominator;
}
void
dispValue()
{
cout<<"\n"<<numerator<<"/"<<denominator<<"\n";
}
void
reduce();
void
add(Rational r);
void
subtract(Rational r);
void
multiply(Rational r);
void
divide(Rational r);
int
equality(Rational r);
};
int
Rational::equality(Rational r)
{
return
( (numerator == r.numerator && denominator == r.denominator)
? TRUE : FALSE );
}
void
Rational::divide(Rational r)
{
Rational
sum;
sum.numerator
= (numerator)*(r.denominator);
sum.denominator
= (denominator)*(r.numerator);
sum.reduce();
cout<<"\n
On divison the rationals:";
sum.dispValue();
}
void
Rational::multiply(Rational r)
{
Rational
sum;
sum.numerator
= (numerator)*(r.numerator);
sum.denominator
= (denominator)*(r.denominator);
sum.reduce();
cout<<"\n
Product of the rationals is:";
sum.dispValue();
}
void
Rational::subtract(Rational r)
{
int
lcm=1,i;
for(i=1;;i++)
{
if
(denominator % i == 0 && r.denominator % i== 0)
break;
}
lcm =
((denominator)*(r.denominator))/i;
Rational
sum;
sum.numerator
= numerator*(lcm/denominator)-(r.numerator)*(lcm/r.denominator);
sum.denominator
= lcm;
sum.reduce();
cout<<"\n
Difference of the rationals is:";
sum.dispValue();
}
void
Rational::add(Rational r)
{
int
lcm=1,i;
for(i=1;;i++)
{
if
(denominator % i == 0 && r.denominator % i== 0)
break;
}
lcm =
((denominator)*(r.denominator))/i;
Rational
sum;
sum.numerator
= numerator*(lcm/denominator)+(r.numerator)*(lcm/r.denominator);
sum.denominator
= lcm;
sum.reduce();
cout<<"\n
Sum of the rationals is:";
sum.dispValue();
}
void
Rational::reduce()
{
int
a,b,rem;
if(numerator
> denominator) {
a =
numerator;
b =
denominator;
}
else
{
a =
denominator;
b =
numerator;
}
while(b!=0)
{
rem
= a%b;
a =
b;
b =
rem;
}
numerator
/= a;
denominator
/= a;
}
int
main()
{
int
choice;
Rational
x,y;
x.setValues();
y.setValues();
x.reduce();
y.reduce();
cout<<"\n
Press 1 for addition:";
cout<<"\n
Press 2 for subtraction:";
cout<<"\n
Press 3 for multiplication:";
cout<<"\n
Press 4 for divison:";
cout<<"\n
Press 5 to check for equatilty";
cout<<"\n
Enter your choice:";
cin>>choice;
switch(choice)
{
case
1:
x.add(y);
break;
case
2:
x.subtract(y);
break;
case
3:
x.multiply(y);
break;
case
4:
x.divide(y);
break;
case
5:
x.equality(y)==TRUE
? cout<<"\n They are equal:\n" : cout<<"\n
They are unequal:\n";
break;
default:
break;
}
return
0;
}
Code
in JAVA
import
java.util.*;
public
class Rational
{
public
static void main(String[] args)
{
int
choice;
Scanner
in = new Scanner(System.in);
OperationRational
x = new OperationRational();
OperationRational
y = new OperationRational();
x.setValues();
y.setValues();
x.reduce();
y.reduce();
System.out.println("You
have entered the following rationals:");
x.dispValue();
y.dispValue();
System.out.println("Press
1 for addition:");
System.out.println("Press
2 for subtraction:");
System.out.println("Press
3 for multiplication:");
System.out.println("Press
4 for divison:");
System.out.println("Press
5 to check for equatilty");
System.out.println("Enter
your choice:");
choice
= in.nextInt();
switch(choice)
{
case
1:
x.add(y);
break;
case
2:
x.subtract(y);
break;
case
3:
x.multiply(y);
break;
case
4:
x.divide(y);
break;
case
5:
if
(x.equality(y) == true)
System.out.println("They
are equal:");
else
System.out.println("They are unequal:");
break;
default:
break;
}
}
}
class
OperationRational
{
OperationRational()
{
numerator = 1;
denominator = 1;
}
public void setValues()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the numerator:");
numerator = in.nextInt();
System.out.println("Enter the denominator:");
denominator = in.nextInt();
}
public
void dispValue()
{
System.out.println(numerator+"/"+denominator);
}
public void reduce()
{
int
a,b,rem;
if(numerator > denominator) {
a =
numerator;
b =
denominator;
}
else {
a =
denominator;
b =
numerator;
}
while(b!=0) {
rem
= a%b;
a =
b;
b =
rem;
}
numerator /= a;
denominator /= a;
}
public void add(OperationRational r)
{
int
lcm=1,i;
for(i=1;;i++)
{
if
(denominator % i == 0 && r.denominator % i== 0)
break;
}
lcm
= ((denominator)*(r.denominator))/i;
OperationRational
sum = new OperationRational();
sum.numerator
= numerator*(lcm/denominator)+(r.numerator)*(lcm/r.denominator);
sum.denominator
= lcm;
sum.reduce();
System.out.println("Sum
of the rationals is:");
sum.dispValue();
}
public void subtract(OperationRational r)
{
int
lcm=1,i;
for(i=1;;i++)
{
if
(denominator % i == 0 && r.denominator % i== 0)
break;
}
lcm
= ((denominator)*(r.denominator))/i;
OperationRational
sum = new OperationRational();
sum.numerator
= numerator*(lcm/denominator)-(r.numerator)*(lcm/r.denominator);
sum.denominator
= lcm;
sum.reduce();
System.out.println("Difference
of the rationals is:");
sum.dispValue();
}
public void multiply(OperationRational r)
{
OperationRational
sum = new OperationRational();;
sum.numerator
= (numerator)*(r.numerator);
sum.denominator
= (denominator)*(r.denominator);
sum.reduce();
System.out.println("Product
of the rationals is:");
sum.dispValue();
}
public void divide(OperationRational r)
{
OperationRational
sum = new OperationRational();;
sum.numerator
= (numerator)*(r.denominator);
sum.denominator
= (denominator)*(r.numerator);
sum.reduce();
System.out.println("On
divison the rationals:");
sum.dispValue();
}
public boolean equality(OperationRational r)
{
return ( (numerator == r.numerator && denominator ==
r.denominator) ? true : false );
}
private int numerator;
private int denominator;
}