ProgrammingModerateCompetitive coding (CP)

How many moves does this recursive Towers of Hanoi program make for 4 disks? Compute with the program.

How many moves does this recursive Towers of Hanoi program make for 4 disks? Compute with the program.
#include <stdio.h>
int moves = 0;
void hanoi(int n, char a, char b, char c) {
    if (n == 0) return;
    hanoi(n - 1, a, c, b);
    moves++;
    hanoi(n - 1, c, b, a);
}
int main(void) {
    hanoi(4, 'A', 'B', 'C');
    printf("%d", moves);
}
  1. A. 15
  2. B. 16
  3. C. 14
  4. D. 8

Correct answer: A. 15

Towers of Hanoi with n disks takes 2^n - 1 moves, so 4 disks take 15 moves.

More C Programming questions

Exams that ask this topic