[go: up one dir, main page]

0% found this document useful (0 votes)
78 views19 pages

Pattern Printing in C - INFORTEC International

The document provides 15 examples of C programs that print various pyramid and number patterns. Each example includes the output pattern, the C code to generate that pattern, and a brief description. The patterns include squares of asterisks, increasing numbers, combinations of letters, and a pattern that alternates between 0 and 1 based on the sum of row and column indexes. The document is intended to demonstrate different approaches for printing patterns using loops and conditional statements in C programming.

Uploaded by

IT Hub SL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views19 pages

Pattern Printing in C - INFORTEC International

The document provides 15 examples of C programs that print various pyramid and number patterns. Each example includes the output pattern, the C code to generate that pattern, and a brief description. The patterns include squares of asterisks, increasing numbers, combinations of letters, and a pattern that alternates between 0 and 1 based on the sum of row and column indexes. The document is intended to demonstrate different approaches for printing patterns using loops and conditional statements in C programming.

Uploaded by

IT Hub SL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

C Programming -Mod-12

Pattern Printing in C -INFORTEC


INTERNATIONAL
1. Output :

*****
*****
*****
*****
* * * * *_

Program :

/* Program to print pyramid pattern in C : Pattern 1

#include <stdio.h>
#include <conio.h>
void main()
{
int i,j;
clrscr();
for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
{
printf(" * ");
}
printf("\n");
}
getch();
}

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12

2. Output :

*
**
***
****
* * * * *_

Program :

/* Program to print pyramid pattern in C : Pattern 2

#include <stdio.h>
#include <conio.h>
void main()
{
int i,j;
clrscr();
for(i=0; i<5; i++)
{
for(j=0; j<=i; j++)

{
printf(" * ");
}
printf("\n");
}
getch();
}

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12

3. Output :

*
**
***
****
* * * * *_

Program :

/* Program to print pyramid pattern in C : Pattern 3

#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=1; i<=5; i++)
{
for(j=5; j>=i; j--)
{
printf(" ");
}
for(k=1; k<=i; k++)
{
printf("*");
}
printf("\n");
}
getch();
}

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12

4. Output :

*****
****
***
**
*_

Program :

/* Program to print pyramid pattern in C : Pattern 4

#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,k,samp=1;
clrscr();
for(i=5; i>=1; i--)
{
for(k=samp; k>=0; k--)
{
printf(" "); // only 1 space
}
for(j=i; j>=1; j--)
{
printf("*");
}
samp = samp + 1;
printf("\n");
}
getch();
}

or

#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,k;

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12
clrscr();
for(i=1; i<=5; i++)
{
for(j=5; j>=i; j--)
{
printf("*");
}
for(k=1; k<=i; k++)
{
printf(" ");
}
printf("\n");
}
getch();
}

5. Output :

*****
****
***
**
*_

Program :

/* Program to print pyramid pattern in C : Pattern 5

#include <stdio.h>
#include <conio.h>
void main()
{
int i,j;
clrscr();
for(i=5; i>=1; i--)
{
for(j=1; j<=i; j++)
{
printf(" * ");

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12
}
printf("\n");
}
getch();
}

6. Output :

*
**
***
****
* * * * *_

Program :

/* Program to print pyramid pattern in C : Pattern 6

#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,k,t=0;
clrscr();
for(i=1; i<=5; i++)
{
for(k=t; k<5; k++)
{
printf(" ");
}
for(j=0; j< i; j++)
{
printf(" * ");
t = t + 1;
}
printf("\n");
}
getch();

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12

7. Output :

*
**
***
****
*****
****
***
**
*_

Program :

/* Program to print pyramid pattern in C : Pattern 7

Creation Date : 01:13 AM 22/11/2010

Author : www.technoexam.com [Technowell, Sangli] */

#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,k,samp=1;
clrscr();
for(i=1; i<=5; i++)
{
for(k=samp; k<=5; k++)
{
printf(" ");
}
for(j=0; j< i; j++)
{
printf("*");
}
samp = samp + 1;
printf("\n");
}
samp = 1;
for(i=4; i>=1; i--)
{
for(k=samp; k>=0; k--)

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12
{
printf(" ");
}
for(j=i; j>=1; j--)
{
printf("*");
}
samp = samp + 1;
printf("\n");
}
getch();

