write a function that computes how many digits with values less than or equal to 5

Okay so I am new at c++ and was assigned a problem. I do not know exactly what I am being asked let alone how to even start this one. Can someone push me into the right direction?

Write a function that computes how many digits with values less than or equal to 5 and how many digits with values greater than 5 are there in a given positive integer. Your function should takes an integer parameter of long type and return the number of digits that have values equal or less than 5 and the number of digits that have values greater than 5. You also need to write a main program to test your function.

Last edited on
You can use the modulus operator (%) with the value 10.

This will give you the last value of the integer.

So 516646949761 % 10 = 1, 821892928 % 10 = 8. You see?

Now just have two variables.

1.) int count_05
2.) int count_69

Now have an if statement checking the value of the modulus value.
Increment whichever counter count_05 or count_69.

Then just print them out.
Can you explain what I am even suppose to be trying to figure out? I don't even understand what I am being asked.
It is asking you to take some number and print out the frequency of individual numbers that make up the entire number.

So for 362408547

3, 2, 4, 0, 5, 4 are all less than or equal to 5. So the number of digits that fit this criteria is 6.

6,8,7 are the leftovers. They total 3. So you just print those values out.

3 (less than)
6 (greater than)
2 (less than)
4 (less than)
0 (less than)
8 (greater than)
5 (less than)
4 (less than)
7 (greater than)

See? If not let me know.
Ok, now I understand. Going back to your original reply, How would that modulus affect anything but the last number?
Last edited on
Yeah I too have this question- wouldn't that read only the last digit? How would you have the program move on to the other values
1
2
3
4
5
6
7
8
9
while (number > 0)
{
    remainder = number % 10;
    if (remainder < 6)
        counter1++;
    else
        counter2++;
    number /= 10;
}
So, to explain fg109 program, which is correct.

While the number is larger than 0. So your number will be looped through completely.

remainder, just an integer to be used to specify the modulated number. So remainder on 27 would be for example 27 & 10 would be 7. So remainder now checks if it is less than 6 or greater than 6.

If it is less than 6, (same thing as <=5 for ints) then it adds one to the less_than_six counter.

If it is greateer than 6, or equal to 6, it adds one to the greater_than_six counter.

Now all you would need to do is print the values of less_than_six, and greater_than_six.
Works great thank you!
#include <iostream>
using namespace std;
int calc1(int);

int calc1(int number){
int counter1;

while (number > 0)
{
int remainder;

remainder = number % 10;

if (remainder < 6)
counter1++;
else;

}

return counter1;

}
int calc2(int);

int calc2(int number){

int counter2;

while (number > 0)
{
int remainder;

remainder = number % 10;

if (remainder <= 5)
counter2++;
else;
}

return counter2;
}

int main() {
int number;
cout<<"Enter a value: ";
cin>>number;


cout<<"In the value you entered, there are "<<calc1(number)<<" numbers less than or equal to 5 and "<<calc2(number)<<" numbers greater than 5."<<endl;


}
for some reason I'm not getting an output. I can't figure out why
never mind I figured it out. Thanks for the hints!!
Hi fayeda2014, I am writing some code based off your structure but am having the same problem with the outputs not showing. What did you do to fix it?
fayeda2014 what did you do to get your code to run??
Topic archived. No new replies allowed.