Home > Backend Development > C++ > body text

C program to display numbers in X form

WBOY
Release: 2023-08-26 20:05:06
forward
677 people have browsed it

C program to display numbers in X form

Refer to the algorithm below and write a C program to display X-shaped numbers.

Algorithm

Step 1: Start
Step 2: Declare variables
Step 3: Read number of rows
Step 4: for loop satisfies<ul class="list"><li>if(i==j || i+j==rows-1)<ul class="list"><li>print i+1</li></ul></li><li>Print " "</li></ul>Step 5: Print new line
Step 6: Stop
Copy after login

The logic to print numbers in X pattern is as follows −

for(i=0;i<rows;i++){
   for(j=0;j<rows;j++){
      if(i==j || i+j==rows-1){
         printf("%d",i+1);
      }else{
         printf(" ");
      }
   }
   printf("</p><p>");
}
Copy after login

Program

Following is the C program to display the numbers in X pattern −

#include<stdio.h>
main(){
   int i,j,rows;
   printf("Enter number of rows:</p><p>");
   scanf("%d",&rows);
   for(i=0;i<rows;i++){
      for(j=0;j<rows;j++){
         if(i==j || i+j==rows-1){
            printf("%d",i+1);
         }else{
            printf(" ");
         }
      }
      printf("</p><p>");
   }
}
Copy after login

Output

When the above program is executed, it produces the following result−

Enter number  of rows:10
1           1
  2        2
    3    3
      4 4
       55
       66
      7 7
    8     8
  9        9
10          10
Copy after login

The above is the detailed content of C program to display numbers in X form. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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!