C++ Course Listing

 

Example Code:

Recursion

// recursion01.cpp : Defines the entry point for the console application.
// Matthew Martin, 2004

#include "stdafx.h"
#include "stdio.h"
#include "factr.h" //NB: You must write this Header (.h) file!!!


int main(int argc, char* argv[])
{
int i;
int num;
printf ("Please enter the number you want to factorise and hit return: ");
scanf ("%i", &i);
num = factr (i);
return 0;
}

int factr (int n)
{
int answer;
if (n==1) return 1;
answer = factr (n-1) * n;

// The following line displays values to the screen.
printf ("\n answer: %i", answer);
printf ("\n n: %i", n);
printf ("\n");

return answer;
}

 

by Matthew Martin

top of page