%calculates the molecular weight of an molecule with C,N,H,O as elements V = input('enter your molecule -it can only have C,N,H,0') n = length(V); k=1; mol = 0; disp('num will be the various numbers in the formula for the molecule') while(k <= n) %look for the location of the next letter % the chars in between we convert into a number j = 1; while ( ((k+j)<= n)& isletter(V(k+j))==0) j=j+1; end; if (j == 1) num = 1; else num = str2num(V(k+1:k+j-1)) end; %if if(V(k) == 'C') %determine weight of an element wt = 12; elseif (V(k) == 'N') wt = 14; elseif (V(k) == 'O') wt = 16; elseif (V(k) == 'H') wt = 1; end; mol=mol + num*wt;%add weight to running sum of weights k = k+j; end; mol