ANSWER: The taking of a census, by David or anyone else, is not a sin of itself. The reason for doing it, however, certainly can be and is something that might be worthy of being punished for! The book of Chronicles tells us that Satan the devil, who was against ancient Israel, moved or provoked King David to count his troops (1Chronicles 21:1).
Satan moved in spiritual warfare against the whole nation of Israel and not just the king. With his cunning ways, the devil set out to entice David to sin by numbering his army through a census, which is exactly what he did (2Samuel 24:1)!
The KJV Bible translation of 2Samuel 24:1, unfortunately, tends to give the reader the wrong impression regarding the motivation behind the census. Young's Literal Translation offers a more accurate explanation of what transpired. Samuel states, "And the anger of Jehovah addeth to burn against Israel, and an adversary [Satan] moveth David about them, saying, 'Go, number Israel and Judah'" (2Samuel 24:1).
It was Satan the devil who moved David to disobey God. The king seemed to have been prompted by a feeling of pride and ambitious curiosity. The devil tempted, but the king went along with it and wanted to use the census as evidence to trust in his own power not God's. It was his vanity and the desire to exalt himself in his own mind that got David in trouble.
Looking at the scriptures we can know that there is an evil intent by their content. Anytime Satan is involved you can be sure he intends to get someone to sin! He put the thought in David's mind that if he knew the number of young men under his rule (meaning those fit for war) he could brag or boast how great a king he was by the size of his army! Taking a census was a common way to guage a nation's potential strength in battle (Numbers 1:1 - 4, 19).
Joab, commander of David's army, tried to warn him to not number Israel so that he and the nation would not be punished (1Chronicles 21:2 - 3). His wise counsel, however, went unheeded.
Easton's Illustrated Bible Dictionary also has some interesting comments regarding this incident. It states that David acted out of pride and a desire to glorify himself. He had come to the point in his life where he was relying far more on his own strength and that of his army rather than on God.
After his sin, God told David to choose one of three ways to be punished. The first way was to have a famine throughout the land for seven years. The second choice of correction was to flee from his enemies for three months. David knew what this was like, as he had many times fled from Saul and even had to flee from his son Absalom.
The third way of being punished was three days of pestilence (a deadly epidemic) upon the land. The king choose to, as he put it, 'fall into the hands of the Lord,' which meant the pestilence. Ten of thousands died throughout Israel until the death angel was stopped just before entering Jerusalem. David pleaded for mercy and was told to build an altar to God on a particular threshing floor (2Samuel 24:16 - 18). The pestilence was thus halted.
It would be the threshing floor location on which David appeased God, and ended he and nation being punished, that another momentous event would take place! His sacrifice for taking a census took place on Mount Moriah. This is the same mount where Abraham was to sacrifice his son Isaac (Genesis 22:2). It is also the exact spot chosen by King Solomon to build God's magnificent temple (2Chronicles 3:1).