## Count zeroes with Recursion keep crashing

recursion c++

what is recursion

why recursion is used

recursion explained

how does recursion work java

recursion in c factorial

tricky questions on recursion

My code is supposed to count the number of zeroes using recursion.

However my program keeps crashing.

Expected output:

Input: 10500 Result: 3

Code:

void rCountZeros2(int num, int *result) { if (num > 0) { if ((num % 10) == 0) { rCountZeros2((num / 10) + 1, *result); } rCountZeros2(num / 10, *result); } else if (num == 0) { *result = 1; } *result = num; }

How do I make it work?

Here's the solution with and without using pointers Hope this helps

#include <stdio.h> // For C++ #include<iostream> // Without using pointer int count_zeros_recursive2(int num) { if (num % 10 == 0) { if (num > 19 || num < -19) { return 1 + count_zeros_recursive2(num / 10); } return 1; } if (num > 19 || num < -19) { return count_zeros_recursive2(num / 10); } return 0; } // Using pointer void count_zeros_recursive(int num, int *result) { if (num % 10 == 0) *result = *result + 1; if (num > 19 || num < -19) count_zeros_recursive(num / 10, result); } int count_zeros(int num) { int result = 0; count_zeros_recursive(num, &result); //result = count_zeros_recursive2(num); return result; } int main() { int n; while (1) { scanf("%d", &n); // For C++ std::cin >> n; printf("%d\n", count_zeros(n));// For C++ std::cout << n << "\n"; } return 0; }

eg Input: 1010 Output: 2 Input: 0 Output: 1 Input: 10204012410 Output: 4 Input: -101 Output: 1

Neal Stephenson (REAMDE, The Diamond Age, Snow Crash, etc.), William Gibson (Count Zero, Neuromancer, etc.) world, or situations involving recursively entering a simulation from within a simulation, shared virtual world coherently allow other users to keep experiencing the existence that they chose and designed? A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

I prefer returning the result instead of passing the pointer to the result.
Stop condition is when the number is a single digit, then if 0 you have 1 zero, otherwise none.
If the number is not a single digit, calculate the number of zeros in the number without the lowest digit `(num/10)`

and add 1 to the returned value if the lowest digit was 0.

#include <stdio.h> int count_zeros_recursive(int num) { int res = 0; int num_without_lowest_digit = num/10; if(num_without_lowest_digit == 0) { res = (num == 0); } else { res = count_zeros_recursive(num_without_lowest_digit); if (num % 10 == 0) { res++; } } return res; } int main(void) { int nums[] = { 202, 7, 100, 10500, 10234, 10000}; int i; for (i=0; i < sizeof(nums)/sizeof(nums[0]); ++i) { printf("Number %d has %d zeros\n", nums[i], count_zeros_recursive(nums[i])); } return 0; }

Number 202 has 1 zeros Number 7 has 0 zeros Number 100 has 2 zeros Number 10500 has 3 zeros Number 10234 has 1 zeros Number 10000 has 4 zeros

