0% found this document useful (0 votes)
89 views30 pages

Pattern Question C-1

The document contains code snippets for printing various star patterns using C programming. It provides over 20 programs that print patterns like pyramids, triangles, numbers, and mirror images. Each program includes the code and an explanation of what pattern it produces. This is followed by writing prompts for additional patterns and code solutions to generate those patterns.

Uploaded by

AnjaliSaxena
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
89 views30 pages

Pattern Question C-1

The document contains code snippets for printing various star patterns using C programming. It provides over 20 programs that print patterns like pyramids, triangles, numbers, and mirror images. Each program includes the code and an explanation of what pattern it produces. This is followed by writing prompts for additional patterns and code solutions to generate those patterns.

Uploaded by

AnjaliSaxena
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 30

PATTERN QUESTION

*
***
*****
*******
*********
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 programming code
#include<stdio.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--;
for ( c = 1 ; c <= 2*row - 1 ; c++ )
printf("*");
printf("\n");
}//loop end
return 0;
}

Program to print

the pattern

*
**
***
****
*****
Program 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");
} //end of loop

Program to print the below number pattern


1
12
123
1234
Program Code:
#include<stdio.h>
main()
{
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");
}
getch();
}

Program to print
* * * * *
* * *
* *
*
Program Code:
#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");
}//end of outer for loop
getch();
}

Program to print the below pattern


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

Program to get output


1
232
34543
4567654
567898765

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

program to get the output as follows:


1
121
12321
1234321
123454321

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

Write a Program

to print

1234
5678
9012
3456
7890
--------

Program Code:
main()
{
int i,j,n,temp;
printf("enter number of lines");
scanf("%d",&n);
j=n*4;
for(i=0,temp=0;i<j;i++,temp++)
{
if(temp!=4)
{
printf("%d",i%10);
}
else
{
printf("\n%d",i%10);
temp=0;
}
}
return 0;
}

Write a Program to Print


*
* *
* * *
* * * *

Program Code:
/* This is a simple mirror-image of a right angle triangle */
#include<stdio.h>
int main() {
char prnt = '*';
int i, j, nos = 4, s;
for (i = 1; i <= 5; i++) { for (s = nos; s >= 1; s--) { // Spacing factor
printf(" ");
}
for (j = 1; j <= i; j++) {
printf("%2c", prnt);
}
printf("\n");
--nos; // Controls the spacing factor
}
return 0;
}

Write a Program to Print


*

* * *
* * * * *

* * *
* * * * *

* * * * * * * * * * *

Program Code:
#include<stdio.h>
int main() {
char prnt = '*';
int i, j, k, s, c = 1, nos = 9;
for (i = 1; c <= 4; i++) {
// As we want to print the columns in odd sequence viz. 1,3,5,.etc
if ((i % 2) != 0) {
for (j = 1; j <= i; j++)
{
printf("%2c", prnt);
}
for (s = nos; s >= 1; s--)
{ //The spacing factor
if (c == 4 && s == 1) {
break;
}

printf(" ");
}
for (k = 1; k <= i; k++) {
if (c == 4 && k == 5) {
break;
}
printf("%2c", prnt);
}
printf("\n");
nos = nos - 4; // controls the spacing factor
++c;
}
}
return 0;
}

Write a Program to Print


*

*
*

*
*

*
*

*
*

*
*

*
*

*
*

*
*

*
*

*
*

*
*

Program code:
#include<stdio.h>
int main() {
char prnt = '*';
int i, j, k, s, p, r, nos = 7;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
{
if ((i % 2) != 0 && (j % 2) != 0)
{
printf("%3c", prnt);

}
else if ((i % 2) == 0 && (j % 2) == 0)
{
printf("%3c", prnt);
}
else
{
printf(" ");
}
}
for (s = nos; s >= 1; s--)
{ // for the spacing factor
printf(" ");
}
for (k = 1; k <= i; k++)
{ //Joining seperate figures
if (i == 5 && k == 1)
{
continue;
}
if ((k % 2) != 0)
{
printf("%3c", prnt);
}
else
{
printf(" ");
}
}
printf("\n");
nos = nos - 2; // space control
}
nos = 1; // remaining half..
for (p = 4; p >= 1; p--)
{
for (r = 1; r <= p; r++)
{
if ((p % 2) != 0 && (r % 2) != 0)
{
printf("%3c", prnt);
}
else if ((p % 2) == 0 && (r % 2) == 0)
{

printf("%3c", prnt);
}
else
{
printf(" ");
}
}
for (s = nos; s >= 1; s--)
{
printf(" ");
}
for (k = 1; k <= p; k++)
{
if ((k % 2) != 0) {
printf("%3c", prnt);
}
else {
printf(" ");
}
}
nos = nos + 2; // space control
printf("\n");
}
return 0;
}

