Extracting the amount of integers that contain the number "5" from a given range.

Oct 21, 2017 at 12:10am
I am writing a program for a homework assignment in which the user is promped for a minimum and maximum range in integer form. The given range is then to be tested for integers containing a specific number, lets say "5". The program is then supposed to print out "There is/are X numbers containing 5 within the specified range."

I am having difficulty finding a way to extract the amount of integers that contain "5" from the range the user inputs. it has to be in a loop, and with "if" statements.

I am not allowed to use stringstream, as the professor has not taught the class that.

My knowledge of c++ is very elementary, so please forgive my noobness.

Last edited on Oct 21, 2017 at 2:16am
Oct 21, 2017 at 3:32am
You'll need to use %10 to get the last digit and you then need to remove the last digit if it's not found. So lets say you the number you're trying to check is 4521. What you should do is
1
2
3
4
5
6
7
8
9
10
4521 % 10 = 1
1 == 5 // False
New Number: 452

452 % 10 = 2
1 == 2 // False
New Number: 45

45 % 10 = 5
5 == 5 // True 


Then just throw this into a for loop. I would suggest you make a function for this so it's easy to call during your loop.
Last edited on Oct 21, 2017 at 3:32am
Oct 21, 2017 at 6:14am
Thank you for your reply. I must admit however, that im not sure how to apply this to my code.

Below is my code that places the numbers in a given range into a variable, in this case "range". Since the range is not going to be static, i need to find a way to test all numbers in any possible range. Say the user inputs 1 as "startingPoint" and 10 as "endPoint", there is one number with a 5, 5. But i need the number 1 in this case, as "there is 1 number containing a five in the given range" is what needs to be printed out.

1
2
3
4
5
6
7
8
9
int range, totalNumbersWithFives;
if (startingPoint <= endPoint)
{
		for (range = startingPoint; range <= endPoint; range++)
		{
			//code to find the numerical amount of numbers in the range that contain 
                        //fives, and put that numerical amount into a varible to be printed out.
		}
}
Oct 21, 2017 at 7:10am
Brute force is more than adequate for this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>

bool has_digit_5( int number )
{
    if( number < 0 ) return has_digit_5( -number ) ;

    if( number < 10 ) return number == 5 ; // true if the single digit number is 5

    return number%10 == 5 || // true if either a. the last digit is 5
           has_digit_5( number/10 ) ; // or b. there is a 5 in one of the earlier digits
}

int main()
{
    int from = -654321 ;
    int till = +123456 ;
    int cnt = 0 ;

    for( int num = from; num <= till; ++num ) if( has_digit_5(num) ) ++cnt ;

    std::cout << "count of numbers with the digit 5 in [" << from << ',' << till
              << "] == " << cnt << '\n' ;
}

http://coliru.stacked-crooked.com/a/8884bacd0c279c13
Oct 21, 2017 at 8:45am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

bool containsChar( string test, char c ) { return test.find( c ) != string::npos; }

int main()
{
   int a, b, digit;
   cout << "Enter beginning and end of range: ";   cin >> a >> b;
   cout << "Enter digit to be found: ";   cin >> digit;
   char cdigit = digit + '0';

   int counter = 0;
   for ( int i = a; i <= b; i++ ) if ( containsChar( to_string( i ), cdigit ) ) counter++;

   cout << "There is/are " << counter << " numbers containing " << digit << " within the specified range";
}


Enter beginning and end of range: -654321 123456
Enter digit to be found: 5
There is/are 375002 numbers containing 5 within the specified range
Oct 21, 2017 at 1:49pm
Direct attack rather than brute-force search.
(Positive ends of range only)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;