recursively starts at zero and prints all the integers from zero to the parameter (that is, I was able to write a similar code that counts down then up. recursiveDownAndUp(i-1); // recursive call How I can fix this to make it stop directly. The program doesn't crash when he runs it, its just that the buttons show up out of The purpose of this article is to demonstrate why counting with Recursive CTE's is such a bad idea and 3 ways you can quickly (and I do mean quickly, this time) get around the Hidden RBAR 1

Let's say you really want to use a pointer for some reason. Here's a full solution:

#include <stdio.h> void rCountZeros2(int num, int *result) { if(num>0) { if( (num % 10) == 0) { *result = *result + 1; } rCountZeros2(num/10, result); } } int main() { int i = 0; int n; n = scanf("%d", &n); int *result = &i; rCountZeros2(n, result); printf("%d\n", *result); }

So what will happen is that you're effectively just updating the value at the location which `result`

is pointing to, which means that you're really just storing the count in the variable `i`

.

Explains recursion and how to use it in programs. The computer keeps function calls on a stack and once too many are called without ending, the program will crash. checks some variable for a condition (such as a number being less than zero, If we'd chosen to stop when count equaled ten, then if the function were Using Recursion to Count Up then Down I'm trying to design a recursive method for printing rows of numbers from zero up to a user-specified number using the same recursive method for both counting up and down with the output looking like the following example if the given number was 6:

There is no need to pass a pointer if you can use a functional approach. Just return the value.

long recursiveZeroesCount(long num) { if(num == 0) return 1; if(num < 10) return 0; if(num % 10 == 0) return 1 + recursiveZeroesCount(num / 10); else return recursiveZeroesCount(num / 10); }

Learn how to use recursion in C and C++, with example recursive programs. The computer keeps function calls on a stack and once too many are called without ending, void recurse ( int count ) // Each call gets its own count checks some variable for a condition (such as a number being less than zero, or greater than This is from javabat.com. The Recursion-1 section and problem titled count7. Given a non-negative int n, return the count of the occurrences of 7 as a digit, so for example 717 yields 2. (no loops). Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).

Your program has undefined behavior because you pass the value of the `*result`

instead of the pointer to the result variable to recursive calls. Furthermore, your algorithm is flawed as you cannot use this API recursively because the recursive calls update the result as well as the current call, a very impractical situation.

Here is a corrected version without recursion:

void rCountZeros2(int num, int *result) { int count = 0; for (;;) { if (num % 10 == 0) count++; num /= 10; if (num == 0) break; } *result = count; }

And here is a recursive implementation with a simpler API:

int rCountZeros2(int num) { if (num % 10 == 0) { if (num == 0) return 1; else return 1 + rCountZeros2(num / 10); } else { if (num < 10) return 0; else return rCountZeros2(num / 10); } }

The above function can be simplified as a single statement:

int rCountZeros2(int num) { return !(num % 10) + ((num < 10) ? 0 : rCountZeros2(num / 10)); }

Bidirectional filtering: Recursive method used to produce a zero phase filter. Carried out by counting in binary with the bits flipped left-for-right. from another; fastest roll-off while keeping the passband flat; can be analog or digital. Closing: A morphological operation defined as an erosion operation followed by a Java > Recursion-1 > count8 (CodingBat Solution) Problem: Given a non-negative int n, compute recursively (no loops) the count of the occurrences of 8 as a digit, except that an 8 with another 8 immediately to its left counts double, so 8818 yields 4.

countDown(count-1); // countDown() calls itself recursively. } stack overflow will result, and the program will crash or terminate. A recursive termination is a condition that, when met, will cause the recursive function to stop calling itself. seems like it works for negative and positive integers and zero. Now you can just call MainFun() as before, not passing a count, and the count is set to zero for the first call. All following recursive calls pass a count so the default value isn't used. All following recursive calls pass a count so the default value isn't used.

Sample Output. // 2. package Recursion;. public class CoutZeroesRecursive {. public static void main(String[] args) {. System.out.println(countZerosRec(10204));. }. Unity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.

It follows from power counting that this integral contributes at order Vy. Keeping also the subleading term in the (h-p) contribution, we obtain [18] i-d/2a-0B = 2 1 1/2 (i"e"). all propagators are expanded in their zero-order components squared. The resulting (d – 1)-dimensional integrals can be calculated recursively in Given an integer n, write a function that returns count of trailing zeroes in n!. Input: n = 5 Output: 1 Factorial of 5 is 120 which has one trailing 0. Input: n = 20 Output: 4 Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes. Input: n = 100 Output: 24. A simple method is to first calculate factorial of n, then count trailing

##### Comments

`rCountZeros2((num/10) + 1, *result);`

- remove the`*`

. And the other call as well. The compiler should have warned you about this.- You've got a good start with giving us the input and expected output, now throw in the actual output you are getting.
- Why the line
`*result = num`

? I think, your algorithm to count zeros has to be reconsidered. - firstly, dont ignore compiler warnings (that you are surely getting)
- The questioner possibly wanted to pass a pointer as a parameter, since he also chose the tag "pointer". With a pointer, you could simply sum all zeros of your array of numbers.
- @TomKuschel, Same this can be achieved without passing the result as a pointer. Just instead passing the
`result`

by pointer, it can add the returned value:`result += count_zeros_recursive(nums[i]);`

. As I mentioned in the answer, I prefer this because then the function is self contained and there is no doubts if it has to check for a`NULL`

value or should it or should not initialize the`*result`

to 0, etc.