Write a Program to Print


*

*
*

*
*

*
*

*
*

*
*
*
*

*
*

*
*

*
*

*
*

Program code:
#include<stdio.h>

int main() {
char prnt = '*';
int i, j, s, nos = 0;
for (i = 9; i >= 1; (i = i - 2)) {
for (s = nos; s >= 1; s--) {
printf(" ");
}
for (j = 1; j <= i; j++) {
if ((i % 2) != 0 && (j % 2) != 0) {
printf("%2c", prnt);
} else {
printf(" ");
}
}
printf("\n");
nos++;
}
nos = 3;
for (i = 3; i <= 9; (i = i + 2)) { for (s = nos; s >= 1; s--) {
printf(" ");
}
for (j = 1; j <= i; j++) {
if ((i % 2) != 0 && (j % 2) != 0) {
printf("%2c", prnt);
} else {
printf(" ");
}
}
nos--;
printf("\n");
}
return 0;
}

Write a C program to print the following pattern:

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

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

Program Code:
#include<stdio.h>
int main() {
char prnt = '*';
int i, j, k, s, nos = 4;
for (i = 1; i <= 5; i++) { for (s = nos; s >= 1; s--) {
printf(" ");
}
for (j = 1; j <= i; j++) {
printf("%2c", prnt);
}
for (k = 1; k <= (i - 1); k++)
{
if (i == 1)
{
continue;
}
printf("%2c", prnt);
}
printf("\n");
nos--;
}
nos = 1;
for (i = 4; i >= 1; i--)
{
for (s = nos; s >= 1; s--)
{
printf(" ");
}
for (j = 1; j <= i; j++) {
printf("%2c", prnt);
}
for (k = 1; k <= (i - 1); k++) {
printf("%2c", prnt);
}

nos++;
printf("\n");
}
nos = 3;
for (i = 2; i <= 5; i++)
{
if ((i % 2) != 0)
{
for (s = nos; s >= 1; s--)
{
printf(" ");
}
for (j = 1; j <= i; j++) {
printf("%2c", prnt);
}
}
if ((i % 2) != 0) {
printf("\n");
nos--;
}
}
return 0;
}

Write a C program to print the following pattern:


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

Program Code:
/*
This can be seen as two right angle triangles sharing the same base
which is modified by adding few extra shifting spaces
*/
#include<stdio.h>
// This function controls the inner loop and the spacing
// factor guided by the outer loop index and the spacing index.

int triangle(int nos, int i) {


char prnt = '*';
int s, j;
for (s = nos; s >= 1; s--) { // Spacing factor
printf(" ");
}
for (j = 1; j <= i; j++) {
//The inner loop
printf("%2c", prnt);
}
return 0;
}
int main() {
int i, nos = 5;
//draws the upper triangle
for (i = 1; i <= 4; i++)
{
triangle(nos, i); //Inner loop construction
nos++;
// Increments the spacing factor
printf("\n");
}
nos = 7; //Draws the lower triangle skipping its base.
for (i = 3; i >= 1; i--)
{
int j = 1;
triangle(nos, i); // Inner loop construction
nos = nos - j; // Spacing factor
printf("\n");
}
return 0;
}

Write a C program to print the following pattern:


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

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

* * * * * * * * *

Program Code:
#include<stdio.h>
int main() {
char prnt = '*';
int i, j, k, s, nos = -1;
for (i = 5; i >= 1; i--) {
for (j = 1; j <= i; j++) { printf("%2c", prnt); } for (s = nos; s >= 1; s--) {
printf(" ");
}
for (k = 1; k <= i; k++) {
if (i == 5 && k == 5) {
continue;
}
printf("%2c", prnt);
}
nos = nos + 2;
printf("\n");
}
nos = 5;
for (i = 2; i <= 5; i++) {
for (j = 1; j <= i; j++)
{
printf("%2c", prnt);
}
for (s = nos; s >= 1; s--)
{
printf(" ");
}
for (k = 1; k <= i; k++) {
if (i == 5 && k == 5) {
break;
}
printf("%2c", prnt);
}
nos = nos - 2;
printf("\n");
}
return 0;
}

