www.programmingsimplified.com c program print stars pyra

21
Search Search this site: Search 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 Home C Programming Tutorials C Graphics source codes Submit Program Page 1 of 21 c program to print patterns of numbers and stars 16/09/2011 http://www.programmingsimplified.com/c-program-print-stars-pyramid

Upload: santosh-awghade

Post on 02-Mar-2015

1.442 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: Www.programmingsimplified.com c Program Print Stars Pyra

SearchSearch this site: Search

Quick navigationC 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•

Home C Programming Tutorials C Graphics source codes Submit Program

Page 1 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 2: Www.programmingsimplified.com c Program Print Stars Pyra

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

©2011 Programming Simplified

Home | Contact Us | Feedback | Links | Sitemap

Page 2 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 3: Www.programmingsimplified.com c Program Print Stars Pyra

Delete file•Random numbers•Add complex numbers

Print date•Get ip address•Shutdown computer

Home → C programming → Programs → c program to print patterns of numbers and stars

c program to print patterns of numbers and stars

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.

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

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--;  

Page 3 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 4: Www.programmingsimplified.com c Program Print Stars Pyra

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<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:

Page 4 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 5: Www.programmingsimplified.com c Program Print Stars Pyra

26/08/2011 - 12:35

26/08/2011 - 17:06

23/08/2011 - 22:04

Floyd triangle Pascal triangle

»

Comments

#1 Guest : i need a code for this pattern.

12345 1234 123 12 1

#2 adminPs : number pattern c code

#include<stdio.h>   main() { int n, c, k, space;   scanf("%d", &n);   space = 0;   for ( k = n ; k >= 1 ; k-- ) { for ( c = 1 ; c <= space ; c++ ) printf(" ");   space++;   for ( c = 1 ; c <= k ; c++) printf("%d", c);   printf("\n"); }   return 0; }

#3 Guest : thanks for the patterns it

thanks for the patterns it really helped me a lot.

Page 5 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 6: Www.programmingsimplified.com c Program Print Stars Pyra

20/08/2011 - 18:07

20/08/2011 - 21:29

20/08/2011 - 14:51

#4 Guest : i need code of this pattern

A B C D E F G H A B C D E F G A B C D E F A B C D E A B C D A B C A B A

#5 adminPs : c program for character pattern

#include<stdio.h>   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; }

#6 Guest : c language

diamond pattern of stars

Page 6 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 7: Www.programmingsimplified.com c Program Print Stars Pyra

26/08/2011 - 09:48

19/08/2011 - 17:38

20/08/2011 - 08:43

#7 adminPs : diamond pattern

Diamond pattern

#8 Guest : please help give me source code

1 01 010 1010 10101

#9 adminPs : c program to print pattern

#include<stdio.h>   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; }

#10 Guest : can you write code for this

p pr pro prog

Page 7 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 8: Www.programmingsimplified.com c Program Print Stars Pyra

18/08/2011 - 21:20

18/08/2011 - 21:59

18/08/2011 - 20:40

progr progra program

#11 adminPs : pattern for string

Just input the string and press enter, corresponding pattern will be printed.

#include<stdio.h> #include<string.h>   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; }

#12 Guest : c programming code

please get me the code of the c program to get the output as follows:

1 121 12321 1234321 123454321

#13 adminPs : c program to print number pattern

#include<stdio.h>  

Page 8 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 9: Www.programmingsimplified.com c Program Print Stars Pyra

18/08/2011 - 22:10

17/08/2011 - 19:15

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; }

#14 Guest : c++ code to print pattern

1 1 12 21 12321

#15 adminPs : c++ code to print pattern

#include<iostream>   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;

Page 9 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 10: Www.programmingsimplified.com c Program Print Stars Pyra

18/08/2011 - 17:14

16/08/2011 - 21:34

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; }

#16 Guest : may i get its source code

1 232 34543 4567654 567898765

#17 adminPs : c code for number pattern

#include<stdio.h>   main() { int n, c, d, num = 1, space;   scanf("%d",&n);   space = n - 1;

Page 10 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 11: Www.programmingsimplified.com c Program Print Stars Pyra

18/08/2011 - 17:17

13/08/2011 - 17:53

  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 Guest : how to print this pattern

******* ***S*** **SSS** *SSSSS*

where S represents Space

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

#19 adminPs : c code for star pattern

#include<stdio.h>   main() { int n, c, k, space, r;   printf("Enter number of rows\n"); scanf("%d",&n);

Page 11 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 12: Www.programmingsimplified.com c Program Print Stars Pyra

13/08/2011 - 19:17

22/08/2011 - 09:47

22/08/2011 - 13:01

  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; }

#20 Guest : Doubt

In the program, instead of the statement space=2*k-1; , can we go for the statement, space+=2; ?

#21 adminPs : sure

You can use but don't forget to initialize variable space to one.

#22 Guest : code

guys i need the code for this

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

Page 12 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 13: Www.programmingsimplified.com c Program Print Stars Pyra

13/08/2011 - 17:15

13/08/2011 - 17:29

12/08/2011 - 18:48

#23 adminPs : star pattern source code

#include<stdio.h>   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; }

#24 Guest : i need a program to draw the below patern

1 22 333 4444 55555

