Please help: Getting specific quantities of Resistors!

So an assignment I am doing, I need to ask the user what their target resistance in a series is. I did that. There are 3 types of Resistors (15, 5, and 1 Ohm).
If you type in let's say 41 Ohms, you would need 2 15's, 2 5's and 1 1 Ohm resistor. How would I go about getting the certain amount of ____ Ohm Resistors?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  P#include <stdio.h>

int main(){
	const int resistor1 = 1;
	const int resistor2 = 5;
	const int resistor3 = 15;
	
	int num1;
	int num2;
	int num3;
	int targetResistance;
	int numResistors;
	
printf("What is the target resistance?\n");
scanf("%d ", targetResistance);
	

	int num3Resistors = targetResistance / 15;
	int num2Resistors = targetResistance / 5;
	int num1Resistors = targetResistance / 1;
	
printf("You need a minimum of %d resistors.\n", numResistors);	
printf("%d x 15 ohms\n", num3Resistors);
printf("%d x 5 ohms\n", num2Resistors);
printf("%d x 1 ohm\n", num1resistors);
	
}
Last edited on
Mathematically, I would think along these lines.

41 Ohms -

subtract 15 as many times as you can. (two)
41-15-15=11
then take the next biggest resistor(5) and subtract that as many times as you can.
11-5-5=1
Then the last one...
1-1 =0
You have two 15's, two 5's, and one 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
int r1,r2,r3=0;
target = 41;
while (target>resistor3){
target=-resistor3;; r3++;
}
while (target>resistor2){
target=-resistor2;; r2++;
}
while (target>resistor1){
target=-resistor1;; r1++;
}

You need r3 resistor 3's, r2  resistor 2's, r1  resistor 1's 


something along that lines.
@pearlyman

Thank you so much for the reply. However, I have a question!
What is the =- you have in your equation? IIRC, it's the same as target = target-resistor?
This is actually really good. I'm surprised I didn't think of this to be honest. Thank you so much!
Topic archived. No new replies allowed.