Skip to content

Instantly share code, notes, and snippets.

@juanfal
Created March 25, 2026 08:02
Show Gist options
  • Select an option

  • Save juanfal/fb5023d415fa2c462ce20b7ed969d06b to your computer and use it in GitHub Desktop.

Select an option

Save juanfal/fb5023d415fa2c462ce20b7ed969d06b to your computer and use it in GitHub Desktop.
count digits all in main
// t04e19.countdigits.cpp
// juanfc 2026-03-25
//
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
if (n < 0)
n = -n;
int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0,
count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0;
if (n == 0) {
count0 = 1;
} else {
while (n > 0) {
switch (n % 10) {
case 0: count0++; break;
case 1: count1++; break;
case 2: count2++; break;
case 3: count3++; break;
case 4: count4++; break;
case 5: count5++; break;
case 6: count6++; break;
case 7: count7++; break;
case 8: count8++; break;
case 9: count9++; break;
}
n /= 10;
}
}
if (count0 > 0) { cout << "0:" << count0 << " "; }
if (count1 > 0) { cout << "1:" << count1 << " "; }
if (count2 > 0) { cout << "2:" << count2 << " "; }
if (count3 > 0) { cout << "3:" << count3 << " "; }
if (count4 > 0) { cout << "4:" << count4 << " "; }
if (count5 > 0) { cout << "5:" << count5 << " "; }
if (count6 > 0) { cout << "6:" << count6 << " "; }
if (count7 > 0) { cout << "7:" << count7 << " "; }
if (count8 > 0) { cout << "8:" << count8 << " "; }
if (count9 > 0) { cout << "9:" << count9 << " "; }
cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment