fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4.  
  5. int n, choice, i;
  6. char sel;
  7.  
  8.  
  9. printf("This is a program that calculates resistance.\n");
  10.  
  11. do{
  12. printf("Enter 1 for parallel or 2 for series: ");
  13. scanf("%d", &choice);
  14.  
  15. printf("How many resistors are there: ");
  16. scanf("%d", &n);
  17. printf("\n");
  18.  
  19. double r[n];
  20.  
  21. for(i=0; i<n; i++) {
  22. printf("Resistor %d: ", i+1);
  23. scanf("%lf", &r[i]);
  24. }
  25. float res_tot = 0, inv_res_tot = 0;
  26. if(choice==1) {
  27. for(i=0; i<n; i++){
  28. inv_res_tot+=(1/r[i]);
  29. }
  30.  
  31. res_tot=(1/inv_res_tot);
  32. }
  33.  
  34. else if(choice==2) {
  35. for(i=0; i<n; i++) {
  36. res_tot+=r[i];
  37. }
  38. }
  39.  
  40. printf("\nThe total resistance is: %f Ohms\n", res_tot);
  41.  
  42. printf("\nWould you like to calculate another(y/n): ");
  43. scanf("%c", &sel);
  44.  
  45. } while(sel=='Y' || sel=='y');
  46.  
  47.  
  48.  
  49. return(0);
  50. }
Success #stdin #stdout 0s 2116KB
stdin
1
3
17.1
18.9
19.3
y
2
3
14.5
18.9
22.7
n

stdout
This is a program that calculates resistance.
Enter 1 for parallel or 2 for series: How many resistors are there: 
Resistor 1: Resistor 2: Resistor 3: 
The total resistance is: 6.127336 Ohms

Would you like to calculate another(y/n): Enter 1 for parallel or 2 for series: How many resistors are there: 
Resistor 1: Resistor 2: Resistor 3: 
The total resistance is: 56.100002 Ohms

Would you like to calculate another(y/n):