Skip to content

Instantly share code, notes, and snippets.

@Imorate
Created April 26, 2021 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Imorate/8d2b7a7209417ef863941c01e53c6f35 to your computer and use it in GitHub Desktop.
Save Imorate/8d2b7a7209417ef863941c01e53c6f35 to your computer and use it in GitHub Desktop.
Serial or parallel resistance calculation
import java.util.Scanner;
class ResistanceCalculation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int calculationType = 1;
System.out.println("Enter the type of calculation:");
System.out.println("1. Serial");
System.out.println("2. Parallel");
calculationType = scanner.nextInt();
System.out.println("Enter the N of resistances:");
int n = scanner.nextInt();
float[] resistances = new float[n];
for (int i = 0; i < n; i++) {
System.out.print("Data[" + (i + 1) + "]: ");
resistances[i] = scanner.nextFloat();
}
if (calculationType == 1) {
System.out.println("\nSerial calculation:");
System.out.println(serialCalculation(resistances));
} else {
System.out.println("\nParallel calculation:");
System.out.println(parallelCalculation(resistances));
}
}
public static float serialCalculation(float[] data) {
float sum = 0f;
for (float datum : data) {
sum += datum;
}
return sum;
}
public static float parallelCalculation(float[] data) {
float sum = 0f;
for (float datum : data) {
sum += 1f / datum;
}
return 1f / sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment