Home >Backend Development >C++ >C program to represent numbers in spiral pattern

C program to represent numbers in spiral pattern

王林
王林forward
2023-09-08 19:05:011146browse

The spiral pattern representing numbers is as follows -

C program to represent numbers in spiral pattern

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>");
}

Program

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>");
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete