Need help writing c++ with using recursion (intro c++) Attempt inside.

Pages: 12
Hello. I've tried to do this on my own over the weekend but it's really confusing. How would I go about making a recursive function which is my 'int evenUp ( int number )' to add 1 to each of my even digits?

The program will create a new number found by increasing each even digit of user's number by 1. Then the program will output the difference of the new number subtract the old number.

Example:
Enter a number: 3456
3557 - 3456 = 101

Here's what I have so far:


#include <iostream>
using namespace std;

int getNumber()
{
int num;
cout << "Enter a number: ";
cin >> num;
return num;
}


int evenUp (int number)
{
if ( number < 10 && number%2 == 1 ) return number;
int a_digit = number % 10; //stores last digit of the number
if ( a_digit%10 == 0) {
a_digit = a_digit+1;
return a_digit + number/10;
}
}






int main()
{
int number = getNumber();
int evenUpNumber = evenUp( number );

cout << evenUpNumber << " - " << number << " = " << evenUpNumber - number << endl;
system("Pause");
return 0;
}


As you can see my recursion function 'int evenUp (int number)' is wrong but I can't figure out how to fix it.
Any help would be appreciated. Thank you.
Hi Sir,

In order to help you I need to understand more what this function eventUp is intended to do.

Can you explain me what this function is about?

[]'s

Sandro Boschetti.
Hello Sir Sandrorb,


int evenUp (int number) is supposed to take the number the user put in and for every even digit, the function will add 1 to it then return it so I can use it in int main.

So for example, if number is 323, it should return 424.

Thank you for any help.
Based on what you said, the function should be:

int evenUp (int number){
if ( number % 2 != 0) {
return number + 1;
}
return number;
}

Right?
Let me try. I'm going to kick myself if it was that easy.
Nope, it gives me this:

Input: 323
output: 324 - 323 = 1

when it should be
Input: 323
output: 424 - 323 = 101

Thank you anyway, good sir.
I'm probably not understanding your needs.

Do you need a function that return the first even number from the entered number + 100?

Like this?

int evenUp (int number){
if ( (number + 100) % 2 != 0) {
return number + 100 + 1;
}
return number + 100;
}
The program is supposed to prompt the user for an integer number.

Then it will take that number and find every even digit and add 1 to it.

So for instance:
User puts in: 323

int evenUp (int number) should make it so that each even digit in 323 will add 1 to it and return 424.

Another example is:
User puts in: 5234
it will return: 5335 because every even digit in 5234 had 1 added to it.
Last edited on
I'm sorry. I think now I understand what you need.

I am thinking over to find out a solution. As sson as I get it, I post it here.
I'm sorry to bother you, sandrorb.

Not at all. It's a good puzzle to be up to date.
Heh. I'm trying out different codes.

Here's what my professor sent me as a hint.


What I have understood from your description and your code;
First You will need to get each digit from your number separately.
Here is how You can get it :
Number is 323.
a_digit = 323 % 10 //(% == Mod)
//a_digit now contains 3; You can even check it here and increase or whatever u like
Number = 323 / 10
//Number will now contain 32


So using other names, here's the same thing as above, written differently
{
a_digit = Number % 10 // a_digit = 2
// Increase digit and store digit if odd etc.
Number = Number / 10 // Number = 3
}



I just can't figure out how to increase by 1 and return it
Almost there. The digits are inverted and I'm not using recursivity.

int evenUp (int number){

int i = 1;
int digit;
int newNumber = 0;

while ( number > 0){
digit = number % 10;
cout << digit;
number = number / 10;
if (digit % 2 != 0) {
if (digit == 9){
digit = 0;
} else {
digit = digit + 1;
}
}
newNumber = newNumber * 10 + digit ;
i++;
}

return newNumber;
}
Hey Sandrorb, I figured it out.

Here it is just incase you're curious. Thank you so much for your help but it had to use recursion.



#include <iostream>
using namespace std;


/**
* This function asks the user for a number.
*
* Parameter: None
* Return: Number enter by user.
*/
int getNumber()
{
int num;
cout << "Enter a number: ";
cin >> num;
return num;
}

/**
* This function takes the number and adds 1 to all even digits
*
* Parameter: integer number given in getNumber()
* Return: Returns a number where all the even digits are added by 1
*/
int evenUp (int number)

{
if ( number < 10 )
{
if ( number % 2 != 0 ) return number;
return number + 1;
}
int digitA = evenUp(number%10);
return evenUp( number / 10 ) * 10 + digitA;
}


/**
* This function prints out the difference
* between the new number and the original number
*
*/
int main()
{
int number = getNumber();
int evenUpNumber = evenUp( number );

cout << evenUpNumber << " - " << number << " = " << evenUpNumber - number << endl;
system("Pause");
return 0;
}
You're welcome!
Don't you think you should change (number % 2 != 0) to number % 2 == 0)?
In your algorithm if you enter a number like 1929 the result is 3030. Is the correct?
I guess you should take care of digit 9.
Even though you have said recursion is mandatory, just for a sake of curiosity, I post this code below:


int evenUp (int number){

int i = 0;
int digit;
int newNumber = 0;

while ( number > 0){
digit = number % 10;
number = number / 10;
if (digit % 2 != 0) {
if (digit == 9){
digit = 0;
} else {
digit = digit + 1;
}
}
newNumber = newNumber + pow(10, i) * digit;
i++;
}

return newNumber;
}
soory for the lack of knowledge ,, but i think that the recursive function is not working properly .
can you please elobrate on the function .
What recursive function? That one by sam0617?
I suppose the only thing wrong with that function is how it treats the digit nine.
Last edited on
Pages: 12