รูปแบบเกลียวเพื่อแสดงตัวเลขแสดงอยู่ด้านล่าง -

ตรรกะที่ใช้พิมพ์ตัวเลขในรูปแบบเกลียวมีดังนี้ -
for(i=1;i<=rows*2;i+=2){
if(k%2==1){
printf("%3d %3d",i,i+1);
k++;
}else{
printf("%3d %3d",i+1,i);
k++;
}
printf("\n");
} โปรแกรม
ต่อไปนี้เป็นโปรแกรม C สำหรับแสดงตัวเลขในรูปแบบเกลียว -
#include<stdio.h>
main(){
int i,rows,k=1;
printf("Enter number of Rows for Spiral Pattern\n");
scanf("%d",&rows);
for(i=1;i<=rows*2;i+=2){
if(k%2==1){
printf("%3d %3d",i,i+1);
k++;
}else{
printf("%3d %3d",i+1,i);
k++;
}
printf("\n");
}
} ผลลัพธ์
เมื่อโปรแกรมข้างต้นทำงาน มันจะให้ผลลัพธ์ดังต่อไปนี้ −
Enter number of Rows for Spiral Pattern 10 1 2 4 3 5 6 8 7 9 10 12 11 13 14 16 15 17 18 20 19