Density is inversely related with volume and directly related with mass
Density = Mass/volume
When the number of carbon atom increases the molar mass of the substance increases. So there will be increase in density.
The second reason is increase in attractive forces due to which the molecules are closely packed and volume is reducing. The attractive forces between hydrocarbons are Vander Walls Forces which is nothing but gravitational forces. These forces are directly related with mass of the molecules. So the attractive force between higher hydrocarbon is more. (This is why the state of hydrocarbons are changing from gas --> liquid --> solid on increasing the number of carbon atoms). Due to more attractive force the molecules come closer and volume becomes less. So the density increases.