Home > Backend Development > C++ > body text

C program to represent numbers in spiral pattern

王林
Release: 2023-09-08 19:05:01
forward
996 people have browsed it

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

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

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!