Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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