I need to write a program like this
Sample output:
Please enter a number(Less than 8 digits):
1203021

There are 2 zero (0) in this number
I dont know how to write a formula to count the 0 inside number please teach me.

The Code below shows how to iterate Strings.
With Efforts you should be able to get what you want
God bless you!

``````#include <string>
#include <iostream>

int main()
{
int i=0;
std::string s = "UJehova uyaphile Yesu Mwema!";
for(i; i<s.length(); i++)
{
std::cout<<s[i]<<std::endl;
}
return 0;
} …``````

Taking into account that:

``````If a number N ends in zero then the remainder of N integer divided by ten is zero
``````

You could count the number of zeroes inside a number N with the following algorithm:

``````Initialize Counter to zero
While N …``````

## All 5 Replies

The Code below shows how to iterate Strings.
With Efforts you should be able to get what you want
God bless you!

``````#include <string>
#include <iostream>

int main()
{
int i=0;
std::string s = "UJehova uyaphile Yesu Mwema!";
for(i; i<s.length(); i++)
{
std::cout<<s[i]<<std::endl;
}
return 0;
}
``````

Taking into account that:

``````If a number N ends in zero then the remainder of N integer divided by ten is zero
``````

You could count the number of zeroes inside a number N with the following algorithm:

``````Initialize Counter to zero
While N is greather than zero
IF N ends in zero then increment Counter
Integer divide N by ten
End while
Print Counter.
``````

with my hint above modulo operation is unnecessary!
:)

You could count the number of zeroes inside a number N with the following algorithm

And if the number begins with zeroes? What if the number exceeds the range of your numeric type? The best approach to handle input as it's typed is to work with it as a string rather than a numeric data type, like in Stefano's hint.

even tho both gentleman before did provide great help i just wanted to participate since i'm still a student like you .

``````#include <iostream>
#include <string>

using namespace std;

int main()
{
string inputNum;
int numCount = 0;

cout << "Please enter an 8 digit number" << endl;
cin >> inputNum;
while ((inputNum.length() > 8) || (inputNum.length() < 0)) // make sure the range is equal or less than 8 digit & larger than 0
{
cout << "Sorry you have entered an invalide range - please re enter an 8 digit number " << endl;
cin >> inputNum;
}
for (int counter = 0; counter < inputNum.length(); counter++) // loop throughout the entire string in order to read each char
{
if (inputNum[counter] == '0' ) // if char '0' match increase the numCount for final display
{
numCount++;
}
else
{
;
}
}
cout << "There are ; " << numCount << " Zero (0) in this number " << endl;

system("pause");
return 0;
}
``````

Thank You.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.