Previous topic | Next topic | Ada Home Page | Index

Print a number

Problem: Print a number (an integer)

Recursive algorithm:

  1. print all of the number except the last digit
  2. print the last digit

Pseudocode:

	procedure PrintNum (N : in integer) is
	begin
		print the number N DIV 10;
		print the digit N MOD 10
	end PrintNum;

Stopping condition added

	procedure PrintNum (N : in integer) is
   	begin
		if N < 10 then
			print the digit N
		else
			print the number N DIV 10;
			print the digit N MOD 10
		end if;
	end;

Recursive procedure

	procedure PrintNum (N : integer) is
	begin
		if N < 10 then
			put(N,WIDTH=>1)
		else
		  	PrintNum (N DIV 10);
			put ((N MOD 10), WIDTH=>1)
		end if;
	end;


Previous topic | Next topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 23 Feb 96