#include<stdio.h> #include <conio.h> #include <math.h> void resistance(); void h
ID: 3614317 • Letter: #
Question
#include<stdio.h>
#include <conio.h>
#include <math.h>
void resistance();
void help();
void table();
int main()
{int i,code=5;
while(code>=1)
{printf("Select a function ");
printf("1 Calculate resistance value ");
printf("2 Help ");
printf("3 exit ");
scanf("%d",&code);
switch(code)
{
case 1: resistance();
break;
case 2: help();
break;
case 3:
return 0;
default: printf("invalidinput-retry ");
break;
}
}
return 0;
}
void help()
{printf("A resistor is a circuit device designed to have ");
printf("a specific resistance value between its ends ");
printf("(see http://en.wikipedia.org/wiki/Resistors for more ");
printf("details on resistors). Resistance values are expressed in ");
printf("ohms (?) or kilo-ohms (k ?). Resistors are frequentlymarked ");
printf("with four colored bands that encode their resistancevalues, ");
printf("The first two bands encode the first two significant digitsof ");
printf("the resistance value, the third is a power-of-tenmultiplier or ");
printf("number-of-zeroes, and the fourth is the tolerance accuracy, ");
printf("or acceptable error, of the value. ");
table();
}
void table()
{printf("Color 1st band 2ndband 3rd band 4thband ");
printf(" (multiplier) (tolerance) ");
printf("Black 0 0 100 ");
printf("Brown 1 1 101 ");
printf("Red 2 2 102 ");
printf("Orange 3 3 103 ");
printf("Yellow 4 4 104 ");
printf("Green 5 5 105 ");
printf("Blue 6 6 106 ");
printf("Violet 7 7 107 ");
printf("Gray 8 8 108 ");
printf("White 9 9 109 ");
printf("Gold 10-1 +-5% ");
printf("Silver 10-2 +-10 ");
}
void resistance()
{int i,code;
double value,exp,tol=0;
char pm;
table();
for(i=1;i<5;i++)
{do
{printf("Please select the color for band %d:",i);
scanf("%d",&code);
if(code<0||code>11||(i==4&&code<10))
printf("Invalid entry ");
}while(code<0||code>11||(i==4&&code<10));
exp=code;
switch(i)
{case1: value=code*10; break;
case 2: value+=code; break;
case 3:if(code>9)
exp=9-code;
value*=pow(10,exp); break;
case 4: if(code==10)
tol=5/100.;
else
tol=10/100.;
value*=pow(10,9-code);
value*=tol;
tol=value*tol;
}
}
printf("The resistance valueis: %.0f+-%.0f k ",value,tol);
}