how to keep subtracting recursively

Hi all

i need to keep subtracting 24 recursively and stop only when the subtraction would give a negative result

this is what i am doing

do
{
current_val = current_val - 24;
} while (current_val > 0);

so if the current value is 77 then it should subtract (24) 3 times and return 5
but its gets stuck in the while loop :(
am i doing something wrong here? any guidance would be greatly appreaciated

thanx
1
2
while ((current_val -24) >= 0)
  current_val -= 24;

Keep in mind that produces the same result as:

current_val %= 24;

Hope this helps.
By the way, this is iteration, not recursion.
Duoas is right, but if you want to use your method its like this:




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
int current_val;

int count = 0;

cin>>current_val;

if(current_val>24)

{

do{

count++; // divide count

current_val -= 24;

    }while(current_val>24); // before was >0 which would return a neg number if > 24

cout<<"Remainder = "<<current_val<<endl

       <<"Divided "<<count<<" times"<<endl;

}
else

cout<<"NOT DIVISABLE"<<endl; // if less than 24

Last edited on
Please do n % 24.

there's nothing like maintaining an O(n) algorithm to solve a problem that can be done in constant time.
Hey jsmith, he's just making himself more valuable to his employer, by writing code only he can maintain...
closed account (S6k9GNh0)
>.> Wouldn't surprise me...I've seen that happen a LOT without even having a job (High School Student = me)
Last edited on
thank you all..it certainly helped a lot :)
Topic archived. No new replies allowed.