//Requires: start > 0; end > 0; 0 <= digit <= 9 //Modifies: nothing //Effects: r
ID: 3631453 • Letter: #
Question
//Requires: start > 0; end > 0; 0 <= digit <= 9
//Modifies: nothing
//Effects: runs the range of numbers from start to end inclusive
// if (end < start), it will swap start and end
// prints the number if any digit of the number is the
// same as 'digit'
// printIfHoldsDigit(5, 10, 7) //prints: 7
// printIfHoldsDigit(5, 30, 8) //prints: 8, 18, 28
// printIfHoldsDigit(1, 30, 2) prints: 2, 12, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29
// and yes you need to print the commas
void printIfHoldsDigit(int start, int end, int digit);
//Requires: n >= 0;
//Modifies: n
//Effects: alters n to be the integer formed by reversing its digits
// if n = 123, then it will be altered to be 321
// if n = 1230, then it will be altered to be 0321, i.e., 321
void reverseNum(int & n);
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
void reverseNum(int &);
void printIfHoldsDigit(int , int , int );
int main()
{int n=123;
cout<<n<<" reversed= ";
reverseNum(n);
cout<<n<<endl;
n=1230;
cout<<n<<" reversed= ";
reverseNum(n);
cout<<n<<endl;
printIfHoldsDigit(5, 10, 7);
printIfHoldsDigit(5, 30, 8);
printIfHoldsDigit(1, 30, 2);
system("pause");
return 0;
}
void reverseNum(int & n)
{int num=0;
while(n>0)
{num=num*10+n%10;
n/=10;
}
n=num;
}
void printIfHoldsDigit(int start, int end, int digit)
{int t,i,first=0;
if(start>end)
{t=start;
start=end;
end=t;
}
for(i=start;i<end;i++)
if(i%10==digit||i/10==digit)
if(first==0)
{cout<<i;
first=1;
}
else
cout<<", "<<i;
cout<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.