Skip to content
Related Articles

Related Articles

Program to calculate Resistance using given color code in circuits

Improve Article
Save Article
  • Last Updated : 22 Nov, 2022
Improve Article
Save Article

There are many different types of Resistor available which can be used in both electrical and electronic circuits. The resistance value, tolerance, and wattage rating are generally printed onto the body of the resistor as color bands. The task is to find the resistance of 4-band resistor and 5-band resistor.

Program to find the resistance of 4-Band Resistor

Given four strings A, B, C and D which denotes the color codes of the 4-band resistor. The task is to find the resistance, tolerance and wattage rating using the given color codes. Examples:

Input: A = “black”, B = “brown”, C = “red”, D = “green” Output: Resistance = 01 x 100 ohms +/- 0.5 % Input: A = “red”, B = “orange”, C = “yellow”, D = “green” Output: Resistance = 23 x 10k ohms +/- 0.5 %

Approach: The idea is to store the digits and multipliers as in the hash-map and then the resistance of the resistor can be computed. Below is the implementation of the above approach: 

C++




// C++ implementation to find the
// resistance of the resistor with
// the given color codes
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the resistance
// using color codes
void findResistance(string a, string b, string c, string d)
{
     
    // Hash-map to store the values
    // of the color-digits
    map<string, string> color_digit;
    color_digit["black"] = "0";
    color_digit["brown"] = "1";
    color_digit["red"] = "2";
    color_digit["orange"] = "3";
    color_digit["yellow"] = "4";
    color_digit["green"] = "5";
    color_digit["blue"] = "6";
    color_digit["violet"] = "7";
    color_digit["grey"] = "8";
    color_digit["white"] = "9";
     
    map<string, string> multiplier;
    multiplier["black"] = "1";
    multiplier["brown"] = "10";
    multiplier["red"] = "100";
    multiplier["orange"] = "1k";
    multiplier["yellow"] = "10k";
    multiplier["green"] = "100k";
    multiplier["blue"] = "1M";
    multiplier["violet"] = "10M";
    multiplier["grey"] = "100M";
    multiplier["white"] = "1G";
     
    map<string, string> tolerance;
     
    tolerance["brown"] =  "+/- 1 %";
    tolerance["red"] = "+/- 2 %";
    tolerance["green"] = "+/- 0.5 %";
    tolerance["blue"] = "+/- 0.25 %";
    tolerance["violet"] = "+/- 0.1 %";
    tolerance["gold"] = "+/- 5 %";
    tolerance["silver"] = "+/- 10 %";
    tolerance["none"] = "+/-20 %";
     
    if (color_digit.count(a)
       && color_digit.count(b)
       && multiplier.count(c)
       && tolerance.count(d))
       {
           string xx = color_digit[a];
           string yy = color_digit[b];
           string zz = multiplier;
           string aa = tolerance[d];
           cout << "Resistance = " << xx << yy <<
                 " x " << zz << " ohms " << aa << endl;
       }
    else
        cout << "Invalid Colors" << endl;
}
         
// Driver Code
int main()
{
    string a = "black";
    string b = "brown";
    string c = "red";
    string d = "green";
         
    // Function Call
    findResistance(a, b, c, d);
}
 
// This code is contributed by phasing17

Java




// Java implementation to find the
// resistance of the resistor with
// the given color codes
import java.util.*;
 
class GFG
{
 
