These program prints various different C program to print patterns of numbers and stars. These codes illustrate how to create various patterns using c programming. Most of these c programs involve usage of nested loops and space. A pattern of numbers, star or characters is a way of arranging these in some logical manner or they may form a sequence. Some of these patterns are triangles which have special importance in mathematics. Some patterns are symmetrical while other are not. Please see the complete page and look at comments for many different patterns

    *
   ***
  *****
 *******
*********

.We have shown five rows above, in the program you will be asked to enter the numbers of rows you want to print in the pyramid of stars.

[ad type=”banner”]

C programming code

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%20%0Aint%20main()%0A%7B%0A%20%20%20int%20row%2C%20c%2C%20n%2C%20temp%3B%0A%20%0A%20%20%20printf(%22Enter%20the%20number%20of%20rows%20in%20pyramid%20of%20stars%20you%20wish%20to%20see%20%22)%3B%0A%20%20%20scanf(%22%25d%22%2C%26n)%3B%0A%20%0A%20%20%20temp%20%3D%20n%3B%0A%20%0A%20%20%20for%20(%20row%20%3D%201%20%3B%20row%20%3C%3D%20n%20%3B%20row%2B%2B%20)%0A%20%20%20%7B%0A%20%20%20%20%20%20for%20(%20c%20%3D%201%20%3B%20c%20%3C%20temp%20%3B%20c%2B%2B%20)%0A%20%20%20%20%20%20%20%20%20printf(%22%20%22)%3B%0A%20%0A%20%20%20%20%20%20temp–%3B%0A%20%0A%20%20%20%20%20%20for%20(%20c%20%3D%201%20%3B%20c%20%3C%3D%202*row%20-%201%20%3B%20c%2B%2B%20)%0A%20%20%20%20%20%20%20%20%20printf(%22*%22)%3B%0A%20%0A%20%20%20%20%20%20printf(%22%5Cn%22)%3B%0A%20%20%20%7D%0A%20%0A%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

For more patterns or shapes on numbers and characters see comments below and also see codes on following pages:

  • Floyd triangle
  • Pascal triangle

Consider the pattern
*
**
***
****
*****

[ad type=”banner”]

to print above pattern see the code below:

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%20%0Aint%20main()%0A%7B%0A%20%20%20%20int%20n%2C%20c%2C%20k%3B%0A%20%0A%20%20%20%20printf(%22Enter%20number%20of%20rows%5Cn%22)%3B%0A%20%20%20%20scanf(%22%25d%22%2C%26n)%3B%0A%20%0A%20%20%20%20for%20(%20c%20%3D%201%20%3B%20c%20%3C%3D%20n%20%3B%20c%2B%2B%20)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for(%20k%20%3D%201%20%3B%20k%20%3C%3D%20c%20%3B%20k%2B%2B%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20printf(%22*%22)%3B%0A%20%0A%20%20%20%20%20%20%20%20printf(%22%5Cn%22)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Using these examples you are in a better position to create your desired pattern for yourself. Creating a pattern involves how to use nested loops properly, some pattern may involve alphabets or other special characters. Key aspect is knowing how the characters in pattern changes.

C pattern programs

Pattern

   *
  *A*
 *A*A*
*A*A*A*

C pattern program of stars and alphabets:

[pastacode lang=”c” manual=”%23include%3Cstdio.h%3E%0A%20%0Amain()%0A%7B%0A%20%20%20%20int%20n%2C%20c%2C%20k%2C%20space%2C%20count%20%3D%201%3B%0A%20%0A%20%20%20%20printf(%22Enter%20number%20of%20rows%5Cn%22)%3B%0A%20%20%20%20scanf(%22%25d%22%2C%26n)%3B%0A%20%0A%20%20%20%20space%20%3D%20n%3B%0A%20%0A%20%20%20%20for%20(%20c%20%3D%201%20%3B%20c%20%3C%3D%20n%20%3B%20c%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for(%20k%20%3D%201%20%3B%20k%20%3C%20space%20%3B%20k%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20printf(%22%20%22)%3B%0A%20%0A%20%20%20%20%20%20%20%20for%20(%20k%20%3D%201%20%3B%20k%20%3C%3D%20c%20%3B%20k%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20printf(%22*%22)%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(%20c%20%3E%201%20%26%26%20count%20%3C%20c)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22A%22)%3B%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20count%2B%2B%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%7D%20%20%20%20%0A%20%0A%20%20%20%20%20%20%20%20printf(%22%5Cn%22)%3B%0A%20%20%20%20%20%20%20%20space–%3B%0A%20%20%20%20%20%20%20%20count%20%3D%201%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Pattern:

        1
       232
      34543
     4567654
    567898765

[ad type=”banner”]

C program:

[pastacode lang=”c” manual=”%23include%3Cstdio.h%3E%0A%20%0Amain()%0A%7B%0A%20%20%20%20%20%20int%20n%2C%20c%2C%20d%2C%20num%20%3D%201%2C%20space%3B%0A%20%0A%20%20%20%20%20%20scanf(%22%25d%22%2C%26n)%3B%0A%20%0A%20%20%20%20%20%20space%20%3D%20n%20-%201%3B%0A%20%0A%20%20%20%20%20%20for%20(%20d%20%3D%201%20%3B%20d%20%3C%3D%20n%20%3B%20d%2B%2B%20)%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20num%20%3D%20d%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20for%20(%20c%20%3D%201%20%3B%20c%20%3C%3D%20space%20%3B%20c%2B%2B%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22%20%22)%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20space–%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20for%20(%20c%20%3D%201%20%3B%20c%20%3C%3D%20d%20%3B%20c%2B%2B%20)%0A%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22%25d%22%2C%20num)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20num%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20num–%3B%0A%20%20%20%20%20%20%20%20%20%20num–%3B%0A%20%20%20%20%20%20%20%20%20%20for%20(%20c%20%3D%201%20%3B%20c%20%3C%20d%20%3B%20c%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20printf(%22%25d%22%2C%20num)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20num–%3B%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20printf(%22%5Cn%22)%3B%0A%20%0A%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Categorized in: