The transverse momentum spectra (pT) of heavy quarks in proton-proton collisions at RHIC and LHC energies is calculated using perturbative quantum chromodynamics (QCD) . For the fragmentation of heavy quarks to heavy mesons, Peterson fragmentation function is used. The heavy quarks loose energy in the hot partonic medium which is reflected in the transverse momentum spectra of heavy mesons. The collisional energy loss of heavy quarks has been calculated using QCD calculations. The radiative energy loss is obtained using two models namely reaction operator formalism and generalized dead cone approach. The nuclear modification factors RAA as a function of transverse momentum by including shadowing and energy loss are calculated for heavy flavor electron in AuAu collisions at √sNN = 200 GeV, for D0 mesons in PbPb collisions at √sNN = 2.76 TeV and for D0 and B+ mesons at √sNN = 5.02 TeV and are compared with the recent measurements. The radiative energy loss calculated by reaction operator formalism added with collisional energy loss describes the heavy flavor electron suppression in high pT range. The radiative energy loss from generalized dead cone approach alone is sufficient to produce measured D0meson RAA at both the LHC energies. The radiative energy loss from reaction operator formalism plus collisional energy loss gives good description of the D0 meson RAA. The radiative energy loss from generalized dead cone approach plus collisional energy loss gives good description of the B+ meson RAA. The radiative process is dominant for charm quark while for the bottom quark, both radiative process and the elastic collisions are important.