LM358 OPAMP non inverting amplifier DC errors
Selected Resistance as R1 2.2K and feedback resistance, R2 51K.

The input offset voltage of the LM358 is typically 2 mV but can be as high as 5 mV and if you ignore the input signal (set it to 0 mV as a thought experiment), the output voltage is: -
$$\text{±2 mV}\cdot\left(1 + \dfrac{\text{51 kohm}}{\text{2.2 kohm}}\right) = \text{±48 mV}$$
So that's a pretty big basic error given that a 40 mV input signal is meant to be amplified to 967 mV (a ±5% error). That is for a typical input offset voltage of 2 mV and, of course will be 2.5 times worse at extremes.

Measured output voltage by changing resistance values R1 to 1K and R2
to upto 1M

Now the gain is about a 1000 and the input offset of ±2 mV could produce an output all on its own of ±2 volts. So at one extreme, the output could be 2 volt low and at the other extreme, the output could be 2 volts high. Somewhere in between these numbers is where the output offset voltage could be.
If your LM358 is powered from 5 volts and 0 volt then clearly it cannot produce a negative output voltage and so the output may &quot;clamp&quot; close to 0 volts.
Do you see the problem: -
$$\color{red}{\boxed{\text{You need an op-amp with a much better input offset voltage}}}$$
There may be other problems too but this one is glaring.