import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// caleb b
// Heron's Formula
int timesUsed = 0;
Scanner input = new Scanner(System.in);
while (timesUsed < 3) {
System.out.println("Enter the length of side A: ");
double a = input.nextDouble();
System.out.println("Enter the length of side B: ");
double b = input.nextDouble();
System.out.println("Enter the length of side C: ");
double c = input.nextDouble();
DecimalFormat df = new DecimalFormat("#,###.###");
System.out.println("The area of the triangle is: " + df.format(huronFormula(a, b, c)) + " units squared");
timesUsed++;
}
}
public static double huronFormula(double a, double b, double c) {
double s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
}
GCF Calculator §
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// caleb b
// greatest common factor calculator
Scanner input = new Scanner(System.in);
int timesUsed = 0;
while (timesUsed < 3) {
System.out.println("Enter two numbers to find the greatest common factor of.");
int a = input.nextInt();
int b = input.nextInt();
System.out.println("The greatest common factor of " + a + " and " + b + " is " + GCF(a, b));
timesUsed++;
}
}
public static int GCF(int a, int b) {
int gcf = 1;
// math stuff
for (int i = 1; i <= a && i <= b; i++) {
// loop runs while the iterator is less than or equal to both numbers
if (a % i == 0 && b % i == 0) {
// if both numbers are divisible by the iterator, set the gcf to the iterator
gcf = i;
}
}
return gcf;
}
}
Fraction Simplifier Challenge §
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// caleb b
// fraction simplifier
Scanner input = new Scanner(System.in);
int timesUsed = 0;
while (timesUsed < 3) {
System.out.println("Enter a fraction to simplify (e.g 2/4): ");
String fraction = input.nextLine();
String[] fractionParts = fraction.split("/");
int numerator = Integer.parseInt(fractionParts[0]);
int denominator = Integer.parseInt(fractionParts[1]);
System.out.println(simplify(numerator, denominator));
timesUsed++;
}
}
public static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a%b);
}
public static String simplify(int numerator, int denominator) {
int gcd = gcd(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
// if the fraction is an improper fraction, convert to mixed number
if (numerator > denominator) {
int whole = numerator / denominator;
numerator %= denominator;
return whole + " " + numerator + "/" + denominator;
}
return numerator + "/" + denominator;
}
}