2nd book of Samuel 24:2.9-17.
King David said to Joab and the leaders of the army who were with him, “Tour all the tribes in Israel from Dan to Beer-sheba and register the people, that I may know their number.”
Joab then reported to the king the number of people registered: in Israel, eight hundred thousand men fit for military service; in Judah, five hundred thousand.
Afterward, however, David regretted having numbered the people, and said to the LORD: “I have sinned grievously in what I have done. But now, LORD, forgive the guilt of your servant, for I have been very foolish.”
When David rose in the morning, the LORD had spoken to the prophet Gad, David’s seer, saying:
“Go and say to David, ‘This is what the LORD says: I offer you three alternatives; choose one of them, and I will inflict it on you.'”
Gad then went to David to inform him. He asked: “Do you want a three years’ famine to come upon your land, or to flee from your enemy three months while he pursues you, or to have a three days’ pestilence in your land? Now consider and decide what I must reply to him who sent me.”
David answered Gad: “I am in very serious difficulty. Let us fall by the hand of God, for he is most merciful; but let me not fall by the hand of man.”
Thus David chose the pestilence. Now it was the time of the wheat harvest when the plague broke out among the people. (The LORD then sent a pestilence over Israel from morning until the time appointed, and seventy thousand of the people from Dan to Beer-sheba died.)
But when the angel stretched forth his hand toward Jerusalem to destroy it, the LORD regretted the calamity and said to the angel causing the destruction among the people, “Enough now! Stay your hand.” The angel of the LORD was then standing at the threshing floor of Araunah the Jebusite.
When David saw the angel who was striking the people, he said to the LORD: “It is I who have sinned; it is I, the shepherd, who have done wrong. But these are sheep; what have they done? Punish me and my kindred.”