C Programming

RECURSIVE PROGRAMMING: EXERCISE C19
Rewrite example c9 using a recursive function.


	#include <stdio.h>
	long int triang_rec( long int );

	long int triang_rec( long int number )
	{
	    long int result;

	    if( number == 0l )
	      result = 0l;
	    else
	      result = number + triang_rec( number - 1 );
	    return( result );
	}

	main ()
	{
	  int request;
	  long int triang_rec(), answer;

	  printf("Enter number to be calculated.\n");
	  scanf( "%d", &request);

	  answer = triang_rec( (long int) request );
	  printf("The triangular answer is %l\n", answer);
	}


Note this version of function triang_rec

	#include <stdio.h>
	long int triang_rec( long int );

	long int triang_rec( long int number )
	{
	   return((number == 0l) ? 0l : number*triang_rec( number-1));
	}


 ©Copyright B Brown. 1984-1999. All rights reserved.