the doctor can often tell based on your symptoms. with bacterial pneumonia the symptoms often come on very suddenly and you feel quite sick often with a high fever. with viral pneumonia (also called nonbacterial pneumonia) symptoms generally come on more slowly. you have a cough but typically don't cough anything up (a dry cough). sometimes the symptoms with viral pneumonia may be so mild that the person may not even feel that sick. if you are coughing up mucus the doctor can send the mucus to the lab for a culture. a culture will tell if it's a bacteria virus or something else. here's more info on pneumonia symptoms and the difference between bacterial and viral pneumonia.
how does the doctor know whether someone has bacterial pneumonia versus viral pneumonia?