Hi to all
I have a quick question. How can go through the integer to find if there is any specific number. For example my input would be 234446 (integer is of type long),and now I would like to check that integer and find how many 4's are in there and then display it.
Appreciate it

You can extract the no.'s digits by this method :
if n is user's input of int type then

``````while(n>0)
{
n=n/10;
digit=n%10;
if(digit==4)
counter++;
}
``````

Then print the counter that contains the no. of 4's in your inputted number

the method is easy :)

Thank for reply
but it don't work, try yourself, I'm sending you piece of code. No matter what numbers you pick, it is just not right (or maybe its me)

``````#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
long n;
int found5,found8;
long digit;
found5=0;
found8=0;
cout<<"Enter an integer: ";
cin>>n;
while(n>0)
{
n=n/10;
digit=n%10;
if(digit==5)
found5++;
else
if (digit==8)
found8++;
}

cout<<"We have: "<<found5<<" 5's and we have: "<<found8<<" 8's";
getch();
return 0;
}
``````

thank you

Logical flaw with your code:

Doing it that way skips the single's digit if the number is found there as you are dividing the number by 10 before you modulus by 10 on the first iteration and check for 4.

Example: 3334 on the first iteration would be:
3334/10
333%10

check:
if( 3 == 4) false;
counter does not update, remains 0;

try:

``````while(n>0)
{
digit=n%10;
if(digit==4)
counter++;
n=n/10;
}
``````

yes perfectly true I did not run the code but I had the statements in mind quite well but not the order . I am sorry.

It is first extract the digit by mod method then use wherever you wish to and then divide the number by 10 to reduce it to zero.

Got it. Thanks for your help.Appreciate it

Be a part of the DaniWeb community

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