Here is source code of the C++ program which counts the number of ones in the given number. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
* C++ Program to Count the Number of ones in the given Number
using namespace std;
int num, temp, count = 0;
cout << "Enter the number : ";
cin >> num;
temp = num;
while (temp != 0)
if (temp % 10 == 1)
temp = temp / 10;
cout << "The number of ones in " << num
<< " is " << count << endl;
$ g++ main.cpp $ ./a.out Enter the number : 101 The number of ones in 101 is 2
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
If you liked this C++ Program, kindly share, recommend or like below!