  // Function to find the resistance
  // using color codes
  static void findResistance(String a, String b, String c, String d)
  {
 
    // Hash-map to store the values
    // of the color-digits
    HashMap<String, String> color_digit = new HashMap<String, String>() ;
    color_digit.put("black", "0");
    color_digit.put("brown", "1");
    color_digit.put("red", "2");
    color_digit.put("orange", "3");
    color_digit.put("yellow", "4");
    color_digit.put("green", "5");
    color_digit.put("blue", "6");
    color_digit.put("violet", "7");
    color_digit.put("grey", "8");
    color_digit.put("white", "9");
 
    HashMap<String, String> multiplier = new HashMap<String, String>() ;
    multiplier.put("black", "1");
    multiplier.put("brown", "10");
    multiplier.put("red", "100");
    multiplier.put("orange", "1k");
    multiplier.put("yellow", "10k");
    multiplier.put("green", "100k");
    multiplier.put("blue", "1M");
    multiplier.put("violet", "10M");
    multiplier.put("grey", "100M");
    multiplier.put("white", "1G");
 
    HashMap<String, String> tolerance = new HashMap<String, String>() ;
 
    tolerance.put("brown""+/- 1 %");
    tolerance.put("red", "+/- 2 %");
    tolerance.put("green", "+/- 0.5 %");
    tolerance.put("blue", "+/- 0.25 %");
    tolerance.put("violet", "+/- 0.1 %");
    tolerance.put("gold", "+/- 5 %");
    tolerance.put("silver", "+/- 10 %");
    tolerance.put("none", "+/-20 %");
 
    if (color_digit.containsKey(a)
        && color_digit.containsKey(b)
        && multiplier.containsKey(c)
        && tolerance.containsKey(d))
    {
      String xx = color_digit.get(a);
      String yy = color_digit.get(b);
      String zz = multiplier.get(c);
      String aa = tolerance.get(d);
      System.out.println("Resistance = " + xx + yy+
                         " x " + zz + " ohms " +  aa);
    }
    else
      System.out.println("Invalid Colors");
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    String a = "black";
    String b = "brown";
    String c = "red";
    String d = "green";
 
    // Function Call
    findResistance(a, b, c, d);
  }
}
 
// This code is contributed by phasing17

Python3




# Python implementation to find the
# resistance of the resistor with
# the given color codes
 
# Function to find the resistance
# using color codes
def findResistance(a, b, c, d):
     
    # Hash-map to store the values
    # of the color-digits
    color_digit = {'black': '0',
                   'brown': '1',
                   'red': '2',
                   'orange': '3',
                   'yellow': '4',
                   'green' : '5',
                   'blue' : '6',
                   'violet' : '7',
                   'grey' : '8',
                   'white': '9'}
     
    multiplier = {'black': '1',
                  'brown': '10',
                  'red': '100',
                  'orange': '1k',
                  'yellow': '10k',
                  'green' : '100k',
                  'blue' : '1M',
                  'violet' : '10M',
                  'grey' : '100M',
                  'white': '1G'}
     
    tolerance = {'brown': '+/- 1 %',
                  'red' : '+/- 2 %',
                 'green': "+/- 0.5 %",
                  'blue': '+/- 0.25 %',
                 'violet' : '+/- 0.1 %',
                  'gold': '+/- 5 %',
                 'silver' : '+/- 10 %',
                  'none': '+/-20 %'}
     
    if a in color_digit
       and b in color_digit\
       and c in multiplier
       and d in tolerance:
           xx = color_digit.get(a)
           yy = color_digit.get(b)
           zz = multiplier.get(c)
           aa = tolerance.get(d)
           print("Resistance = "+xx + yy+\
                 " x "+zz+" ohms "+aa)
    else:
        print("Invalid Colors")
         
# Driver Code
if __name__ == "__main__":
    a = "black"
    b = "brown"
    c = "red"
    d = "green"
     
    # Function Call
    findResistance(a, b, c, d)

C#




// C# implementation to find the
// resistance of the resistor with
// the given color codes
using System;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to find the resistance
  // using color codes
  static void findResistance(string a, string b, string c, string d)
  {
 
    // Hash-map to store the values
    // of the color-digits
    Dictionary<string, string> color_digit = new Dictionary<string, string>() ;
    color_digit["black"] = "0";
    color_digit["brown"] = "1";
    color_digit["red"] = "2";
    color_digit["orange"] = "3";
    color_digit["yellow"] = "4";
    color_digit["green"] = "5";
    color_digit["blue"] = "6";
    color_digit["violet"] = "7";
    color_digit["grey"] = "8";
    color_digit["white"] = "9";
 
    Dictionary<string, string> multiplier = new Dictionary<string, string>() ;
    multiplier["black"] = "1";
    multiplier["brown"] = "10";
    multiplier["red"] = "100";
    multiplier["orange"] = "1k";
    multiplier["yellow"] = "10k";
    multiplier["green"] = "100k";
    multiplier["blue"] = "1M";
    multiplier["violet"] = "10M";
    multiplier["grey"] = "100M";
    multiplier["white"] = "1G";
 
    Dictionary<string, string> tolerance = new Dictionary<string, string>() ;
 
    tolerance["brown"] =  "+/- 1 %";
    tolerance["red"] = "+/- 2 %";
    tolerance["green"] = "+/- 0.5 %";
    tolerance["blue"] = "+/- 0.25 %";
    tolerance["violet"] = "+/- 0.1 %";
    tolerance["gold"] = "+/- 5 %";
    tolerance["silver"] = "+/- 10 %";
    tolerance["none"] = "+/-20 %";
 
    if (color_digit.ContainsKey(a)
        && color_digit.ContainsKey(b)
        && multiplier.ContainsKey(c)
        && tolerance.ContainsKey(d))
    {
      string xx = color_digit[a];
      string yy = color_digit[b];
      string zz = multiplier;
      string aa = tolerance[d];
      Console.WriteLine("Resistance = " + xx + yy+
                        " x " + zz + " ohms " +  aa);
    }
    else
      Console.WriteLine("Invalid Colors");
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    string a = "black";
    string b = "brown";
    string c = "red";
    string d = "green";
 
    // Function Call
    findResistance(a, b, c, d);
  }
}
 
// This code is contributed by phasing17

Javascript




// JS implementation to find the
// resistance of the resistor with
// the given color codes
 
// Function to find the resistance
// using color codes
function findResistance(a, b, c, d)
{
     
    // Hash-map to store the values
    // of the color-digits
    let color_digit = {'black': '0',
                   'brown': '1',
                   'red': '2',
                   'orange': '3',
                   'yellow': '4',
                   'green' : '5',
                   'blue' : '6',
                   'violet' : '7',
                   'grey' : '8',
                   'white': '9'}
     
    let multiplier = {'black': '1',
                  'brown': '10',
                  'red': '100',
                  'orange': '1k',
                  'yellow': '10k',
                  'green' : '100k',
                  'blue' : '1M',
                  'violet' : '10M',
                  'grey' : '100M',
                  'white': '1G'}
     
    let tolerance = {'brown': '+/- 1 %',
                  'red' : '+/- 2 %',
                 'green': "+/- 0.5 %",
                  'blue': '+/- 0.25 %',
                 'violet' : '+/- 0.1 %',
                  'gold': '+/- 5 %',
                 'silver' : '+/- 10 %',
                  'none': '+/-20 %'}
     
    if (color_digit.hasOwnProperty(a)
       && color_digit.hasOwnProperty(b)
       && multiplier.hasOwnProperty(c)
       && tolerance.hasOwnProperty(d))
       {
           let xx = color_digit[a]
           let yy = color_digit[b]
           let zz = multiplier
           let aa = tolerance[d]
           console.log("Resistance = "+xx + yy+
                 " x "+zz+" ohms "+aa)
       }
    else
        console.log("Invalid Colors")
}
         
// Driver Code
let a = "black"
let b = "brown"
let c = "red"
let d = "green"
     
// Function Call
findResistance(a, b, c, d)
 
 
// This code is contributed by phasing17

Output:

Resistance = 01 x 100 ohms +/- 0.5 %

Time Complexity: O(1)

Auxiliary Space: O(1)

Program to find the resistance of 5-Band Resistor

