Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

โปรแกรม C แสดงตัวเลขแบบเกลียว (Spiral Pattern)

รูปแบบเกลียวสำหรับตัวเลข:

โปรแกรม C แสดงตัวเลขแบบเกลียว (Spiral Pattern)

ตรรกะพิมพ์ตัวเลขแบบเกลียว:

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