c program to print patterns, pyramids of numbers and stars
http://manxinbuon.blogspot.in/2011/08/c-program-to-print-patterns-pyramids-of.html
These
program prints various different 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.
Output:
Consider the pattern
*
**
***
****
*****
to print above pattern see the code below:
For more patterns or shapes on numbers and characters see comments below and also see codes on following pages:
Floyd triangle
Pascal triangle
* *** ***** ******* *********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.
C code
#include #include main() { int row, c, n, temp; printf("Enter the number of rows in pyramid of stars you wish to see "); scanf("%d",&n); temp = n; for ( row = 1 ; row <= n ; row++ ) { for ( c = 1 ; c < temp ; c++ ) printf(" "); temp--; for ( c = 1 ; c <= 2*row - 1 ; c++ ) printf("*"); printf("\n"); } getch(); return 0; }
Output:
Consider the pattern
*
**
***
****
*****
to print above pattern see the code below:
#include main() { int n, c, k; printf("Enter number of rows\n"); scanf("%d",&n); for ( c = 1 ; c <= n ; c++ ) { for( k = 1 ; k <= c ; k++ ) printf("*"); printf("\n"); } return 0; }
For more patterns or shapes on numbers and characters see comments below and also see codes on following pages:
Floyd triangle
Pascal triangle
Comments
#2 : c program for character pattern
#include main() { char ch = 'A'; int n, c, k, space = 0; scanf("%d", &n); for ( k = n ; k >= 1 ; k-- ) { for ( c = 1 ; c <= space ; c++) printf(" "); space++; for ( c = 1 ; c <= k ; c++ ) { printf("%c ", ch); ch++; } printf("\n"); ch = 'A'; } return 0; }
20/08/2011 - 21:29
c program to print pattern
#include main() { int n, c, k, num = 1; scanf("%d", &n); for ( c = 1 ; c <= n ; c++ ) { for ( k = 1 ; k <= c ; k++ ) { printf("%d", num); if ( num == 0 ) num = 1; else num = 0; } printf("\n"); } return 0; }
20/08/2011 - 08:43
: pattern for string
Just input the string and press enter, corresponding pattern will be printed.
#include #include main() { char string[100]; int c, k, length; printf("Enter a string\n"); gets(string); length = strlen(string); for ( c = 0 ; c < length ; c++ ) { for( k = 0 ; k <= c ; k++ ) { printf("%c", string[k]); } printf("\n"); } return 0; }
18/08/2011 - 21:59
c program to print number pattern
#include main() { int n, c, k, x = 1; scanf("%d", &n); for ( c = 1 ; c <= n ; c++ ) { for ( k = 1 ; k <= c ; k++ ) { printf("%d", x); x++; } x--; for ( k = 1 ; k <= c - 1 ; k++ ) { x--; printf("%d", x); } printf("\n"); x = 1; } return 0; }
18/08/2011 - 22:10
c++ code to print pattern
#include using namespace std; main() { int n, k, c, space, x, num = 1; cin >> n; x = n; for ( k = 1 ; k <= n ; k++ ) { for ( c = 1 ; c <= k ; c++ ) { cout << num; num++; } num--; for ( c = 1 ; c <= 2*x - 3 ; c++ ) cout << " "; x--; if ( k != n ) { for ( c = 1 ; c <= k ; c++ ) { cout << num; num--; } } else { num--; for ( c = 1 ; c <= k - 1 ; c++ ) { cout << num; num--; } } printf("\n"); num = 1; } return 0; }
18/08/2011 - 17:14
c code for number pattern
#include main() { int n, c, d, num = 1, space; scanf("%d",&n); space = n - 1; for ( d = 1 ; d <= n ; d++ ) { num = d; for ( c = 1 ; c <= space ; c++ ) printf(" "); space--; for ( c = 1 ; c <= d ; c++ ) { printf("%d", num); num++; } num--; num--; for ( c = 1 ; c < d ; c++) { printf("%d", num); num--; } printf("\n"); } return 0; }
18/08/2011 - 17:17
#14 : c code for star pattern
#include main() { int n, c, k, space, r; printf("Enter number of rows\n"); scanf("%d",&n); space = 1; r = n-1; for( c = 1 ; c <= 2*n - 1 ; c++ ) printf("*"); printf("\n"); for ( k = 2 ; k <= n ; k++ ) { for( c = 1 ; c <= r ; c++ ) printf("*"); for ( c = 1 ; c <= space ; c++ ) printf(" "); space = 2*k-1; for( c = 1 ; c <= r ; c++ ) printf("*"); r--; printf("\n"); } return 0; }
13/08/2011 - 19:17
You can use but don't forget to initialize variable space to one.
22/08/2011 - 13:01
star pattern source code
#include
main()
{
int n, c, k, space;
printf("Enter number of rows\n");
scanf("%d",&n);
space = n;
for ( k = 1 ; k <= n ; k++ )
{
for ( c = 1 ; c < space ; c++ )
printf(" ");
space--;
for( c = 1 ; c <= k ; c++ )
printf("*");
printf("\n");
}
return 0;
}
13/08/2011 - 17:29
#20 : number pattern source code
#include
main()
{
int n, c, k;
printf("Enter number of rows\n");
scanf("%d",&n);
for ( c = 1 ; c <= n ; c++ )
{
for( k = 1 ; k <= c ; k++ )
printf("%d", c);
printf("\n");
}
return 0;
}
12/08/2011 - 20:13
stars pattern using c programming
#include
main()
{
int n, c, k;
printf("Enter number of rows\n");
scanf("%d",&n);
for ( c = 1 ; c <= n ; c++)
{
for ( k = 1 ; k <= c ; k++ )
printf("*");
printf("\n");
}
for ( c = n - 2 ; c >= 0 ; c-- )
{
for ( k = c ; k >= 0 ; k-- )
printf("*");
printf("\n");
}
return 0;
}
09/08/2011 - 22:38
code to print pattern of stars and numbers
#include
main()
{
int n, c, k, space, count = 1;
printf("Enter number of rows\n");
scanf("%d",&n);
space = n;
for ( c = 1 ; c <= n ; c++)
{
for( k = 1 ; k < space ; k++)
printf(" ");
for ( k = 1 ; k <= c ; k++)
{
printf("*");
if ( c > 1 && count < c)
{
printf("A");
count++;
}
}
printf("\n");
1111111
ReplyDelete1A1AAAA
1AA1AAA
1AAA1AA
1AAAA1A
1AAAAA1
1111111
C++ Program to Print number pattern
ReplyDeleteIn c++ we can easily print any number patter just we need two for loop one inner loop for increment and decrements and other for line break.