Help with implementing 3 functions for a problem that can be solved with just 1

Hi all, I'm new to the forum! I was wondering if someone could point me out with the logic my code. I essentially have to display the multiples of 79 starting from 0 to 1027 and reverse each number. The output should look something like this:
0 0
79 97
158 851
...
1027 7201

The prompt told us to use these three functions.
int reverse(int num);
int getDigit(int num, int index); // return the index'th digit of num
int numDigits(int num); // return the number of digits in num

I solved the problem essentially just using int reverse and don't understand how to implement the other 2.

Here is my code just using the function int reverse(int num):

#include <iostream>
using namespace std;
int reverse(int num);
int getDigit(int num, int index);
int numDigits(int num);
int main () {
int number;
cout << "enter a number: ";
cin >> number;
for (int i =0; i<=1027; i+=79) {
cout << i << " " << reverse(i) << endl;
}
return 0;
}

int reverse(int num)
{
int temp = num;
int sum = 0;
while (temp)
{
sum*=10;
sum += temp%10;
temp/=10;
}
return sum;
}

Any pointers would be appreciated.
I've already attempted to define int numDigits(int num) to count the amount of digits in the numbers in each iteration and started rewriting my code.

int numDigits(int num) {
int n = 0;
while (num >0) {
num = num/10;
n++;
}
return n;
}

thank you.
Last edited on
you need to store the integer in a string by using sprintf_s

look at the link , scroll down to requirements :
https://msdn.microsoft.com/en-us/library/ce3zzk1k.aspx
Ericool: Do not suggest non-standard functions when there is a perfectly suited standard ones.

Depending on your requirement (does your teacher wants some specific way?) you can solve it in different ways:
1) Mathematically: divide by 10 until number is 0, cout how many times you divided for digits. Divide by 10 n or n - 1 or len - n times (depending on how you will number your digits) and find remainder from division by 10 for nth digit.
2) By stringifying number and working with it as with string. You can use std::to_string, stringstreams or C's snprintf.
Topic archived. No new replies allowed.