Lens on my Nikon D40 is locked up and there's something loose inside it?

I turned my Nikon D40 on and tried to use the zoom but it wouldn't move. I took the lens off the camera and turns out there's a loose cylinder part in the centre which has come off. Zoom won't move at all! It was working perfectly this morning and now it's become like this.
Can I repair this myself? Otherwise how much would it cost me to repair this or to buy a new lens?

A new 18-55 will cost less than a $100.It will be cheaper than getting this fixed. Put the old one on ebay "for parts" and you can make $20 on it

Depends on the lens.
The D40 was the first camera to use the 18-55mm kit lens. And if were the original purchaser, and bought the camera in the US from a legitimate US dealer, you should have received a 5yr warranty card.
Even then, you will probably be close to the end of the 5yr warranty anyway.
You can get a refurbished 18-55mm lens from the Nikon Outlet store for about $160, or for $99 at Adorama. That is going to probably be less than the cost of repair. While the lens usually has a 90 day warranty, I think Adorama offers a 1yr store warranty on many of their refurbished items.
Even if you repair the lens yourself, you have to access it from the front. This requires a specialized spanner wrench or "lens donut" to get the lens apart. These tools will be about the same cost as the $99 refurbished lens from Adorama.
You can also buy a new 18-55 for about $120 or with Vibration Reduction for $199.
This might also be the opportunity to upgrade the lens by buying a Nikon 50mm f/1.8 ($110 new), or 18-200mm for anywhere from $625 refurbished VR I to $900 for a new VR II version.
The 18-200 lenses have a better build than the 18-55 (metal mounting ring, etc).

No you can't repair it yourself. If you're mechanically minded, after you get your new lens, you can tear this one apart just to see how it's made - to me that would be interesting. But repair, don't bother because it sounds like something has broken inside.
- Is Facebook something to have? And is? Likes on Facebook Something To Care About?
- Dust inside Nikon Dslr View finder? Looks like gone inside of the prism?
- Shutter button/release on Nikon D40 seems loose?
- My Nikon 18-55mm Lense has locked up, and I can't zoom?
- Nikon D2X locked and not changing settings?