Hello, this is my homework:
Write a program that prompts the user for an observed resistivity of an unknown material (as nano-ohm metres) and identies the substance if the observed resistivity is within
3% of the known resistivity of any of the materials in Table
1. If the input value lies outside the 3% range, output Unknown substance.
Table 1
Copper 16.78.
Aluminum 26.50
Beryllium 35.6
Potassium 72.0
Iron 96.10
And here is my code:
Code:/* Duy Nguyen CSE155h ID:02104267*/ #include <stdio.h> #include <stdlib.h> #include <math.h> void main(int argc, char *argv[]){ double res; printf("Enter the observed resistivity:\n"); scanf("%lf", &res); // Identify if (res <= 16.78*0.03+16.78 && res >= 16.78*0.03-16.78) printf("The material is Copper\n"); else if (res <= 26.5*0.03+26.5 && res >= 26.5*0.03-26.5) printf("The material is Aluminium\n"); else if (res <= 35.6*0.03+35.6 && res >= 35.6*0.03-35.6) printf("The material is Beryllium\n"); else if (res <= 72.0*0.03+72.0 && res >= 72.0*0.03-72.0) printf("The material is Potassium\n"); else if (res <= 96.1*0.03+96.1 && res >= 96.1*0.03-96.1) printf("The material is Iron\n"); else printf("Unknown substance\n"); return 0; }
Most of the time, it works fine. However, there is a problem when I enter the value lower than the resistivity of a certain substance. It'll just output that substance's name. For example if I enter 20. It'll will say that the substance is Aluminium(which is not correct). If I enter 100 It'll say unknown, but if I enter 1 I'll say Copper.. Help