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:
#includemain() { 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
#includemain() { 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
#includemain() { 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
#includemain() { 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
#includeusing 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
#includemain() { 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
#includemain() { 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
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");
i want a code to print:
ReplyDelete146
25
3
without goto,using nested loops
#include
DeleteMain()
{
Int a,b,c,n=146,I;
a=n%10;
b=(n/10)%10;
C=n/100;
For(I=2;I<=2;I++)
{
Printf("/n%d%d",a-b,a-c);
}
Printf("/n%d",b-c);
}
I want a code
Deletea
bc
def
ghij
klmno
Frnds help me
Find various number and star patterns programs in C.
DeleteNumber pattern programs with logic in C.
Star pattern programs with logic in C.
i want code for
ReplyDelete1
12
123
1234
by using only one looping statement
#include
Deletevoid main()
{
int r,c
clrscr();
for{r=1;r<=4;r++)
{
for(c=1;c<=r;c++)
{
printf("%d",c);
}
printf("\n");
}
getch();
}
i want code for reverse a linked list.......
Deletehey any one know how to print star pattern in terms of no's.
ReplyDeleteGive me a simple program with loop for this:
ReplyDelete____1
__12 21
_123 321
1234 4321
i want to knw the logic behind every progrAM??
ReplyDeleteplease give me the program for getting the output as
ReplyDelete1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
dghhccvv
Delete*
ReplyDelete* *
* *
* *
plz give me c program code
please give me the program for output of following pattern
ReplyDelete****5
***4
**3
*2
1
#include
Delete#include
int main()
{
int i,j;
clrscr();
for(i=5;i>=1;i--)
{
for(j=1;j<=i-1;j++)
{
printf("*");
if(j==i-1)
printf("%d",i);
}
if(i==1)
printf("%d",i);
printf("\n");
}
getch();
}
hey any 1 help for this program
ReplyDelete1
3
5 6
8 9 10
Give me the codes for following patterns
ReplyDelete(a)
1
**
123
****
12345
******
(b)
1
1*
1*3
1*3*
1*3*5
1*3*5*
(c)
1
*2
1*3
*2*4
1*3*5
*2*4*6
Please help me anyone....
hi good blog..
ReplyDeletevisit proud2beengineer.com for more complex programs
This comment has been removed by the author.
ReplyDeletehi pls give me a code that will print this
ReplyDelete5
4 4
3 3 3
2 2 2 2
1 1 1 1 1
for(i=5;i>=1;i--)
Delete{
for(j=5;j>=i;j--)
{
printf("%d",i);
}
printf("\n");
}
1234
ReplyDelete123
12
1
*
ReplyDelete*+*
*++*
*+++*
*++++*
i need code for
ReplyDelete10
98
765
4321
1
ReplyDelete3*2
4*5*6
10*9*8*7
11*12*13*14*15
tell me the code for this plz
#include
Deletevoid printPattern(int n)
{
int k,i;
char pattern[20]="",next[20]="";
k=printPattern(n-1);
if(n%2==1)
{
for(i=1;i<=n;i++)
{
k++;
sprintf(pattern,"%d*",k);
}
sprintf(next,"%d",k);
strcat(pattern,next);
printf("%s\n",pattern);
return k;
}
else
{
beginIndex=k+n;
lastIndex=k+1;
for(i=beginIndex;i>lastIndex;i--)
{
sprintf(next,"%d*",i);
strcat(pattern,next);
}
sprintf(next,"%d",i);
strcat(pattern,next);
printf("%s\n",pattern);
return beginIndex;
}
if(n==1)
{
printf("%d",n);
return 1;
}
}
*****
ReplyDelete#
****
##
***
###
**
####
*
#####
please get me program for dis...
This comment has been removed by the author.
ReplyDeletei want code for
ReplyDelete1
2 7
3 8 12
4 9 13 16
5 10 14 17 19
6 11 15 18 20 21
class Pattern1
Delete{
public static void main(String[] args)
{
int n=6;
for(int i=0;i<=n-1;i++)
{
int a=0;
a=i+1;
for(int j=0;j<=i;j++)
{
System.out.print(a);
a=a+n-j-1;
System.out.print(" ");
}
System.out.println();
}
}
}
i want to print this pattern
ReplyDelete1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
class Pattern1
Delete{
public static void main(String[] args)
{
int n=5;
for(int i=1;i<=n;i++)
{
for(int j=i;j>=1;j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
I want to print this
ReplyDelete4
34
234
1234
*
ReplyDelete* * *
* * * * *
* * *
*
Write a program to print the following pattern based on user input.
ReplyDeleteFor eg: If N=4 the code should print the following pattern.
1*2*3*4*17*18*19*20
--5*6*7*14*15*16
----8*9*12*13
------10*11
Again if N=5 the code should print
1*2*3*4*5*26*27*28*29*30
--6*7*8*9*22*23*24*25
----10*11*12*19*20*21
------13*14*17*18
--------15*16
For N=2 the pattern will be
1*2*5*6
--3*4
Do not use blank spaces instead of dashes.
I could never frame the code even after lot of trying.Please help.
1
ReplyDelete* *
2 3 4
1
ReplyDelete* *
2 3 4
dssdds
ReplyDeleteWrite a program to print a swastic
ReplyDeleteWrite a program to print tha digit in shortest form to a number?
ReplyDelete*
ReplyDelete**
**
i want a code for
ReplyDeleteA
A B
A B C
A B C D
Program for
ReplyDelete1
23
345
4567
Check this c program, it will print your pattern natural number pattern
DeleteProgram for
ReplyDelete1
23
345
4567
Program for
ReplyDelete1
23
345
4567
i want for
ReplyDelete*******
*** ***
** **
* *
** **
*** ***
*******
PRINTING THE PATTERNS
ReplyDeleteA
A B
A B C
A B C D
A B C D E
Print this pattern in C please (not c++)
ReplyDelete* *
* *
* *
* *
* *
* *
* *
This comment has been removed by the author.
ReplyDeletei want this type of code
ReplyDelete12345
6 7 8
10 11 12
13 14 15 16 17
1 2 3 4 5
ReplyDelete6 7 8
10 9 12
13 14 15 16 17
write a program to print
ReplyDelete1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
1
ReplyDeleteAB
234
CDEF
1
ReplyDeleteAB
234
CDEF
#include
Deleteint main()
{
int num,r,c;
static int i=1;
static char ch='A';
printf("Enter no. of rows : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
for(c=1; c<=r; c++)
{
if(r%2==0)
printf(" %c",ch++);
else
printf(" %d",i++);
}
printf("\n");
}
getch();
return 0;
}
A D G J M
ReplyDelete19 17 13 11
P S V
7 5
Y
A D G J M
ReplyDelete19 17 13 11
P S V
7 5
Y
A D G J M
ReplyDelete19 17 13 11
P S V
7 5
Y
1
ReplyDelete2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
i want java program
ReplyDeleteC++ Program to Print Triangle of Stars
ReplyDeletePrint triangle of stars is depend of two concept, first you need outer loop for line break and inner loop for increment and decrements.
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.
plece give me code to print this
ReplyDelete*
* *
* * *
* * * *
* * * * *
can we write a program for tringular pattern with only 1 for loop
ReplyDeletepplace give me code to print this
ReplyDelete1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
please give me the program for output of following pattern
ReplyDelete****4
***3
**2
*1
Give a program for
ReplyDelete1
1 2
1 2 3
1 2 3 4
Give a program for
ReplyDelete1
1 2
1 2 3
1 2 3 4
Hi
ReplyDeleteGive me coad
123
456
789
Hi give me coad
ReplyDelete123
4 6
789
Hi
ReplyDeleteGive me code
1*2
*5*
7*8
Hi
ReplyDeleteGive me code
1*2
*5*
7*8
ReplyDelete*2*
3*4
*7*
ReplyDelete*2*
3*4
*7*
send me the programme to print pattern M
ReplyDeletehi pliz gv me a code that wl print
ReplyDelete1
3 1
5 3 1
7 5 3 1
9 5 7 3 1
1
ReplyDelete3 1
5 3 1
7 5 3 1
9 7 5 3 1
I want c program for this output:
ReplyDeleteABCDDCBA
ABC CBA
AB BA
A A
i want code for this output:
ReplyDelete333
313
323
333
can i get
ReplyDelete1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
can i get
ReplyDelete1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
can some one plz print the output as
ReplyDelete123456789
13579
157
17
7
1
ReplyDeleteA b
3 4 5
c d e f
Plz anyone help me
1
ReplyDelete01
101
0101
10101
plz anyone help me