8. Output :

Enter number of rows: 5

1
23
456
7 8 9 10
11 12 13 14 15_

Program :

/* Program to print pyramid pattern in C : Pattern 8

Creation Date : 02:39 PM 01/10/2011

Author : www.technoexam.com [Technowell, Sangli] */

#include <stdio.h>
#include <conio.h>
void main()
{
int rw, c, no=1 ,len;
clrscr();
printf("Enter number of rows: ");
scanf("%d," &len);
for(rw=1; rw<=len; rw++)
{
printf("\n");
for(c=1; c<=rw; c++)

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12
{
printf(" %2d ", no);
no++;
}
}
getch();
}

9. Output :

Enter number of rows: 5

0
101
21012
3210123
432101234
5 4 3 2 1 0 1 2 3 4 5_

Program :

/* Program to print pyramid pattern in C : Pattern 9

Creation Date : 03:19 PM 01/10/2011

Author : www.technoexam.com [Technowell, Sangli] */

#include <stdio.h>
#include <conio.h>
void main()
{
int no,i,y,x=35;
clrscr();
printf("Enter number of rows: ");
scanf("%d," &no);
for(y=0;y<=no;y++)
{
goto (x,y+1);
for(i=0-y; i<=y; i++)
{
printf(" %3d ", abs(i));
x=x-3;
}

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12
}
getch();
}

10. Output :

1
22
333
4444
5 5 5 5 5_

Program :

/* Program to print pyramid pattern in C : Pattern 10

Creation Date : 03:14 PM 01/10/2011

Author : www.technoexam.com [Technowell, Sangli] */

#include <stdio.h>
#include <conio.h>
void main()
{
int i, j=5, k, x;
clrscr();
for(i=1;i<=5;i++)
{
for(k=1;k<=j;k++)
{
printf(" ");
}
for(x=1;x<=i;x++)

{
printf("%d",i);
printf(" ");
}
printf("\n");
j=j-1;
}
getch();
}

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12

11. Output :

1
12
123
1234
1 2 3 4 5_

Program :

/* Program to print pyramid pattern in C : Pattern 11

Creation Date : 03:24 PM 01/10/2011

Author : www.technoexam.com [Technowell, Sangli] */

#include <stdio.h>
#include <conio.h>
void main()
{
int rw,c,no,spc;
clrscr();
printf("Enter number of rows : ");
scanf("%d", &no);
for(rw=1; rw<=no; rw++)
{
for(spc=no; spc>=rw; spc--)
{
printf(" ");
}
for(c=1; c<=rw; c++)
{
printf("%2d",c);
}
printf("\n");
}
getch();
}

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12

12. Output :

1
123
12345
1234567
1 2 3 4 5 6 7 8 9_

Program :

/* Program to print pyramid pattern in C : Pattern 12

Creation Date : 03:24 PM 01/10/2011

Author : www.technoexam.com [Technowell, Sangli] */

#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=1; i<=5; i++)
{
for(j=1; j<=5-i; j++)
{
printf(" ");
}
for(k=1; k<=2*i-1; k++)
{
printf(" %d ",k);
}
printf("\n");
}
getch();
}

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12

13. Output :

ABCDEFGGFEDCBA
ABCDEFFEDCBA
ABCDEEDCBA
ABCDDCBA
ABCCBA
ABBA
A A_

Program :

/* Program to print pyramid pattern in C : Pattern 13

Creation Date : 04:24 PM 01/10/2011

Author : www.technoexam.com [Technowell, Sangli] */

#include <stdio.h>
#include <conio.h>
void main()

{
int i,j,asci,spc;
clrscr();
for(i=7; i>=1; i--)
{
for(spc=6; spc>=i; spc--)
{
printf(" ");
}
asci=65;
for(j=1; j<=i; j++)
{
printf("%2c",asci++);
}
for(j=i-1; j>=0; j--)
{
printf("%2c",--asci);
}

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12
printf("\n");
}
getch();
}

