C Program Print Stars Pyra
C Program Print Stars Pyra
Page 1 of 21
Search
Search this site: Home C Programming Tutorials C Graphics source codes
Search
Submit Program
Quick navigation
C Mouse Programs C program examples C Source codes Java programs graphics.h C graphics programs conio.h math.h dos.h
c programming examples
Hello world Print Integer Addition Add, subtract, multiply and divide Check vowel Leap year Add digits Factorial Add n numbers Swapping Reverse number Palindrome number Print Pattern Diamond
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 2 of 21
Prime numbers Find armstrong number Generate armstrong number Fibonacci series Print floyd's triangle Print pascal triangle Addition using pointers Maximum element in array Minimum element in array Linear search Binary search Reverse array Insert element in array Delete element from array Bubble sort Insertion sort Selection sort Add matrices Subtract matrices Transpose matrix Multiply two matrices Print string String length Compare strings Copy string Concatenate strings Reverse string Find palindrome Delete vowels C substring Sort a string Remove spaces Change case Swap strings Character's frequency Read file Copy files Merge two files List files in a directory
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 3 of 21
Delete file Random numbers Add complex numbers Print date Get ip address Shutdown computer
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<stdio.h> #include<conio.h> 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--;
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 4 of 21
Output:
Consider the pattern * ** *** **** ***** to print above pattern see the code below:
#include<stdio.h> 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:
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 5 of 21
Comments
#1 Guest : i need a code for this pattern.
12345 1234 123 12 1
26/08/2011 - 12:35
26/08/2011 - 17:06
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 6 of 21
20/08/2011 - 18:07
20/08/2011 - 21:29
#6 Guest : c language
diamond pattern of stars 20/08/2011 - 14:51
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 7 of 21
20/08/2011 - 08:43
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 8 of 21
18/08/2011 - 21:59
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 9 of 21
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
17/08/2011 - 19:15
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 10 of 21
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
16/08/2011 - 21:34
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 11 of 21
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
13/08/2011 - 17:53
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 12 of 21
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
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 13 of 21
13/08/2011 - 17:15
13/08/2011 - 17:29
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 14 of 21
for ( c = 1 ; c <= n ; c++ ) { for( k = 1 ; k <= c ; k++ ) printf("%d", c); printf("\n"); } return 0; }
12/08/2011 - 20:13
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 15 of 21
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
09/08/2011 - 17:21
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 16 of 21
if ( c > 1 && count < c) { printf("A"); count++; } } printf("\n"); space--; count = 1; } return 0; }
09/08/2011 - 21:46
08/08/2011 - 15:32
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 17 of 21
08/08/2011 - 15:29
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 18 of 21
int number = 1, 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 ", number); number++; } number = 1; printf("\n"); } return 0; }
07/08/2011 - 11:32
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 19 of 21
return 0; }
09/08/2011 - 11:19
08/08/2011 - 15:45
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 20 of 21
ABCD ABC AB A
DCBA CBA BA A
04/08/2011 - 20:46
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011
Page 21 of 21
http://www.programmingsimplified.com/c-program-print-stars-pyramid
16/09/2011