int frequency( int N, int digit )           // Number of times digit occurs in 0 ... N
{
   if ( N < 10 ) return ( N >= digit ? 1 : 0 );

   int first = N;
   int pow10 = 1;
   int pow9  = 1;
   while ( first >= 10 )
   { 
      first /= 10;
      pow10 *= 10;
      pow9  *= 9 ;
   }
   int rem = N - first * pow10;             // Remainder after removal of first digit
   int freq999 = pow10 - pow9;              // Frequency in 99...99 (one less digit)
   int freqrem = frequency( rem, digit );   // Frequency in remainder (recursive)
   int result;

   // Consider number started by each possible digit
   if ( first < digit || digit == 0 ) result = first * freq999 + freqrem;
   else if ( first == digit         ) result = first * freq999 + 1 + rem;
   else                               result = ( first - 1 ) * freq999 + freqrem + pow10;

   return result;
}                                                                        


int main()
{
   int a, b, digit;
   cout << "Enter first and last (both positive): ";   cin >> a >> b;
   cout << "Enter digit to be found: ";                cin >> digit;

   cout << "There are " << frequency( b, digit ) - frequency( a - 1, digit )
        << " numbers containing " << digit << " within the specified range";
}


Enter beginning and end of range (positive only, please): 1 654321
Enter digit to be found: 5
There is/are 326272 numbers containing 5 within the specified range

Enter beginning and end of range (positive only, please): 1 123456
Enter digit to be found: 5
There is/are 48730 numbers containing 5 within the specified range

Last edited on Oct 21, 2017 at 7:06pm
Oct 21, 2017 at 5:29pm
Thank you all so much for the replies!

There is a lot to work with here. Most of this is beyond my understanding and beyond what our professor has currently taught us so unfortunately I cant use a good amount of it. But either way, it still helps by showing me the different ways it can be done.

Last edited on Oct 22, 2017 at 1:10am
Oct 23, 2017 at 4:53am
Okay, so I made some progress, but Im still stuck.

I was able to get the program to extract the amount of integers containing the number 5, so long as the second digit was 5. In other words, if the range is from 1 to 100, 50-54 and 56-59 were ommitted from the numerical amount. I was told to use "numberToBeTested /= 10" to remove the rightmost digit. however, when i set it up as i did to extract the second digit, i get an error saying "integer division by zero". I was then told i needed to put it in a while loop that resides within the "for" loop i have for "numbersInBetween"... however im having a hard time understanding the hint he gave:

while (there are still numbers to count)
{
test one digit and remove it from the number being tested.
}

ive spent a good few hours trying different things, and I honestly feel quite frustrated that im unable to figure it out...especailly since the answere is probably something obvious that im overlooking.

here is the part of my program im having trouble with.


1
2
3
4
5
6
7
8
9
int numbersInBetween, totalNumbersWithFive = 0;
for (numbersInBetween = startingPoint; numbersInBetween <= endPoint; numbersInBetween++)
{
if (numbersInBetween % 10 == NUMBERS_WITH_FIVE) totalNumbersWithFive++;
//while (there are still numbers to count)
//{
//	 test one digit and remove it from number being tested
//}
}
Oct 23, 2017 at 11:10pm
Solved it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
// test the givin range for numbers containing fives
int range, totalNumbersWithFive = 0;
for (range = startingPoint; range <= endPoint; range++)
{
	int numbersInRange = range;
	while (numbersInRange > 0)
        {
	    if (numbersInRange % 10 == FIVE)
	    {
		   totalNumbersWithFive++;
	    }
	numbersInRange /= 10;
	}
}
Oct 24, 2017 at 1:38am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// test the givin range for numbers containing fives
int range, totalNumbersWithFive = 0;
for (range = startingPoint; range <= endPoint; range++)
{
	int numbersInRange = range;
	while (numbersInRange > 0)
        {
	    if (numbersInRange % 10 == FIVE)
	    {
		   totalNumbersWithFive++;
                   break ; // *** otherwise numbers containing more than one 5 would be counted 
                           //     multiple times. for example, 555 would be counted as three numbers
	    }
	    numbersInRange /= 10;
	}
}
Oct 24, 2017 at 2:23am
I was just about to comment again asking about that, THANK YOU X100000000!!!!
Topic archived. No new replies allowed.