Home >Backend Development >C++ >C program to represent numbers in spiral pattern
The spiral pattern representing numbers is as follows -
The logic of printing numbers in spiral pattern is as follows-
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("</p><p>"); }
Following is the C program to represent spiral numbers -
#include<stdio.h> main(){ int i,rows,k=1; printf("Enter number of Rows for Spiral Pattern</p><p>"); 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("</p><p>"); } }
When the above program is executed, the following result is produced -
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
The above is the detailed content of C program to represent numbers in spiral pattern. For more information, please follow other related articles on the PHP Chinese website!