Write a C program to print the following pattern:


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

* * * * * * *

* * * * *

* * * * *

* * *

* * *

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

Program code:
#include<stdio.h>
int main() {
char prnt = '*';
int i, j, k, s, sp, nos = 0, nosp = -1;
for (i = 9; i >= 3; (i = i - 2)) {
for (s = nos; s >= 1; s--) {
printf(" ");
}
for (j = 1; j <= i; j++)
{
printf("%2c", prnt);
}
for (sp = nosp; sp >= 1; sp--)
{
printf(" ");
}
for (k = 1; k <= i; k++)
{
if (i == 9 && k == 1)
{
continue;
}
printf("%2c", prnt);
}
nos++;
nosp = nosp + 2;
printf("\n");

}
nos = 4;
for (i = 9; i >= 1; (i = i - 2))
{
for (s = nos; s >= 1; s--) {
printf(" ");
}
for (j = 1; j <= i; j++) {
printf("%2c", prnt);
}
nos++;
printf("\n");
}
return 0;
}

Write a C program to print the following pattern:


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

* *

* *

* * *

* * *

* * * * * * *
* * *

* * *

* *

* *

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

Program code:
#include<stdio.h>
/*
* nos = Num. of spaces required in the triangle.
* i = Counter for the num. of charcters to print in each row
* skip= A flag for checking whether to

*
skip a character in a row.
*
*/
int triangle(int nos, int i, int skip) {
char prnt = '*';
int s, j;
for (s = nos; s >= 1; s--) {
printf(" ");
}
for (j = 1; j <= i; j++) {
if (skip != 0) {
if (i == 4 && j == 1) {
continue;
}
}
printf("%2c", prnt);
}
return 0;
}
int main() {
int i, nos = 4;
for (i = 1; i <= 7; (i = i + 2)) {
triangle(nos, i, 0);
nos--;
printf("\n");
}
nos = 5;
for (i = 1; i <= 4; i++)
{
triangle(1, i, 0); //one space needed in each case of the formation
triangle(nos, i, 1); //skip printing one star in the last row.
nos = nos - 2;
printf("\n");
}
nos = 1;
for (i = 3; i >= 1; i--)
{
triangle(1, i, 0);
triangle(nos, i, 0);
nos = nos + 2;
printf("\n");
}

nos = 1;
for (i = 7; i >= 1; (i = i - 2))
{
triangle(nos, i, 0);
nos++;
printf("\n");
}
return 0;
}

Write a C program to print the following pattern:


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

*
*

*
*

*
*

*
*

*
*
*
*

*
*

*
*

*
*

*
*

*
*

*
*

*
*

*
*

*
*
*

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

Program code:
#include<stdio.h>
/*
* nos = Num. of spaces required in the triangle.
* i = Counter for the num. of characters to print in each row
* skip= A flag for check whether to
*
skip a character in a row.
*
*/
int triangle(int nos, int i, int skip) {
char prnt = '*';
int s, j;
for (s = nos; s >= 1; s--) {
printf(" ");
}
for (j = 1; j <= i; j++)
{
if (skip != 0)

{
if (i == 9 && j == 1)
{
continue;
}
}
if (i == 1 || i == 9)
{
printf("%2c", prnt);
}
else if (j == 1 || j == i)
{
printf("%2c", prnt);
}
else
{
printf(" ");
}
}
return 0;
}
int main()
{
int i, nos = 0, nosp = -1, nbsp = -1;
for (i = 9; i >= 1; (i = i - 2))
{
triangle(nos, i, 0);
triangle(nosp, i, 1);
triangle(nbsp, i, 1);
printf("\n");
nos++;
nosp = nosp + 2;
nbsp = nbsp + 2;
}
nos = 3, nosp = 5, nbsp = 5;
for (i = 3; i <= 9; (i = i + 2)) {
triangle(nos, i, 0);
triangle(nosp, i, 1);
triangle(nbsp, i, 1);
printf("\n");
nos--;
nosp = nosp - 2;
nbsp = nbsp - 2;

}
return 0;
}

Write a Program to print


5
5 4
5 4 3
5 4 3 2
5 4 3 2 1

Program code:
main()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=5;j>=i;j--)
{
printf("%d",j);
}
printf("\n");
}
}

Write a program to print


333
22
1

Program code:
main()
{
int i,a=5,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{

printf("%d",i);
}
printf(" ");
}
}

Write a program to print


1
22
333
4444
55555

Program code:
main()
{
int n, c, k;
printf("Enter number of rows\n");
n=5;
for ( c = 1 ; c <= n ; c++ )
{
for( k = 1 ; k <= c ; k++ )
printf("%d",c);
printf("\n");
}

Program to print the below number pattern


12345
2345
345
45
5

Program Code:

#include<stdio.h>>
void main()
{
int i,j,n;
clrscr();
printf("enter the no \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
printf("%d",j);
} //innner loop
printf("\n");
}//outer loop
}

C program to print a pattern like below:


*
*
*
*

*
*
*

Program Code:
main ()
{
int i,j,k;
for (i=1; i<=10; i+=2)
{
for(k=10; k > i; k-=2) /* small inner loop for the spaces */
{
printf(" ");
}
for (j=0; j <i; j++)

{
if(j==0)
printf("*");
else if(j==i-1)
printf("*");
printf(" ");

} /* end of inner loop */


printf("\n"); /* new line */
} /* End of outer loop */
}
Program to print the below pattern of numbers
1
01
101
0101
10101
int main()
{
int n, i, c, a = 1;
printf("Enter the number of rows of Floyd's triangle to print\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (c = 1; c <= i; c++)
{
printf("%d ",a);
a++;
}
printf("\n");
}

return 0;
}

Write a Program to print the following pattern


1
10
010
0101
10101
int main()
{
int n=5, i, c, a =1;
clrscr();
for (i = 1; i <= n; i++)
{
for (c = 1; c <= i; c++)
{
printf("%d ",a%2);
a++;
}//inner for loop
printf("\n");
a=a-1;
}//outer for loop
return 0;
}

Write a program to print :

#include<stdio.h>/*calling the header files*/


int main()
{
int i,j,k,n,c,c1=0; /*initializing the variables which are needed*/
printf("Please enter the rows for the right and led triangle \n");
scanf("%d",&n);
/*Loop for the upper part of the pattern*/
for(i=n;i>=1;i--)
{
c=64;
for(j=1;j<=i;j++)
{
printf("%c",c+j);
}
for(k=n;k>i;k--)
{
printf(" ");
}
c=65+n-c1;
for(j=1;j<=i;j++)
{
printf("%c",c-j);

}
c1++;
printf("\n");
}
/*loop for lower part */
c1=2;
for(i=2;i<=n;i++)
{
c=64;
for(j=1;j<=i;j++)
{
printf("%c",c+j);
}
for(k=i;k<n;k++)
{
printf(" ");
}
c=65+c1;
for(j=1;j<=i;j++)
{
printf("%c",c-j);
}
c1++;
printf("\n");
}
return 0;
}

C Program To Perform Character Design :

# # # # #
# # # #
# # #
# #
#

/* Declaration Of Header Files */


#include<stdio.h>
#include<conio.h>

/* Start Of Main Program */


int main()
{
/* Declaration Of Variables */
int num, row = 1, col = 0, space = 0;
/* Asking For The Input From User */
printf(" \n Enter Number of Rows For Design : ");
scanf("%d",&num);
/* Source Code For Computing ----- */
for(; num>=1; num--, row++)
{
for(space = row; space>1; space--)
printf(" ");
for(col=1; col<=num; col++)
printf("#");
printf("\n");
}
getch();
return 0;
}
/* End Of Main Program */
Output :~
Enter Number of Rows For Design : 5
#####
####
###
##
#

C program to print
55555
54444
54333
54322
54321

Code:
#include<stdio.h>
void main()
{
int i,j,n,k;
clrscr();
printf("enter the no \n");
scanf("%d",&n);
for(i=5;i>=1;i--)
{
for(j=n;j>=i;j--)
{
printf("%d",j);
} //innner loop
for(k=j;k>=1;k--)
printf("%d",j+1);
printf("\n");
}//outer loop
}

You might also like