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.
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
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.
}
}
#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' ;
}
#include <iostream>
usingnamespace 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;
elseif ( 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
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.
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
//}
}
// 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;
}
}
// 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;
}
}