Introduction

We have to write a C++ program to find the color coding of 5-band carbon resistors. Carbon resistors have bands with distinct colors on them. These bands determine the value of resistance of the resistor. We can calculate the resistance value by looking at these colors. However, suppose you are making a circuit in a lab and you have a pile of unarranged resistors in front of you, you need to pick up the right resistor but you have not got enough time to measure all of the resistances using a Digital Multimeter. Now, you either recall the color coding of these resistors from your memory and then recall the formula to calculate the resistance from the coding and finally perform calculations, or simply run this program in C++ Shell, enter the resistance value you require, and get the correct color arrangement for the required resistor. This program takes the resistance value as input and tells you the sequence of colors so that you can easily find the resistor. The following table shows the values assigned to different colors:



The first, second, and third digits in the resistance value correspond to the first, second, and third color bands respectively. The fourth digit corresponds to the digit in the multiple that is raised to the power of 10. The fifth band represents the tolerance that a resistor offers, Gold corresponds to a tolerance of ±5%, whereas silver refers to a tolerance of ±10%.

Algorithm 

➢ First, we take the value of resistance from the user.

➢ Then we separate each digit of this value and store each digit in the array.

➢ Then we use the first three digits and apply the switch case statement to find the color of the first three bands, we run the for loop for three iterations with the switch case inside it to give us three colors.

➢ Now, for the fourth color band we find the power of 10 which is a multiple of resistance value.

➢ Again, we use the switch case to find the color of the fourth band.

➢ At last, we simply print gold or silver color for the 5th band because the value of tolerance is predetermined by the manufacturer.

Code

#include <iostream> 
using namespace std; 
int main() 
 int res,tempres,count=0,num=0;
 int V[15]; //first we take integer input value in ohms 
 cout<<"Enter the value of resistance in ohms: ";
 cin>>res; //count the number of digits in the value 
 tempres=res; 

 while (tempres!=0) 
 { 
 tempres=tempres/10;
 ++count;
 } 
 //separate each digit of the resistance value and store it in an array 

 int i=0; 
 while (res!=0)
 {
 V[i]=res%10;
 res=res/10;
 ++i;
 } 
 cout<<endl;
//color using for loop and switch case to find the color coding for the first three bands. 

cout<<"\n***Colour Coding of 5 band Carbon Resistor from left to right.***\n";
 
for (int i=count-1;i>count-4;--i) 
 { 
    switch(V[i])
 case 0:
 cout<<"\nblack\n"; break; 

 case 1: 
 cout<<"\nbrown\n"; break; 

 case 2: 
 cout<<"\nred\n"; break; 

 case 3: 
 cout<<"\norange\n"; break; 

 case 4:
 cout<<"\nyellow\n"; break; 

 case 5: 
 cout<<"\ngreen\n"; break; 

 case 6:
 cout<<"\nblue\n"; break; 

 case 7:
 cout<<"\nviolet\n"; break; 

 case 8:
 cout<<"\ngrey\n"; break; 

 case 9: 
 cout<<"\nwhite\n"; break; 
 }
 //to find color of the 4th band we find the power of 10 which is a multiple of resistance value. 
 
for (int i=count-4;i>=0;--i) 
 { 
 ++num; 
 } 
switch(num)
 case 0:
 cout<<"\nblack\n"; break; 

 case 1: 
 cout<<"\nbrown\n"; break; 

 case 2: 
 cout<<"\nred\n"; break; 

 case 3: 
 cout<<"\norange\n"; break; 

 case 4:
 cout<<"\nyellow\n"; break; 

 case 5: 
 cout<<"\ngreen\n"; break; 

 case 6:
 cout<<"\nblue\n"; break; 

 case 7:
 cout<<"\nviolet\n"; break; 

 case 8:
 cout<<"\ngrey\n"; break; 

 case 9: 
 cout<<"\nwhite\n"; break; 
 }
//the fifth band is either gold or silver depending on the tolerance value 
 cout<<"\nGold/Silver\n"; 
 } 


Output

The output for a resistor of 120k ohms is given below.


Comments