I am trying to make a recursive program that prints out
the numbers from 1-100 without using any iterations such
as loops, but every-time it just keeps printing out 5050..?

#include<stdio.h>
#include<stdlib.h>

int counter(int num);
int sum=0;

  int main(){
      int result, num=100;
      result = counter(num);
      printf("%d", result);
      system("PAUSE");
      return 0;
      }
      
      int counter(int num){
          if (num==0)
             return sum; 
                else{
                  sum = sum+num;  //<--- If I change the num to 1 it just prints 100 not all the numbers from 1-100
                  counter(--num);}
                      
                       return (sum);
          }

Thanks for yourr help!

Recommended Answers

All 7 Replies

Please help on easy C++ recursion.. PLEEASE!

Don't beg. It makes you sound desperate and needy, and people will avoid your post.

Try printing out some values in the function to see what is happening as it runs.

5050 is the sum of the numbers from 1-100.

You are printing the value returned from counter in main() so it will only print once.

If you want to print all the numbers from 1-100 you should have your print statement within the recursive function and you do not need to have the function return anything (make it a void function).

commented: thank you... +0

First, you've declared integer sum as a global. That's both poor technique, and in the case of a variable used in a recursive function it will often cause you design heartache. Please declare it within main() and pass it as a second argument to counter(). Hint: a simpler solution to the problem doesn't even require 2 integer arguments to counter(), but life is hard enough so just get it going with the 2 arguments and then see if you can simplify.

Second, within counter() you comment that you've tried changuing "num" to the constant "1". This is correct: the problem lies elsewhere (see paragraph below). You don't want to add 100 to sum, then 99 to sum, then 98 to sum, and so forth. You just want to add 1 to sum each time counter() recursively executes. By the way, adding 100 + 99 + 98 + 97 + ... + 1 + 0 was how you arrived at 5050. For fun, go to Wikipedia and look up Carl Friedrich Gauss, and go to the section on "mythology" to see a reference to this exact same problem as solved by one of history's great mathematicians.

Third, the biggest problem seems to be that you're having trouble visualizing the recursion. Your main() executes once, with no loops. Your call to the recursive function counter() executes in main(), and then calls itself 99 more times. After the 100th time, control returns to main() and you execute printf() 1 time. Then you pause, and the program terminates. Since you only execute printf() once, you're only going to print one number. Since you want to print all 100 numbers, you're going to have to move the printf() statement to be in the counter() function. Now, exactly where you put the printf() in counter() is really important. As you know, counter() contains a recursive call to itself: counter(). If you put the printf() function BEFORE the recursive call, then the printf() will execute as the recursion is "growing". If you put the printf() function AFTER the recursive call to counter(), then the printf() will execute as the recursion is "shrinking", or "unwinding". The best thing to do is try it first one way, and then the other, and think about it until it's clear.

Good luck,
David

commented: thank you... +0

you print the result of sum ,what you need is not the sum so instead of doing sum operations in recurse function print the number

I am trying to make a recursive program that prints out
the numbers from 1-100 without using any iterations such
as loops, but every-time it just keeps printing out 5050..?

#include<stdio.h>
#include<stdlib.h>

int counter(int num);
int sum=0;

  int main(){
      int result, num=100;
      result = counter(num);
      printf("%d", result);
      system("PAUSE");
      return 0;
      }
      
      int counter(int num){
          if (num==0)
             return sum; 
                else{
                  sum = sum+num;  //<--- If I change the num to 1 it just prints 100 not all the numbers from 1-100
                  counter(--num);}
                      
                       return (sum);
          }

Thanks for yourr help!

Hi,

Your function should be as simple as possible

void counter( int num){
   if( 0 < num && num < 101 )
      cout<< num << endl, counter(++num);
}

and you call it from main like following

counter(1);

Let's keep helping the guy that hasn't been here since July last year....

What about this:

#include <stdio.h>

void counter(int, int);
int main()
{
	counter(1,100);
	return 0;
}

void counter (int start, int end)
{
	printf("%d", start);
	if(start == end)
	{ 	printf("\n");
		return;
	}
	else
	{	printf(" , ");
		counter(start+1, end);
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.