A submarine is able to float and sink by controlling its buoyancy, which is the upward force that opposes the force of gravity. This is achieved through the use of ballast tanks, which are compartments that can be filled with water or air as needed to adjust the submarine's density.

Here is a more detailed explanation of how a submarine floats and sinks, along with the principles that make it possible:

Buoyancy: Buoyancy is the upward force exerted on an object when it is submerged in a fluid. The buoyant force is equal to the weight of the fluid displaced by the object. For example, if a submarine displaces 1000 kilograms of water, the buoyant force on the submarine will be 1000 kilograms.

Archimedes' principle: This principle states that the buoyant force on an object is equal to the weight of the fluid displaced by the object. In other words, if a submarine displaces 1000 kilograms of water, the buoyant force on the submarine will be equal to the weight of 1000 kilograms of water, which is equal to the force of gravity acting on the submarine.

Ballast tanks: Submarines are equipped with ballast tanks that can be filled with water or air as needed to adjust the submarine's buoyancy. When the ballast tanks are filled with water, the submarine becomes denser and sinks. When the tanks are emptied, the submarine becomes less dense and rises to the surface.

Dive planes: In addition to using ballast tanks to control buoyancy, submarines also have dive planes that can be adjusted to control the angle of descent or ascent. By tilting the dive planes up or down, the submarine can change its angle of attack and move up or down in the water column.