|
UMBC CMSC 201 Fall '06 CSEE | 201 | 201 F'06 | lectures | news | help |
/* File: countdown.c
* Author: Richard Chang
* Date: pre 1997
* Modified by: Sue Evans
* Date: 11/8/05
* Section: 101
* EMail: bogar@cs.umbc.edu
*
* A very simple example of a recursive function.
*/
#include <stdio.h>
void Countdown (int) ;
int main()
{
int n ;
printf("Engaging the auto-destruct sequence\n") ;
printf("Enter number of seconds to auto-destruct: ") ;
scanf("%d", &n) ;
printf("Counting down from %d\n", n) ;
Countdown(n) ;
return 0;
}
/******************************************
* Function Name: Countdown ()
* Countdown() uses recursion to count down from k to 0
*
* Inputs: integer to count down from
* Outputs: returns nothing
* Side effect: prints to screen the counting down and
* "KABOOM!!!"
******************************************/
void Countdown (int k)
{
/* The BASE CASE */
if (k == 0)
{
printf("KABOOM!!!\n") ;
}
/* The GENERAL RULE - recursive case */
else
{
printf(" %d seconds to auto-destruct\n", k) ;
Countdown (k - 1) ;
}
}