if start and end are variables to store the values then
1) You could just start the limits with 0 to end-start and value passed value - start 2) Calculate Percentage 3) Return 100-percentage
public static void main(String[] args) {
System.out.println(find_percent(100,300,100)+"%");
System.out.println(find_percent(100,300,300)+"%");
System.out.println(find_percent(100,300,200)+"%");
System.out.println(find_percent(100,300,150)+"%");
System.out.println(find_percent(100,300,250)+"%");
System.out.println("");
System.out.println(find_percent(20,40,20)+"%");
System.out.println(find_percent(20,40,40)+"%");
System.out.println(find_percent(20,40,25)+"%");
System.out.println(find_percent(20,40,35)+"%");
}
public static double find_percent(double start,double end,double val){
end = end- start;
val = val - start;
start = 0;
return((1-(val/end))*100);
}
Output:
100.0%
0.0%
50.0%
75.0%
25.0%
100.0%
0.0%
75.0%
25.0%