In: Computer Science
construct a c++ program that calculate/print the molecular weight of the below five amino acids
Oxygen: 15.9994
Carbon: 12.011
Nitrogen: 14.00674
Sulfur: 32.066
Hydrogen: 1.00794
The program should prompt the user to enter the number of each one of the atoms and then print/compute the molecular weight.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#include<iostream>
using namespace std;
//declaring constants for storing atomic masses of all 5 elements
const double ATOMIC_MASS_OXYGEN=15.9994;
const double ATOMIC_MASS_CARBON=12.011;
const double ATOMIC_MASS_NITROGEN=14.00674;
const double ATOMIC_MASS_SULFUR=32.066;
const double ATOMIC_MASS_HYDROGEN=1.00794;
int main(){
//declaring variables
int oxygen, carbon, nitrogen, sulfur, hydrogen;
//asking and reading number of atoms of each element
cout<<"Enter the number of Oxygen atoms: ";
cin>>oxygen;
cout<<"Enter the number of Carbon atoms: ";
cin>>carbon;
cout<<"Enter the number of Nitrogen atoms: ";
cin>>nitrogen;
cout<<"Enter the number of Sulfur atoms: ";
cin>>sulfur;
cout<<"Enter the number of Hydrogen atoms: ";
cin>>hydrogen;
//finding molecular weight by summing the product of number of atoms and atomic mass
//of each element
double molecular_weight=oxygen*ATOMIC_MASS_OXYGEN+
carbon*ATOMIC_MASS_CARBON+
nitrogen*ATOMIC_MASS_NITROGEN+
sulfur*ATOMIC_MASS_SULFUR+
hydrogen*ATOMIC_MASS_HYDROGEN;
//displaying the result
cout<<"Molecular weight of the compound: "<<molecular_weight<<endl;
return 0;
}
/*OUTPUT*/
Enter the number of Oxygen atoms: 1
Enter the number of Carbon atoms: 1
Enter the number of Nitrogen atoms: 1
Enter the number of Sulfur atoms: 0
Enter the number of Hydrogen atoms: 3
Molecular weight of the compound: 45.041