Add Hanoi C example.

This commit is contained in:
Nicolás A. Ortega Froysa 2022-10-17 16:40:39 +02:00
parent bd127e494f
commit cf292e1e27

33
2/SAD/hanoi.c Normal file
View File

@ -0,0 +1,33 @@
#include <stdlib.h>
#include <stdio.h>
void print_hanoi(int n)
{
if(n == 2)
{
printf("1\n2\n1\n");
}
else
{
print_hanoi(n-1);
printf("%d\n", n);
print_hanoi(n-1);
}
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Give me a number!!!\n");
return EXIT_FAILURE;
}
int n = atoi(argv[1]);
if (n < 2)
{
fprintf(stderr, "Number must be greater than 1!\n");
return EXIT_FAILURE;
}
print_hanoi(n);
return EXIT_SUCCESS;
}