Home > Backend Development > C++ > body text

Write a program to print a number pattern in C

PHPz
Release: 2023-09-05 20:13:07
forward
1163 people have browsed it

Program Description

Number patterns are sequences of numbers created according to rules called pattern rules. Pattern rules can use one or more mathematical operations to describe the relationship between consecutive numbers in a sequence.

Pattern Example

Pattern 1

1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
Copy after login

Pattern 2

        1
      1 2 3
    1 2 3 4 5
  1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9
  1 2 3 4 5 6 7
    1 2 3 4 5
      1 2 3
        1
Copy after login

Algorithm

<strong>Pattern 1:</strong>
i stands for rows and j stands for columns.
5 stands for making pattern for 5 Rows and Columns
Loop for each Row (i)
K is initialized to i
Loop for each Column (j)
Do the Pattern for the current Column (j)
Display the Value of K
Reinitialize the Value of K = k + 5 - j
<strong>Pattern 2:</strong>
First Row: Display 1
Second Row: Display 1,2,3
Third Row: Display 1,2,3,4,5
Fourth Row: Display 1,2,3,4,5,6,7
Fifth Row: Display 1,2,3,4,5,6,7,8,9
Display the same contents from 4th Row till First Row below the fifth Row.
Copy after login

Example

/* Program to print Numeric Pattern */
#include<stdio.h>
int main(){
   int i,j,k;
   printf("Numeric Pattern 1");
   printf("</p><p>");
   printf("</p><p>");
   for(i=1;i<=5;i++){
      k = i;
      for(j=1;j<=i;j++){
         printf("%d ", k);
         k += 5-j;
      }
      printf("</p><p>");
   }
   printf("</p><p>");
   printf("Numeric Pattern 2");
   printf("</p><p>");
   printf("</p><p>");
   for(i = 1;i<=5;i++){
      for(j = i;j<5;j++){
         printf(" ");
      }
      for(k = 1;k<(i*2);k++){
         printf("%d",k);
      }
      printf("</p><p>");
   }
   for(i = 4;i>=1;i--){
      for(j = 5;j>i;j--){
         printf(" ");
      }
      for(k = 1;k<(i*2);k++){
         printf("%d",k);
      }
      printf("</p><p>");
   }
   getch();
   return 0;
}
Copy after login

Output

Write a program to print a number pattern in C

The above is the detailed content of Write a program to print a number pattern in C. 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