How did you ensure the 24V output of the rotary encoder doesn't destroy the Arduino's inputs?
I have not given 24v to Arduino pin.
Serial.print((300*60)/i); //speed r/min (60*1000000/(45*6*2*i))
The output voltage I didn't check
Means rpm is = (60*1000)/number of pulses per second.