In các cặp số liên tiếp theo hình xoắn ốc: 1 2, 4 3, 5 6, 8 7...
Logic
for(i=1; i<=rows*2; i+=2)
if(k%2==1) printf("%3d %3d", i, i+1);
else printf("%3d %3d", i+1, i);
k++; printf("\n");
Chương trình C
#include <stdio.h>
main() {
int i, rows, k = 1;
printf("Nhập số hàng: ");
scanf("%d", &rows);
for (i = 1; i <= rows*2; i += 2) {
if (k % 2 == 1) printf("%3d %3d", i, i+1);
else printf("%3d %3d", i+1, i);
k++;
printf("\n");
}
}
Kết quả
Nhập số hàng: 10 1 2 4 3 5 6 8 7 9 10 12 11 13 14 16 15 17 18 20 19