Given five strings A, B, C, D and E which denotes the color codes of the 5-band resistor. The task is to find the resistance, tolerance and wattage rating using the given color codes. Examples:

Input: A = “black”, B = “brown”, C = “red”, D = “green”, E = “silver” Output: Resistance = 012 x 100k ohms +/- 10 % Input: A = “red”, B = “orange”, C = “yellow”, D = “green”, E = “gold” Output: Resistance = 234 x 100k ohms +/- 5 %

Approach: The idea is to store the digits and multipliers as in the hash-map and then the resistance of the resistor can be computed. Below is the implementation of the above approach: 

C++




// C++ implementation to find the
// resistance of the resistor with
// the given color codes
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the resistance
// using color codes
void findResistance(string a, string b, string c, string d,
                    string e)
{
 
  // Hash-map to store the values
  // of the color-digits
  map<string, string> color_digit;
  color_digit["black"] = "0";
  color_digit["brown"] = "1";
  color_digit["red"] = "2";
  color_digit["orange"] = "3";
  color_digit["yellow"] = "4";
  color_digit["green"] = "5";
  color_digit["blue"] = "6";
  color_digit["violet"] = "7";
  color_digit["grey"] = "8";
  color_digit["white"] = "9";
 
  map<string, string> multiplier;
  multiplier["black"] = "1";
  multiplier["brown"] = "10";
  multiplier["red"] = "100";
  multiplier["orange"] = "1k";
  multiplier["yellow"] = "10k";
  multiplier["green"] = "100k";
  multiplier["blue"] = "1M";
  multiplier["violet"] = "10M";
  multiplier["grey"] = "100M";
  multiplier["white"] = "1G";
 
  map<string, string> tolerance;
 
  tolerance["brown"] = "+/- 1 %";
  tolerance["red"] = "+/- 2 %";
  tolerance["green"] = "+/- 0.5 %";
  tolerance["blue"] = "+/- 0.25 %";
  tolerance["violet"] = "+/- 0.1 %";
  tolerance["gold"] = "+/- 5 %";
  tolerance["silver"] = "+/- 10 %";
  tolerance["none"] = "+/-20 %";
 
  if (color_digit.count(a) && color_digit.count(b)
      && color_digit.count(c) && multiplier.count(d)
      && tolerance.count(e)) {
    string xx = color_digit[a];
    string yy = color_digit[b];
    string zz = color_digit;
    string aa = multiplier;
    string bb = tolerance[d];
    cout << "Resistance = " << xx << yy << zz << " x "
      << aa << " ohms " << bb << endl;
  }
  else
    cout << "Invalid Colors" << endl;
}
 
// Driver Code
int main()
{
  string a = "red";
  string b = "orange";
  string c = "yellow";
  string d = "green";
  string e = "gold";
 
  // Function Call
  findResistance(a, b, c, d, e);
}
 
// This code is contributed by phasing17

Java




// Java implementation to find the
// resistance of the resistor with
// the given color codes
import java.util.*;
 
class GFG {
 