14. Output :

AAA AAB AAC ABA ABB ABC ACA ACB ACC BAA BAB BAC BBA BBB
BBC BCA BCB BCC CAA CAB CAC CBA CBB CBC CCA CCB CCC_

Program :

/* Program to print all Combinations of characters

A, B, C : Pattern 14

Creation Date : 11:33 PM 01/10/2011

Author : www.technoexam.com [Technowell, Sangli] */

#include <stdio.h>
#include <conio.h>
void main()
{
char ch1, ch2, ch3;
clrscr();
for(ch1='A' ; ch1<='C' ; ++ch1)
{
for(ch2='A' ; ch2<='C' ; ++ch2)
{
for(ch3='A' ; ch3<='C' ; ++ch3)
{
printf(" %c%c%c", ch1, ch2, ch3);
}
}
}
getch();

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12

15. Write a C program to print the following pattern:


1
01
101
0101
10101

Program:

#include <stdio.h>

int main(void) {
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j <= i; j++) {
if (((i + j) % 2) == 0) { // Decides on as to which digit to print.
printf("0");
} else {
printf("1");
}
printf("\t");
}
printf("\n");
}
return 0;
}

16. Write C program to print the following pattern:


0
11
235
8 13 21

Program:

#include <stdio.h>

int main(void) {
int i, j, a = 0, b = 1, temp = 1;

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12
for (i = 1; i <= 4; i++) {
for (j = 1; j <= i; j++) {
if (i == 1 && j == 1) { // Prints the '0' individually first
printf("0");
continue;
}
printf("%d ", temp); // Prints the next digit in the series
//Computes the series
temp = a + b;
a = b;
b = temp;
if (i == 4 && j == 3) { // Skips the 4th character of the base
break;
}
}
printf("\n");
}
return 0;
}

17. Write C program to print the following pattern:


1
121
12321
1234321
12321
121
1

Program:

#include <stdio.h>

void sequence(int x);


int main() {
/* c taken for columns */
int i, x = 0, num = 7;
for (i = 1; i <= num; i++) {
if (i <= (num / 2) + 1) {
x = i;
} else {
x = 8 - i;
}
sequence(x);

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12
puts("\n");
}
return 0;
}

void sequence(int x) {
int j;

for (j = 1; j < x; j++) {


printf("%d", j);
}
for (j = x; j > 0; j--) {
printf("%d", j);
}
}

18. Write a C program to print the following pattern:

77777777777
7
7
7
7
7
7
7
7
7
7

Program:

#include <stdio.h>

int main(void) {
int i, j;
for (i = 11; i >= 1; i--) {
for (j = 1; j <= i; j++) {
if (i == 11) {

printf("7"); // Makes sure the base is printed completely


continue;
} else if (j == i) { // Hollows the rest
printf("7");

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}

19. Write a C program to print the following pattern:


1
24
369
24
1

Program:

#include <stdio.h>
int main(void) {
int i,j;
for (i=1; i<=3 ; i++) {
for (j=1; j<=i; j++) {
printf("%2d", (i*j));
}
printf("\n");
}
for (i=2; i>=1; i--) { // As they share the same base
for (j=1; j<=i; j++) {
printf("%2d",i*j);
}
printf("\n");
}
return 0;
}

20. Write a C program to print the following pattern:

1
10
100
1000
10000

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS


C Programming -Mod-12
100000
1000000
100000
10000
1000
100
10
1

Program:

#include <stdio.h>
int main(void) {
int i,j;
for (i=1; i<=7; i++) {
for (j=1; j<=i; j++) {
if (j==1) { // Applying the condition
printf(" 1");
} else {
printf(" 0");
}
}
printf("\n");
}
for (i=6; i>=1; i--) { //As it shares the same base i=6
for (j=1; j<=i; j++) {
if (j==1) { // Applying the condition
printf(" 1");
} else {
printf(" 0");
}
}
printf("\n");
}
return 0;
}

HDCS-INFORTEC INTERNATIONAL ASIA CAMPUS

You might also like