C Programming Amazing Patterns
Write a C program to print the following pattern:
Output:
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
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;
}
Explanation: This is a right angle triangle composed of 0's and 1's.
_________________________________________________________________
Write C program to print the following pattern:
0
1 1
2 3 5
8 13 21
Program:
#include <stdio.h>
int main(void) {
int i, j, a = 0, b = 1, temp = 1;
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;
}
Explanation: This prints the Fibonacci series in a right angle triangle formation where the base has only three characters.
Back to top
_________________________________________________________________
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);
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);
}
}
_________________________________________________________________
Write a C program to print the following pattern:
2
4 5 6
6 7 8 9 10
4 5 6
2
Program:
#include <stdio.h>
int main(void) {
int prnt;
int i, j, k, r, s, sp, nos = 3, nosp = 2; //nos n nosp controls the spacing factor
// Prints the upper triangle
for (i = 1; i <= 5; i++) {
if ((i % 2) != 0) {
for (s = nos; s >= 1; s--) {
printf(" ");
}
for (j = 1; j <= i; j++) {
if (i == 5 && j == 5) { //Provides the extra space reqd betn 9 n 10
printf(" "); // as 10 is a 2 digit no.
}
prnt = i + j;
printf("%2d", prnt);
}
}
if ((i % 2) != 0) {
printf("\n");
nos--;
}
}
// Prints the lower triangle skipin its base..
for (k = 3; k >= 1; k--) {
if ((k % 2) != 0) {
for (sp = nosp; sp >= 1; sp--) {
printf(" ");
}
for (r = 1; r <= k; r++) {
prnt = k + r;
printf("%2d", prnt);
}
}
if ((k % 2) != 0) {
printf("\n");
nosp++;
}
}
return 0;
}
Explanation: This is a diamond formation composed of numbers. The numbers are in the following order next_no=i+j where
next_no = The next no to be printed
i = index of the outer for loop
j = index of the inner for loop
_________________________________________________________________
Write a C program to print the following pattern:
1 1
3 3 3 3 3 3
5 5 5 5 5 5 5 5 5 5
7 7 7 7 7 7 7 7 7 7 7 7 7 7
5 5 5 5 5 5 5 5 5 5
3 3 3 3 3 3
1 1
Program:
#include <stdio.h>
int main(void) {
int i, j, k, s, p, q, sp, r, c = 1, nos = 13;
for (i = 1; c <= 4; i++) {
if ((i % 2) != 0) { // Filters out the even line nos.
for (j = 1; j <= i; j++) { // The upper left triangle
printf("%2d", i);
}
for (s = nos; s >= 1; s--) { // The spacing factor
printf(" ");
}
for (k = 1; k <= i; k++) { // The upper right triangle
printf("%2d", i);
}
printf("\n");
nos = nos - 4; // Space control
++c;
}
}
nos = 10; // Space control re intialized
c = 1;
for (p = 5; (c < 4 && p != 0); p--) {
if ((p % 2) != 0) { // Filters out the even row nos
for (q = 1; q <= p; q++) { // Lower left triangle
printf("%2d", p);
}
for (sp = nos; sp >= 1; sp--) { // Spacing factor
printf(" ");
}
for (r = 1; r <= p; r++) { // Lower right triangle
printf("%2d", p);
}
printf("\n");
--c;
nos = nos + 8; // Spacing control.
}
}
return 0;
}
Explanation: Here we are printing only the odd row nos along with thier respective line number. This structure can divided into four identical right angle triangles which are kind of twisted and turned placed in a particular format .
_________________________________________________________________
Write a C program to print the following pattern:
0
-2-3 0
-4-3-2-1 0
-2-3 0
0
Program:
#include <stdio.h>
int main(void) {
int i, j, k, r, s, sp, nos = 2, nosp = 1;
for (i = 1; i <= 5; i++) {
if ((i % 2) != 0) {
for (s = nos; s >= 1; s--) { //for the spacing factor.
printf(" ");
}
for (j = 1; j <= i; j++) {
printf("%2d", j-i);
}
}
if ((i % 2) != 0) {
printf("\n");
nos--;
}
}
for (k = 3; k >= 1; k--) {
if ((k % 2) != 0) {
for (sp = nosp; sp >= 1; sp--) { // for the spacing factor.
printf(" ");
}
for (r = 1; r <= k; r++) {
printf("%2d", r-k);
}
}
if ((k % 2) != 0) {
printf("\n");
nosp++;
}
}
return 0;
}
Explanation:This can be seen as a diamond composed of numbers. If we use the conventional nested for loop for its construction the numbers can be seen to flowing the following function f(x) -> j-i
where
j= inner loop index
i= outer loop index
_________________________________________________________________
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");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
Explanation: This can be seen as a hollow right-angled triangle composed of 7's
_________________________________________________________________
Write a C program to print the following pattern:
1 1
1 0 1 0
1 0 1 1 0 1
1 0 1 0 1 0 1 0
1 0 1 0 1 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0
1 0 1 0 1 1 0 1 0 1
1 0 1 0 1 0 1 0
1 0 1 1 0 1
1 0 1 0
1 1
Program:
#include <stdio.h>
int main(void) {
int i,j,k,s,nos=11;
for (i=1; i<=7; i++) {
for (j=1; j<=i; j++) {
if ((j%2)!=0) { // Applying the condition
printf(" 1");
} else {
printf(" 0");
}
}
for (s=nos; s>=1; s--) { // Space factor
printf(" ");
}
for (k=1; k<=i; k++) {
if(i==7 && k==1) // Skipping the extra 1
{
continue;
}
if ((k%2)!=0) { // Applying the condition
printf(" 1");
} else {
printf(" 0");
}
}
printf("\n");
nos=nos-2; // Space Control
}
nos=1;
for ( i=6; i>=1; i--) { // It shares the same base
for (j=1; j<=i; j++) {
if (j%2!=0) {
printf(" 1");
} else {
printf(" 0");
}
}
for(s=nos; s>=1; s--) // Spacing factor
{
printf(" ");
}
for (k=1; k<=i; k++) {
if (k%2!=0) {
printf(" 1");
} else {
printf(" 0");
}
}
printf("\n");
nos=nos+2;
}
return 0;
}
_________________________________________________________________
1
2 4
3 6 9
2 4
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;
}
Explanation: This can be seen as two right angle triangles sharing th same base
The numbers are following the following function f(x) = i *j
where
i = Index of the Outer loop
j = Index of the inner loop
_________________________________________________________________
Write a C program to print the following pattern:
1
1 0
1 0 0
1 0 0 0
1 0 0 0 0
1 0 0 0 0 0
1 0 0 0 0 0 0
1 0 0 0 0 0
1 0 0 0 0
1 0 0 0
1 0 0
1 0
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;
}
_________________________________________________________________
C Program To display the triangle using *, numbers and character
Write a C Program to print half pyramid as using * as shown in figure below.
*
* *
* * *
* * * *
* * * * *
#include <stdio.h>
int main()
{
int i,j,rows;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=1;i<=rows;++i)
{
for(j=1;j<=i;++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}
_________________________________________________________________
Write a C Program to print half pyramid as using numbers as shown in figure below.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include <stdio.h>
int main()
{
int i,j,rows;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=1;i<=rows;++i)
{
for(j=1;j<=i;++j)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
_________________________________________________________________
Write a C Program to print triangle of characters as below
A
B B
C C C
D D D D
E E E E E
#include<stdio.h>
int main()
{
int i,j;
char input,temp='A';
printf("Enter uppercase character you want in triangle at last row: ");
scanf("%c",&input);
for(i=1;i<=(input-'A'+1);++i)
{
for(j=1;j<=i;++j)
printf("%c",temp);
++temp;
printf("\n");
}
return 0;
}
_________________________________________________________________
C Program To Display inverted half pyramid using * and numbers
Write a C Program to print inverted half pyramid using * as shown below.
* * * * *
* * * *
* * *
* *
*
#include <stdio.h>
int main()
{
int i,j,rows;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=rows;i>=1;--i)
{
for(j=1;j<=i;++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
_________________________________________________________________
Write a C program to print pyramid using *.
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
#include <stdio.h>
int main()
{
int i,space,rows,k=0;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=1;i<=rows;++i)
{
for(space=1;space<=rows-i;++space)
{
printf(" ");
}
while(k!=2*i-1)
{
printf("* ");
++k;
}
k=0;
printf("\n");
}
return 0;
}
_________________________________________________________________
Write a C program to print the pyramid of digits in pattern as below.
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
#include <stdio.h>
int main()
{
int i,space,rows,k=0,count=0,count1=0;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=1;i<=rows;++i)
{
for(space=1;space<=rows-i;++space)
{
printf(" ");
++count;
}
while(k!=2*i-1)
{
if (count<=rows-1)
{
printf("%d ",(i+k));
++count;
}
else
{
++count1;
printf("%d ", (i+k-2*count1));
}
++k;
}
count1=count=k=0;
printf("\n");
}
return 0;
}
_________________________________________________________________
Write a C program to display reverse pyramid.
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
#include<stdio.h>
int main()
{
int rows,i,j,space;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=rows;i>=1;--i)
{
for(space=0;space<rows-i;++space)
printf(" ");
for(j=i;j<=2*i-1;++j)
printf("* ");
for(j=0;j<i-1;++j)
printf("* ");
printf("\n");
}
return 0;
}
_________________________________________________________________
C Program to Draw Pascal's triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
#include<stdio.h>
int main()
{
int rows,coef=1,space,i,j;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=0;i<rows;i++)
{
for(space=1;space<=rows-i;space++)
printf(" ");
for(j=0;j<=i;j++)
{
if (j==0||i==0)
coef=1;
else
coef=coef*(i-j+1)/j;
printf("%4d",coef);
}
printf("\n");
}
}
_________________________________________________________________
C Program to display Floyd's Triangle.
1
2 3
4 5 6
7 8 9 10
#include<stdio.h>
int main()
{
int rows,i,j,k=0;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;++j)
printf("%d ",k+j);
++k;
printf("\n");
}
}
Write a C program to print the following pattern:
Output:
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
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;
}
Explanation: This is a right angle triangle composed of 0's and 1's.
_________________________________________________________________
Write C program to print the following pattern:
0
1 1
2 3 5
8 13 21
Program:
#include <stdio.h>
int main(void) {
int i, j, a = 0, b = 1, temp = 1;
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;
}
Explanation: This prints the Fibonacci series in a right angle triangle formation where the base has only three characters.
Back to top
_________________________________________________________________
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);
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);
}
}
_________________________________________________________________
Write a C program to print the following pattern:
2
4 5 6
6 7 8 9 10
4 5 6
2
Program:
#include <stdio.h>
int main(void) {
int prnt;
int i, j, k, r, s, sp, nos = 3, nosp = 2; //nos n nosp controls the spacing factor
// Prints the upper triangle
for (i = 1; i <= 5; i++) {
if ((i % 2) != 0) {
for (s = nos; s >= 1; s--) {
printf(" ");
}
for (j = 1; j <= i; j++) {
if (i == 5 && j == 5) { //Provides the extra space reqd betn 9 n 10
printf(" "); // as 10 is a 2 digit no.
}
prnt = i + j;
printf("%2d", prnt);
}
}
if ((i % 2) != 0) {
printf("\n");
nos--;
}
}
// Prints the lower triangle skipin its base..
for (k = 3; k >= 1; k--) {
if ((k % 2) != 0) {
for (sp = nosp; sp >= 1; sp--) {
printf(" ");
}
for (r = 1; r <= k; r++) {
prnt = k + r;
printf("%2d", prnt);
}
}
if ((k % 2) != 0) {
printf("\n");
nosp++;
}
}
return 0;
}
Explanation: This is a diamond formation composed of numbers. The numbers are in the following order next_no=i+j where
next_no = The next no to be printed
i = index of the outer for loop
j = index of the inner for loop
_________________________________________________________________
Write a C program to print the following pattern:
1 1
3 3 3 3 3 3
5 5 5 5 5 5 5 5 5 5
7 7 7 7 7 7 7 7 7 7 7 7 7 7
5 5 5 5 5 5 5 5 5 5
3 3 3 3 3 3
1 1
Program:
#include <stdio.h>
int main(void) {
int i, j, k, s, p, q, sp, r, c = 1, nos = 13;
for (i = 1; c <= 4; i++) {
if ((i % 2) != 0) { // Filters out the even line nos.
for (j = 1; j <= i; j++) { // The upper left triangle
printf("%2d", i);
}
for (s = nos; s >= 1; s--) { // The spacing factor
printf(" ");
}
for (k = 1; k <= i; k++) { // The upper right triangle
printf("%2d", i);
}
printf("\n");
nos = nos - 4; // Space control
++c;
}
}
nos = 10; // Space control re intialized
c = 1;
for (p = 5; (c < 4 && p != 0); p--) {
if ((p % 2) != 0) { // Filters out the even row nos
for (q = 1; q <= p; q++) { // Lower left triangle
printf("%2d", p);
}
for (sp = nos; sp >= 1; sp--) { // Spacing factor
printf(" ");
}
for (r = 1; r <= p; r++) { // Lower right triangle
printf("%2d", p);
}
printf("\n");
--c;
nos = nos + 8; // Spacing control.
}
}
return 0;
}
Explanation: Here we are printing only the odd row nos along with thier respective line number. This structure can divided into four identical right angle triangles which are kind of twisted and turned placed in a particular format .
_________________________________________________________________
Write a C program to print the following pattern:
0
-2-3 0
-4-3-2-1 0
-2-3 0
0
Program:
#include <stdio.h>
int main(void) {
int i, j, k, r, s, sp, nos = 2, nosp = 1;
for (i = 1; i <= 5; i++) {
if ((i % 2) != 0) {
for (s = nos; s >= 1; s--) { //for the spacing factor.
printf(" ");
}
for (j = 1; j <= i; j++) {
printf("%2d", j-i);
}
}
if ((i % 2) != 0) {
printf("\n");
nos--;
}
}
for (k = 3; k >= 1; k--) {
if ((k % 2) != 0) {
for (sp = nosp; sp >= 1; sp--) { // for the spacing factor.
printf(" ");
}
for (r = 1; r <= k; r++) {
printf("%2d", r-k);
}
}
if ((k % 2) != 0) {
printf("\n");
nosp++;
}
}
return 0;
}
Explanation:This can be seen as a diamond composed of numbers. If we use the conventional nested for loop for its construction the numbers can be seen to flowing the following function f(x) -> j-i
where
j= inner loop index
i= outer loop index
_________________________________________________________________
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");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
Explanation: This can be seen as a hollow right-angled triangle composed of 7's
_________________________________________________________________
Write a C program to print the following pattern:
1 1
1 0 1 0
1 0 1 1 0 1
1 0 1 0 1 0 1 0
1 0 1 0 1 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0
1 0 1 0 1 1 0 1 0 1
1 0 1 0 1 0 1 0
1 0 1 1 0 1
1 0 1 0
1 1
Program:
#include <stdio.h>
int main(void) {
int i,j,k,s,nos=11;
for (i=1; i<=7; i++) {
for (j=1; j<=i; j++) {
if ((j%2)!=0) { // Applying the condition
printf(" 1");
} else {
printf(" 0");
}
}
for (s=nos; s>=1; s--) { // Space factor
printf(" ");
}
for (k=1; k<=i; k++) {
if(i==7 && k==1) // Skipping the extra 1
{
continue;
}
if ((k%2)!=0) { // Applying the condition
printf(" 1");
} else {
printf(" 0");
}
}
printf("\n");
nos=nos-2; // Space Control
}
nos=1;
for ( i=6; i>=1; i--) { // It shares the same base
for (j=1; j<=i; j++) {
if (j%2!=0) {
printf(" 1");
} else {
printf(" 0");
}
}
for(s=nos; s>=1; s--) // Spacing factor
{
printf(" ");
}
for (k=1; k<=i; k++) {
if (k%2!=0) {
printf(" 1");
} else {
printf(" 0");
}
}
printf("\n");
nos=nos+2;
}
return 0;
}
_________________________________________________________________
1
2 4
3 6 9
2 4
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;
}
Explanation: This can be seen as two right angle triangles sharing th same base
The numbers are following the following function f(x) = i *j
where
i = Index of the Outer loop
j = Index of the inner loop
_________________________________________________________________
Write a C program to print the following pattern:
1
1 0
1 0 0
1 0 0 0
1 0 0 0 0
1 0 0 0 0 0
1 0 0 0 0 0 0
1 0 0 0 0 0
1 0 0 0 0
1 0 0 0
1 0 0
1 0
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;
}
_________________________________________________________________
C Program To display the triangle using *, numbers and character
Write a C Program to print half pyramid as using * as shown in figure below.
*
* *
* * *
* * * *
* * * * *
#include <stdio.h>
int main()
{
int i,j,rows;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=1;i<=rows;++i)
{
for(j=1;j<=i;++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}
_________________________________________________________________
Write a C Program to print half pyramid as using numbers as shown in figure below.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include <stdio.h>
int main()
{
int i,j,rows;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=1;i<=rows;++i)
{
for(j=1;j<=i;++j)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
_________________________________________________________________
Write a C Program to print triangle of characters as below
A
B B
C C C
D D D D
E E E E E
#include<stdio.h>
int main()
{
int i,j;
char input,temp='A';
printf("Enter uppercase character you want in triangle at last row: ");
scanf("%c",&input);
for(i=1;i<=(input-'A'+1);++i)
{
for(j=1;j<=i;++j)
printf("%c",temp);
++temp;
printf("\n");
}
return 0;
}
_________________________________________________________________
C Program To Display inverted half pyramid using * and numbers
Write a C Program to print inverted half pyramid using * as shown below.
* * * * *
* * * *
* * *
* *
*
#include <stdio.h>
int main()
{
int i,j,rows;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=rows;i>=1;--i)
{
for(j=1;j<=i;++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
_________________________________________________________________
Write a C program to print pyramid using *.
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
#include <stdio.h>
int main()
{
int i,space,rows,k=0;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=1;i<=rows;++i)
{
for(space=1;space<=rows-i;++space)
{
printf(" ");
}
while(k!=2*i-1)
{
printf("* ");
++k;
}
k=0;
printf("\n");
}
return 0;
}
_________________________________________________________________
Write a C program to print the pyramid of digits in pattern as below.
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
#include <stdio.h>
int main()
{
int i,space,rows,k=0,count=0,count1=0;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for(i=1;i<=rows;++i)
{
for(space=1;space<=rows-i;++space)
{
printf(" ");
++count;
}
while(k!=2*i-1)
{
if (count<=rows-1)
{
printf("%d ",(i+k));
++count;
}
else
{
++count1;
printf("%d ", (i+k-2*count1));
}
++k;
}
count1=count=k=0;
printf("\n");
}
return 0;
}
_________________________________________________________________
Write a C program to display reverse pyramid.
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
#include<stdio.h>
int main()
{
int rows,i,j,space;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=rows;i>=1;--i)
{
for(space=0;space<rows-i;++space)
printf(" ");
for(j=i;j<=2*i-1;++j)
printf("* ");
for(j=0;j<i-1;++j)
printf("* ");
printf("\n");
}
return 0;
}
_________________________________________________________________
C Program to Draw Pascal's triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
#include<stdio.h>
int main()
{
int rows,coef=1,space,i,j;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=0;i<rows;i++)
{
for(space=1;space<=rows-i;space++)
printf(" ");
for(j=0;j<=i;j++)
{
if (j==0||i==0)
coef=1;
else
coef=coef*(i-j+1)/j;
printf("%4d",coef);
}
printf("\n");
}
}
_________________________________________________________________
C Program to display Floyd's Triangle.
1
2 3
4 5 6
7 8 9 10
#include<stdio.h>
int main()
{
int rows,i,j,k=0;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;++j)
printf("%d ",k+j);
++k;
printf("\n");
}
}
No comments:
Post a Comment