  // Function to find the resistance
  // using color codes
  static void findResistance(String a, String b, String c,
                             String d, String e)
  {
 
    // Hash-map to store the values
    // of the color-digits
    HashMap<String, String> color_digit
      = new HashMap<String, String>();
    color_digit.put("black", "0");
    color_digit.put("brown", "1");
    color_digit.put("red", "2");
    color_digit.put("orange", "3");
    color_digit.put("yellow", "4");
    color_digit.put("green", "5");
    color_digit.put("blue", "6");
    color_digit.put("violet", "7");
    color_digit.put("grey", "8");
    color_digit.put("white", "9");
 
    HashMap<String, String> multiplier
      = new HashMap<String, String>();
    multiplier.put("black", "1");
    multiplier.put("brown", "10");
    multiplier.put("red", "100");
    multiplier.put("orange", "1k");
    multiplier.put("yellow", "10k");
    multiplier.put("green", "100k");
    multiplier.put("blue", "1M");
    multiplier.put("violet", "10M");
    multiplier.put("grey", "100M");
    multiplier.put("white", "1G");
 
    HashMap<String, String> tolerance
      = new HashMap<String, String>();
 
    tolerance.put("brown", "+/- 1 %");
    tolerance.put("red", "+/- 2 %");
    tolerance.put("green", "+/- 0.5 %");
    tolerance.put("blue", "+/- 0.25 %");
    tolerance.put("violet", "+/- 0.1 %");
    tolerance.put("gold", "+/- 5 %");
    tolerance.put("silver", "+/- 10 %");
    tolerance.put("none", "+/-20 %");
 
    if (color_digit.containsKey(a)
        && color_digit.containsKey(b)
        && color_digit.containsKey(c)
        && multiplier.containsKey(d)
        && tolerance.containsKey(e)) {
      String xx = color_digit.get(a);
      String yy = color_digit.get(b);
      String zz = color_digit.get(c);
      String aa = multiplier.get(d);
      String bb = tolerance.get(e);
      System.out.println("Resistance = " + xx + yy
                         + zz + " x " + aa + " ohms "
                         + bb);
    }
    else
      System.out.println("Invalid Colors");
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    String a = "red";
    String b = "orange";
    String c = "yellow";
    String d = "green";
    String e = "gold";
 
    // Function Call
    findResistance(a, b, c, d, e);
  }
}
 
// This code is contributed by phasing17

Python3




# Python implementation to find the
# resistance of the resistor with
# the given color codes
 
# Function to find the resistance
# using color codes
def findResistance(a, b, c, d, e):
     
    # Hash-map to store the values
    # of the color-digits
    color_digit = {'black': '0',
                   'brown': '1',
                   'red': '2',
                   'orange': '3',
                   'yellow': '4',
                   'green' : '5',
                   'blue' : '6',
                   'violet' : '7',
                   'grey' : '8',
                   'white': '9'}
     
    multiplier = {'black': '1',
                  'brown': '10',
                  'red': '100',
                  'orange': '1k',
                  'yellow': '10k',
                  'green' : '100k',
                  'blue' : '1M',
                  'violet' : '10M',
                  'grey' : '100M',
                  'white': '1G'}
     
    tolerance = {'brown': '+/- 1 %',
                  'red' : '+/- 2 %',
                 'green': "+/- 0.5 %",
                  'blue': '+/- 0.25 %',
                 'violet' : '+/- 0.1 %',
                  'gold': '+/- 5 %',
                 'silver' : '+/- 10 %',
                  'none': '+/-20 %'}
     
    if a in color_digit
       and b in color_digit\
       and c in color_digit
       and d in multiplier\
       and e in  tolerance:
           xx = color_digit.get(a)
           yy = color_digit.get(b)
           zz = color_digit.get(c)
           aa = multiplier.get(d)
           bb = tolerance.get(e)
           print("Resistance = "+xx + yy\
               + zz+" x "+aa+" ohms "+bb)
    else:
        print("Invalid Colors")
         
# Driver Code
if __name__ == "__main__":
    a = "red"
    b = "orange"
    c = "yellow"
    d = "green"
    e = "gold"
     
    # Function Call
    findResistance(a, b, c, d, e)

C#




