Reducing fractions to lowest terms
Here given code implementation process.
/*
C program for
Reducing fractions to lowest terms
*/
#include <stdio.h>
// Returns the greatest common divisor of two numbers
int gcd(int a, int b)
{
if (b == 0)
{
return a;
}
return gcd(b, a % b);
}
void lowestTerms(int a, int b)
{
int divisor = gcd(a, b);
// Display given number
printf("\n Given A : %d", a);
printf("\n Given B : %d", b);
// Show lowest form
printf("\n A : %d", (a / divisor));
printf("\n B : %d\n", (b / divisor));
}
int main()
{
// Test
lowestTerms(21, 42);
lowestTerms(65, 30);
lowestTerms(42, 13);
lowestTerms(32, 80);
return 0;
}
Output
Given A : 21
Given B : 42
A : 1
B : 2
Given A : 65
Given B : 30
A : 13
B : 6
Given A : 42
Given B : 13
A : 42
B : 13
Given A : 32
Given B : 80
A : 2
B : 5
/*
Java Program for
Reducing fractions to lowest terms
*/
public class Fractions
{
// Returns the greatest common divisor of two numbers
public int gcd(int a, int b)
{
if (b == 0)
{
return a;
}
return gcd(b, a % b);
}
public void lowestTerms(int a, int b)
{
int divisor = gcd(a, b);
// Display given number
System.out.print("\n Given A : " + a);
System.out.print("\n Given B : " + b);
// Show lowest form
System.out.print("\n A : " + (a / divisor));
System.out.println("\n B : " + (b / divisor));
}
public static void main(String[] args)
{
Fractions task = new Fractions();
// Test
task.lowestTerms(21, 42);
task.lowestTerms(65, 30);
task.lowestTerms(42, 13);
task.lowestTerms(32, 80);
}
}
Output
Given A : 21
Given B : 42
A : 1
B : 2
Given A : 65
Given B : 30
A : 13
B : 6
Given A : 42
Given B : 13
A : 42
B : 13
Given A : 32
Given B : 80
A : 2
B : 5
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Reducing fractions to lowest terms
*/
class Fractions
{
public:
// Returns the greatest common divisor of two numbers
int gcd(int a, int b)
{
if (b == 0)
{
return a;
}
return this->gcd(b, a % b);
}
void lowestTerms(int a, int b)
{
int divisor = this->gcd(a, b);
// Display given number
cout << "\n Given A : " << a;
cout << "\n Given B : " << b;
// Show lowest form
cout << "\n A : " << (a / divisor);
cout << "\n B : " << (b / divisor) << endl;
}
};
int main()
{
Fractions *task = new Fractions();
// Test
task->lowestTerms(21, 42);
task->lowestTerms(65, 30);
task->lowestTerms(42, 13);
task->lowestTerms(32, 80);
return 0;
}
Output
Given A : 21
Given B : 42
A : 1
B : 2
Given A : 65
Given B : 30
A : 13
B : 6
Given A : 42
Given B : 13
A : 42
B : 13
Given A : 32
Given B : 80
A : 2
B : 5
// Include namespace system
using System;
/*
Csharp Program for
Reducing fractions to lowest terms
*/
public class Fractions
{
// Returns the greatest common divisor of two numbers
public int gcd(int a, int b)
{
if (b == 0)
{
return a;
}
return this.gcd(b, a % b);
}
public void lowestTerms(int a, int b)
{
int divisor = this.gcd(a, b);
// Display given number
Console.Write("\n Given A : " + a);
Console.Write("\n Given B : " + b);
// Show lowest form
Console.Write("\n A : " + (a / divisor));
Console.WriteLine("\n B : " + (b / divisor));
}
public static void Main(String[] args)
{
Fractions task = new Fractions();
// Test
task.lowestTerms(21, 42);
task.lowestTerms(65, 30);
task.lowestTerms(42, 13);
task.lowestTerms(32, 80);
}
}
Output
Given A : 21
Given B : 42
A : 1
B : 2
Given A : 65
Given B : 30
A : 13
B : 6
Given A : 42
Given B : 13
A : 42
B : 13
Given A : 32
Given B : 80
A : 2
B : 5
package main
import "fmt"
/*
Go Program for
Reducing fractions to lowest terms
*/
type Fractions struct {}
func getFractions() * Fractions {
var me *Fractions = &Fractions {}
return me
}
// Returns the greatest common divisor of two numbers
func(this Fractions) gcd(a, b int) int {
if b == 0 {
return a
}
return this.gcd(b, a % b)
}
func(this Fractions) lowestTerms(a, b int) {
var divisor int = this.gcd(a, b)
// Display given number
fmt.Print("\n Given A : ", a)
fmt.Print("\n Given B : ", b)
// Show lowest form
fmt.Print("\n A : ", (a / divisor))
fmt.Println("\n B :", (b / divisor))
}
func main() {
var task * Fractions = getFractions()
// Test
task.lowestTerms(21, 42)
task.lowestTerms(65, 30)
task.lowestTerms(42, 13)
task.lowestTerms(32, 80)
}
Output
Given A : 21
Given B : 42
A : 1
B : 2
Given A : 65
Given B : 30
A : 13
B : 6
Given A : 42
Given B : 13
A : 42
B : 13
Given A : 32
Given B : 80
A : 2
B : 5
<?php
/*
Php Program for
Reducing fractions to lowest terms
*/
class Fractions
{
// Returns the greatest common divisor of two numbers
public function gcd($a, $b)
{
if ($b == 0)
{
return $a;
}
return $this->gcd($b, $a % $b);
}
public function lowestTerms($a, $b)
{
$divisor = $this->gcd($a, $b);
// Display given number
echo("\n Given A : ".$a);
echo("\n Given B : ".$b);
// Show lowest form
echo("\n A : ".((int)($a / $divisor)));
echo("\n B : ".((int)($b / $divisor)).
"\n");
}
}
function main()
{
$task = new Fractions();
// Test
$task->lowestTerms(21, 42);
$task->lowestTerms(65, 30);
$task->lowestTerms(42, 13);
$task->lowestTerms(32, 80);
}
main();
Output
Given A : 21
Given B : 42
A : 1
B : 2
Given A : 65
Given B : 30
A : 13
B : 6
Given A : 42
Given B : 13
A : 42
B : 13
Given A : 32
Given B : 80
A : 2
B : 5
/*
Node JS Program for
Reducing fractions to lowest terms
*/
class Fractions
{
// Returns the greatest common divisor of two numbers
gcd(a, b)
{
if (b == 0)
{
return a;
}
return this.gcd(b, a % b);
}
lowestTerms(a, b)
{
var divisor = this.gcd(a, b);
// Display given number
process.stdout.write("\n Given A : " + a);
process.stdout.write("\n Given B : " + b);
// Show lowest form
process.stdout.write("\n A : " + (parseInt(a / divisor)));
console.log("\n B : " + (parseInt(b / divisor)));
}
}
function main()
{
var task = new Fractions();
// Test
task.lowestTerms(21, 42);
task.lowestTerms(65, 30);
task.lowestTerms(42, 13);
task.lowestTerms(32, 80);
}
main();
Output
Given A : 21
Given B : 42
A : 1
B : 2
Given A : 65
Given B : 30
A : 13
B : 6
Given A : 42
Given B : 13
A : 42
B : 13
Given A : 32
Given B : 80
A : 2
B : 5
# Python 3 Program for
# Reducing fractions to lowest terms
class Fractions :
# Returns the greatest common divisor of two numbers
def gcd(self, a, b) :
if (b == 0) :
return a
return self.gcd(b, a % b)
def lowestTerms(self, a, b) :
divisor = self.gcd(a, b)
# Display given number
print("\n Given A : ", a, end = "")
print("\n Given B : ", b, end = "")
# Show lowest form
print("\n A : ", (int(a / divisor)), end = "")
print("\n B : ", (int(b / divisor)))
def main() :
task = Fractions()
# Test
task.lowestTerms(21, 42)
task.lowestTerms(65, 30)
task.lowestTerms(42, 13)
task.lowestTerms(32, 80)
if __name__ == "__main__": main()
Output
Given A : 21
Given B : 42
A : 1
B : 2
Given A : 65
Given B : 30
A : 13
B : 6
Given A : 42
Given B : 13
A : 42
B : 13
Given A : 32
Given B : 80
A : 2
B : 5
# Ruby Program for
# Reducing fractions to lowest terms
class Fractions
# Returns the greatest common divisor of two numbers
def gcd(a, b)
if (b == 0)
return a
end
return self.gcd(b, a % b)
end
def lowestTerms(a, b)
divisor = self.gcd(a, b)
# Display given number
print("\n Given A : ", a)
print("\n Given B : ", b)
# Show lowest form
print("\n A : ", (a / divisor))
print("\n B : ", (b / divisor), "\n")
end
end
def main()
task = Fractions.new()
# Test
task.lowestTerms(21, 42)
task.lowestTerms(65, 30)
task.lowestTerms(42, 13)
task.lowestTerms(32, 80)
end
main()
Output
Given A : 21
Given B : 42
A : 1
B : 2
Given A : 65
Given B : 30
A : 13
B : 6
Given A : 42
Given B : 13
A : 42
B : 13
Given A : 32
Given B : 80
A : 2
B : 5
/*
Scala Program for
Reducing fractions to lowest terms
*/
class Fractions()
{
// Returns the greatest common divisor of two numbers
def gcd(a: Int, b: Int): Int = {
if (b == 0)
{
return a;
}
return gcd(b, a % b);
}
def lowestTerms(a: Int, b: Int): Unit = {
var divisor: Int = gcd(a, b);
// Display given number
print("\n Given A : " + a);
print("\n Given B : " + b);
// Show lowest form
print("\n A : " + (a / divisor));
println("\n B : " + (b / divisor));
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Fractions = new Fractions();
// Test
task.lowestTerms(21, 42);
task.lowestTerms(65, 30);
task.lowestTerms(42, 13);
task.lowestTerms(32, 80);
}
}
Output
Given A : 21
Given B : 42
A : 1
B : 2
Given A : 65
Given B : 30
A : 13
B : 6
Given A : 42
Given B : 13
A : 42
B : 13
Given A : 32
Given B : 80
A : 2
B : 5
/*
Swift 4 Program for
Reducing fractions to lowest terms
*/
class Fractions
{
// Returns the greatest common divisor of two numbers
func gcd(_ a: Int, _ b: Int) -> Int
{
if (b == 0)
{
return a;
}
return self.gcd(b, a % b);
}
func lowestTerms(_ a: Int, _ b: Int)
{
let divisor: Int = self.gcd(a, b);
// Display given number
print("\n Given A : ", a, terminator: "");
print("\n Given B : ", b, terminator: "");
// Show lowest form
print("\n A : ", (a / divisor), terminator: "");
print("\n B : ", (b / divisor));
}
}
func main()
{
let task: Fractions = Fractions();
// Test
task.lowestTerms(21, 42);
task.lowestTerms(65, 30);
task.lowestTerms(42, 13);
task.lowestTerms(32, 80);
}
main();
Output
Given A : 21
Given B : 42
A : 1
B : 2
Given A : 65
Given B : 30
A : 13
B : 6
Given A : 42
Given B : 13
A : 42
B : 13
Given A : 32
Given B : 80
A : 2
B : 5
/*
Kotlin Program for
Reducing fractions to lowest terms
*/
class Fractions
{
// Returns the greatest common divisor of two numbers
fun gcd(a: Int, b: Int): Int
{
if (b == 0)
{
return a;
}
return this.gcd(b, a % b);
}
fun lowestTerms(a: Int, b: Int): Unit
{
val divisor: Int = this.gcd(a, b);
// Display given number
print("\n Given A : " + a);
print("\n Given B : " + b);
// Show lowest form
print("\n A : " + (a / divisor));
println("\n B : " + (b / divisor));
}
}
fun main(args: Array < String > ): Unit
{
val task: Fractions = Fractions();
// Test
task.lowestTerms(21, 42);
task.lowestTerms(65, 30);
task.lowestTerms(42, 13);
task.lowestTerms(32, 80);
}
Output
Given A : 21
Given B : 42
A : 1
B : 2
Given A : 65
Given B : 30
A : 13
B : 6
Given A : 42
Given B : 13
A : 42
B : 13
Given A : 32
Given B : 80
A : 2
B : 5
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment