We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,489 Members — Technology Publication meets Social Media

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!

8
Contributors
7
Replies
6 Months
Discussion Span
1 Year Ago
Last Updated
8
Views
rayden150
Light Poster
32 posts since Aug 2010
Reputation Points: 9
Skill Endorsements: 0

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.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Skill Endorsements: 37

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).

sfuo
Master Poster
713 posts since Jul 2009
Reputation Points: 173
Skill Endorsements: 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

DDoerschuk
Newbie Poster
6 posts since Aug 2007
Reputation Points: 10
Skill Endorsements: 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

mazzica1
Junior Poster
148 posts since Oct 2011
Reputation Points: 9
Skill Endorsements: 0

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);``
therockon7throw
Newbie Poster
15 posts since Feb 2012
Reputation Points: 10
Skill Endorsements: 0

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

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Skill Endorsements: 37

``````#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);
}
}``````
siaswar
Newbie Poster
11 posts since Jan 2011
Reputation Points: 10