#25 adminPs : number pattern source code

#include<stdio.h>   main() { int n, c, k;   printf("Enter number of rows\n"); scanf("%d",&n);  

Page 13 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 14: Www.programmingsimplified.com c Program Print Stars Pyra

12/08/2011 - 20:13

11/08/2011 - 20:14

12/08/2011 - 07:31

09/08/2011 - 21:46

for ( c = 1 ; c <= n ; c++ ) { for( k = 1 ; k <= c ; k++ ) printf("%d", c);   printf("\n"); }   return 0; }

#26 Guest : need help

i need program to print the following pattern ::

1 2 3 4 5 6 7 8 9 10

#27 adminPs : Floyd's triangle

This pattern is Floyd's triangle see Floyd's Triangle code.

#28 Guest : need a code in c to print below pattern

i want a code to print stars like below

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

#29 adminPs : stars pattern using c programming

#include<stdio.h>   main() {

Page 14 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 15: Www.programmingsimplified.com c Program Print Stars Pyra

09/08/2011 - 22:38

09/08/2011 - 17: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; }

#30 Guest : GET ME THE CODE OF A PROGRAM

GET ME THE CODE OF A PROGRAM TO GET OUTPUT AS FOLLOWS:

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

#31 adminPs : code to print pattern of stars and numbers

#include<stdio.h>   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("*");

Page 15 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 16: Www.programmingsimplified.com c Program Print Stars Pyra

09/08/2011 - 21:46

08/08/2011 - 13:18

08/08/2011 - 15:32

  if ( c > 1 && count < c) { printf("A"); count++; } }   printf("\n"); space--; count = 1; } return 0; }

#32 Guest : codes for pyramid of stars.

i need this pattern * * * * * * * * * * *

#33 adminPs : code for pattern

#include<stdio.h>   main() { int n, c, k, temp;   printf("Enter number of rows\n"); scanf("%d",&n);   temp = n;   for ( c = 1 ; c <= n ; c++ ) { for( k = 1 ; k <= temp ; k++ ) printf("* ");   temp--;   printf("\n"); }   return 0; }

Page 16 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 17: Www.programmingsimplified.com c Program Print Stars Pyra

08/08/2011 - 13:01

08/08/2011 - 15:29

06/08/2011 - 13:59

#34 Guest : Please post the code for this

* * * * * * * * * *

Please post the code to print the following shape as soon as possible.

#35 adminPs : code

#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; }

#36 Guest : please help

i need c program to print no in this format 1 1 2 1 2 3 1 2 3 4

#37 adminPs : source code

#include<stdio.h>   main() {

Page 17 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 18: Www.programmingsimplified.com c Program Print Stars Pyra

07/08/2011 - 11:32

08/08/2011 - 22:02

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; }

#38 Guest : Thanks giving and suggestion

Thanks for the pyramid program!! Please help me in the same problem. Can't we directly print k directly instead of number(in 2nd inner for loop)?

I have tried it but output gives garbage values!!why does this happen? Please help me!

#39 adminPs : number pattern code

You are right there is no need to use variable number you can use k directly as in 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("%d ", k);   printf("\n"); }  

Page 18 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 19: Www.programmingsimplified.com c Program Print Stars Pyra

09/08/2011 - 11:19

05/08/2011 - 11:53

08/08/2011 - 15:45

return 0; }

#40 Guest : i need a code in C to print pyramid of stars

i need a code in C to print stars like this

* * * * * * * * * *

please help?

#41 adminPs : c code to print pattern

#include<stdio.h>   main() { int n, c, k = 2, j;   printf("Enter number of rows\n"); scanf("%d",&n);   for ( j = 1 ; j <= n ; j++ ) { for ( c = 1 ; c <= 2*n-k ; c++) printf(" ");   k = k + 2;   for ( c = 1 ; c <= j ; c++) printf("* ");   printf("\n"); }   getch(); return 0; }

#42 Guest : GET THE SOURCE CODE

GET ME THE CODE OF A PROGRAM TO GET OUTPUT AS FOLLOWS:

ABCDEFEDCBA ABCDE EDCBA

Page 19 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 20: Www.programmingsimplified.com c Program Print Stars Pyra

04/08/2011 - 20:46

04/08/2011 - 11:13

11/07/2011 - 00:06

11/07/2011 - 00:02

29/06/2011 - 13:52

10/07/2011 - 23:00

29/06/2011 - 13:24

ABCD DCBA ABC CBA AB BA A A

#43 Guest : thank you

i was looking for it

#44 Guest : problem

Sir i have some problem regarding loop. Sometime i cant understand how loop is working and i cant implement it right too. How can i improve my understanding skill, please help if possible.

#45 Guest : Compliment

sir your site is awesome....... i really like it and learned lot from it.:)

#46 Guest : help

can you explain the code, the loop coding.

#47 adminPs : program explanation

The first loop is for number of rows, second for printing required spaces and last one to print stars ( 2*(row number) - 1 ).

#48 Guest : thanks

i am searching same and i got

Page 20 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid

Page 21: Www.programmingsimplified.com c Program Print Stars Pyra

Page 21 of 21c program to print patterns of numbers and stars

16/09/2011http://www.programmingsimplified.com/c-program-print-stars-pyramid