// C# implementation to find the
// resistance of the resistor with
// the given color codes
using System;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to find the resistance
  // using color codes
  static void findResistance(string a, string b, string c,
                             string d, string e)
  {
 
    // Hash-map to store the values
    // of the color-digits
    Dictionary<string, string> color_digit
      = new Dictionary<string, string>();
    color_digit["black"] = "0";
    color_digit["brown"] = "1";
    color_digit["red"] = "2";
    color_digit["orange"] = "3";
    color_digit["yellow"] = "4";
    color_digit["green"] = "5";
    color_digit["blue"] = "6";
    color_digit["violet"] = "7";
    color_digit["grey"] = "8";
    color_digit["white"] = "9";
 
    Dictionary<string, string> multiplier
      = new Dictionary<string, string>();
    multiplier["black"] = "1";
    multiplier["brown"] = "10";
    multiplier["red"] = "100";
    multiplier["orange"] = "1k";
    multiplier["yellow"] = "10k";
    multiplier["green"] = "100k";
    multiplier["blue"] = "1M";
    multiplier["violet"] = "10M";
    multiplier["grey"] = "100M";
    multiplier["white"] = "1G";
 
    Dictionary<string, string> tolerance
      = new Dictionary<string, string>();
 
    tolerance["brown"] = "+/- 1 %";
    tolerance["red"] = "+/- 2 %";
    tolerance["green"] = "+/- 0.5 %";
    tolerance["blue"] = "+/- 0.25 %";
    tolerance["violet"] = "+/- 0.1 %";
    tolerance["gold"] = "+/- 5 %";
    tolerance["silver"] = "+/- 10 %";
    tolerance["none"] = "+/-20 %";
 
    if (color_digit.ContainsKey(a)
        && color_digit.ContainsKey(b)
        && color_digit.ContainsKey(c)
        && multiplier.ContainsKey(d)
        && tolerance.ContainsKey(e)) {
      string xx = color_digit[a];
      string yy = color_digit[b];
      string zz = color_digit;
      string aa = multiplier;
      string bb = tolerance[d];
      Console.WriteLine("Resistance = " + xx + yy + zz
                        + " x " + aa + " ohms " + bb);
    }
    else
      Console.WriteLine("Invalid Colors");
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    string a = "red";
    string b = "orange";
    string c = "yellow";
    string d = "green";
    string e = "gold";
 
    // Function Call
    findResistance(a, b, c, d, e);
  }
}
 
// This code is contributed by phasing17

Javascript




// JS implementation to find the
// resistance of the resistor with
// the given color codes
 
// Function to find the resistance
// using color codes
function findResistance(a, b, c, d, e)
{
    // Hash-map to store the values
    // of the color-digits
    let color_digit = {'black': '0',
                   'brown': '1',
                   'red': '2',
                   'orange': '3',
                   'yellow': '4',
                   'green' : '5',
                   'blue' : '6',
                   'violet' : '7',
                   'grey' : '8',
                   'white': '9'}
     
    let multiplier = {'black': '1',
                  'brown': '10',
                  'red': '100',
                  'orange': '1k',
                  'yellow': '10k',
                  'green' : '100k',
                  'blue' : '1M',
                  'violet' : '10M',
                  'grey' : '100M',
                  'white': '1G'}
     
    let tolerance = {'brown': '+/- 1 %',
                  'red' : '+/- 2 %',
                 'green': "+/- 0.5 %",
                  'blue': '+/- 0.25 %',
                 'violet' : '+/- 0.1 %',
                  'gold': '+/- 5 %',
                 'silver' : '+/- 10 %',
                  'none': '+/-20 %'}
     
     if (color_digit.hasOwnProperty(a)
       && color_digit.hasOwnProperty(b)
       && color_digit.hasOwnProperty(c)
       && multiplier.hasOwnProperty(d)
       && tolerance.hasOwnProperty(e))
       {
           let xx = color_digit[a]
           let yy = color_digit[b]
           let zz = color_digit
           let aa = multiplier[d]
           let bb = tolerance[e]
          
           console.log("Resistance = "+xx + yy
               + zz+" x "+aa+" ohms "+bb)
       }
           
    else
        console.log("Invalid Colors")
}
         
// Driver Code
let a = "red"
let b = "orange"
let c = "yellow"
let d = "green"
let e = "gold"
     
// Function Call
findResistance(a, b, c, d, e)
 
 
 
// This code is contributed by phasing17

Output:

Resistance = 234 x 100k ohms +/- 5 %

Time Complexity: O(1)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!