Hi sneha,
There are two separate speakers in most phones; the earpiece speaker that's used when you hold the phone up to your head, and the lower speaker that is used for most sounds when you're not on a phone call. Putting the phone on loudspeaker switches which speaker is being used from the earpiece speaker to the lower speaker.
So it sounds like your earpiece speaker is having problems, but the lower speaker is fine.
The first thing to do is investigate software solutions, such as checking settings, clearing cache, restart the phone, on up through possibly restoring the phone to factory settings (after backing it up, of course).
After those avenues have been exhausted, you may have to open up the phone and replace the speaker.
Keep us updated on what you find.