Prolonged sitting is a frequent cause of back and neck pain. While extended periods of sitting are best avoided, for many, it's a fact of life.
When sitting, it's important to keep the back straight, knees bent, and head centered over the shoulders. Slouching forward may be comfortable and allow the spinal muscles to relax but gradually overstretches spinal ligaments, leading to back and neck pain among other problems. I always encourage patients to maintain a "neutral spine" position at all time is ideal.