Javatpoint Logo
Javatpoint Logo

Number pattern program in C

A key component of learning programming is developing number pattern programs. They not only help you learn numerous programming principles, but they also improve your problem-solving abilities. In this article, we'll go into the world of C programming's number pattern programs. We'll go over the syntax, give thorough justifications, and show you examples and their associated results. So, let's begin and explore the wonders of C's number patterns. We are going to look at various fundamental number patterns along with the associated C code, syntax, examples, and outputs.

Pattern 1: Square Pattern

One of the most basic number patterns is the square pattern. Printing a square with numbers starting from 1 up to N is required, where N is the total number of rows and columns.

Example:

Output:

Enter the number of rows and columns: 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

Explanation:

In this example, the user is asked how many rows and columns there are. While the inner loop manages the number of columns, the outer loop manages the number of rows. Each time the inner loop iterates, the column number represented by the value of 'j' is printed.

Pattern 2: Pyramid Pattern

The pyramid pattern includes printing an incremental pyramidal structure.

Example:

Output:

Enter the number of rows: 5
    1
   1 2
  1 2 3
 1 2 3 4
1 2 3 4 5

Explanation:

The number of rows must be entered by the user. Printing the spaces necessary for the numerals to be correctly aligned takes place in the first inner loop. On each iteration of the outer loop, the second inner loop prints the integers from 1 to 'i' in ascending order.

Pattern 3: Diamond Pattern

The diamond pattern entails printing a numerically constructed diamond shape.

Example:

Output:

Enter the number of rows: 5
    1
   1 2
  1 2 3
 1 2 3 4
1 2 3 4 5
 1 2 3 4
  1 2 3
   1 2
    1

Explanation:

The code for the pyramid pattern can be extended to create the diamond pattern. The lower half of the diamond is printed using a reverse loop after the pyramid shape has been printed.

Pattern 4: Pascal's Triangle

A triangle-shaped array of binomial coefficients is known as Pascal's Triangle. The two numbers immediately above it add up to each number in the triangle.

Output:

Enter the number of rows: 5
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1

Explanation:

We create a function called binomialCoeff that employs a recursive process to determine the binomial coefficient. The user is prompted to specify the number of rows in the main program. The binomialCoeff function is used by the program to construct and output Pascal's Triangle using two nested loops.

Pattern 5: Floyd's Triangle

Each row of Floyd's Triangle contains one more natural number than the row before it. It is a right-angled triangular array of natural numbers.

Example:

Output:

Enter the number of rows: 5
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15

Explanation:

The number of rows must be entered by the user. The program generates and prints Floyd's Triangle using two stacked loops. The current number to be printed is tracked by the variable num.

Pattern 6: Spiral Pattern

The spiral pattern entails spirally printing a square matrix.

Example:

Output:

1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

Explanation:

The program has a function called printSpiral that spirally prints the input matrix. It employs four for loops, each of which represents a side of the square matrix, combined with a while loop. The limits of the spiral are tracked using the loop variables k, l, and N.

Pattern 7: Hollow Square Pattern

The hollow square pattern entails printing a square of numbers with an asterisk (*) border and a number-filled interior.

Output:

Enter the number of rows and columns: 5
* * * * * 
* 1 2 3 * 
* 1 2 3 * 
* 1 2 3 * 
* * * * *

Explanation:

This program prints a square of integers that is comparable to the square pattern. Instead of printing the number, it instead publishes an asterisk (*) if the current position (first row, final row, first column, or last column) is on the square's edge.

Conclusion:

Finally, number pattern programs in C offer a beneficial chance to develop programming expertise and problem-solving skills. In this blog post, we looked at a variety of patterns, from the most fundamental, such as the square, pyramid, and diamond patterns to the most complex-such as the Pascal's triangle, Floyd's triangle, hollow square pattern, and Fibonacci series. We learned more about how numbers may be ordered and changed to produce aesthetically pleasing patterns by comprehending the syntax, putting the code into practice, and examining the results.

These programs encourage creativity and rational thought in addition to helping students understand loops, conditionals, and recursive functions more thoroughly. Programmers can build a solid basis for tackling increasingly difficult programming issues and expand their knowledge of the language's possibilities by becoming experts at number pattern programs in C. To fully develop your programming abilities, continue to explore and experiment with number patterns.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA