Write pattern programs in C language for below-given patterns Enter number of rows: 5
Pattern program 1 1 2 3 4 5
Sample Input/ Output:- 1 2 3 4 5
1 2 3 4 5
Enter number of rows: 3 1 2 3 4 5
*** 1 2 3 4 5
*** #include<stdio.h>
*** int main()
{
Enter number of rows: 5 int n;
*****
***** printf("Enter number of rows: ");
***** scanf("%d",&n);
*****
***** for(int r=1; r<=n; r++)
#include<stdio.h> {
int main() for(int c=1; c<=n; c++)
{ {
int n; printf("%3d",c);
}
printf("Enter number of rows: "); printf("\n");
scanf("%d",&n); }
for(int r=1; r<=n; r++) return 0;
{ }
for(int c=1; c<=n; c++) Pattern program 3
{ Sample Input/ Output:-
printf("* ");
} Enter number of rows: 3
printf("\n"); 1 1 1
} 2 2 2
3 3 3
return 0;
} Enter number of rows: 5
1 1 1 1 1
Pattern program 2 2 2 2 2 2
Sample Input/ Output:- 3 3 3 3 3
4 4 4 4 4
Enter number of rows: 3 5 5 5 5 5
1 2 3 #include<stdio.h>
1 2 3 int main()
1 2 3 {
int n;
printf("\n");
printf("Enter number of rows: "); }
scanf("%d",&n);
return 0;
for(int r=1; r<=n; r++) }
{
for(int c=1; c<=n; c++) Pattern program 5
{ Sample Input/ Output:-
printf("%3d",r);
} Enter number of rows: 3
printf("\n"); A A A
} B B B
C C C
return 0;
} Enter number of rows: 5
A A A A A
Pattern program 4 B B B B B
Sample Input/ Output:- C C C C C
D D D D D
Enter number of rows: 3 E E E E E
a a a #include<stdio.h>
b b b int main()
c c c {
int n;
Enter number of rows: 5
a a a a a printf("Enter number of rows: ");
b b b b b scanf("%d",&n);
c c c c c
d d d d d for(int r=1; r<=n; r++)
e e e e e {
#include<stdio.h> for(int c=1; c<=n; c++)
int main() {
{ printf("%3c",r+64);
int n; }
printf("\n");
printf("Enter number of rows: "); }
scanf("%d",&n);
return 0;
for(int r=1; r<=n; r++) }
{
for(int c=1; c<=n; c++) Pattern program 6
{ Sample Input/ Output:-
printf("%3c",r+96);
} Enter number of rows: 3
A B C #include<stdio.h>
A B C int main()
A B C {
int n;
Enter number of rows: 5
A B C D E printf("Enter number of rows: ");
A B C D E scanf("%d",&n);
A B C D E
A B C D E for(int r=1; r<=n; r++)
A B C D E {
#include<stdio.h> for(int c=1; c<=n; c++)
int main() {
{ printf("%3c",c+96);
int n; }
printf("\n");
printf("Enter number of rows: "); }
scanf("%d",&n);
return 0;
for(int r=1; r<=n; r++) }
{
for(int c=1; c<=n; c++) Pattern program 8
{ Sample Input/ Output:-
printf("%3c",c+64);
} Enter number of rows: 3
printf("\n"); a a a
} B B B
c c c
return 0;
} Enter number of rows: 5
a a a a a
Pattern program 7 B B B B B
Sample Input/ Output:- c c c c c
D D D D D
Enter number of rows: 3 e e e e e
a b c
a b c Enter number of rows: 9
a b c a a a a a a a a a
B B B B B B B B B
Enter number of rows: 5 c c c c c c c c c
a b c d e D D D D D D D D D
a b c d e e e e e e e e e e
a b c d e F F F F F F F F F
a b c d e g g g g g g g g g
a b c d e H H H H H H H H H
i i i i i i i i i 10 10 10 10 10 10 10 10 10 10
In these patterns, if the row number is odd then the small letter alphabet is printed. If #include<stdio.h>
the row number is even then the capital letter alphabet is printed. int main()
#include<stdio.h> {
int main() int n,r,c;
{
int n; printf("Enter number of rows: ");
scanf("%d",&n);
printf("Enter number of rows: ");
scanf("%d",&n); //Outer loop
for(r=1; r<=n; r++)
for(int r=1; r<=n; r++) {
{ //Inner loop
for(int c=1; c<=n; c++) for(c=1; c<=n; c++)
{ {
if(r%2==0) printf("%3c", r+64); if(c <= n-r) printf("%4d",1);
else printf("%3c", r+96); else printf("%4d",r);
} }//end of inner loop
printf("\n");
} printf("\n");
}//end of outer loop
return 0;
} return 0;
}
Pattern program 9
Sample input / outputs: Pattern program 10
Sample input / outputs:
Enter number of rows: 5
1 1 1 1 1 Enter number of rows: 5
1 1 1 2 2 *****
1 1 3 3 3 *ABC*
1 4 4 4 4 *DEF*
5 5 5 5 5 *GHI*
*****
Enter number of rows: 10
1 1 1 1 1 1 1 1 1 1 Enter number of rows: 10
1 1 1 1 1 1 1 1 2 2 **********
1 1 1 1 1 1 1 3 3 3 *ABCDEFGH*
1 1 1 1 1 1 4 4 4 4 *IJKLMNOP*
1 1 1 1 1 5 5 5 5 5 *QRSTUVWX*
1 1 1 1 6 6 6 6 6 6 *YZABCDEF*
1 1 1 7 7 7 7 7 7 7 *GHIJKLMN*
1 1 8 8 8 8 8 8 8 8 *OPQRSTUV*
1 9 9 9 9 9 9 9 9 9 *WXYZABCD*
*EFGHIJKL* {
********** for(j=1;j<=i;j++)
In this pattern, when row number and column number is first and last then there are {
stars only. Otherwise, there are alphabets from ‘A’ to ‘Z’. When the alphabet is printf("* ");
reached at ‘Z’ then it again started from ‘A’. }
#include<stdio.h>
int main() printf("\n");
{ }
int n,r,c;
char ch = 'A'; return 0;
}
printf("Enter number of rows: "); Half pyramid using *, increment and decrement operators
scanf("%d",&n); Here we will use decrement operator for outer loop and increment operator for the
inner loop to print above print.
//Outer loop #include<stdio.h>
for(r=1; r<=n; r++) int main()
{ {
//Inner loop int i,j;
for(c=1; c<=n; c++)
{ //Outer for loop with decrement operator
if(c==1||r==1||c==n||r==n) printf("* "); for(i=5;i>=1;i--)
else printf("%c ",ch++); {
if(ch > 'Z') ch='A';
}//end of inner loop //Inner for loop with increment operator
for(j=i;j<=5;j++)
printf("\n"); {
}//end of outer loop printf("* ");
}
return 0;
} printf("\n");
}
Pattern programs in C for half pyramid
Half pyramid using * and increment operator return 0;
* }
** Now, we will use the increment operator for the outer loop and decrement operator for
*** inner loop to print same (above) pattern.
**** #include<stdio.h>
***** int main()
#include<stdio.h> {
int main() int i,j;
{
int i,j; //Outer for loop with increment operator
for(i=1;i<=5;i++)
for(i=1;i<=5;i++) {
7 8 9 10
//Inner for loop with decrement operator 11 12 13 14 15
for(j=i;j>=1;j--)
{ Enter number of rows: 10
printf("* "); 1
} 2 3
4 5 6
printf("\n"); 7 8 9 10
} 11 12 13 14 15
return 0; 16 17 18 19 20 21
} 22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
Half pyramid using Numbers 37 38 39 40 41 42 43 44 45
1 46 47 48 49 50 51 52 53 54 55
21 #include<stdio.h>
321 int main()
4321 {
54321 int n, r, c, a=1;
#include<stdio.h>
int main() printf("Enter number of rows: ");
{ scanf("%d",&n);
int i,j;
for(r=1; r<=n; r++)
for(i=1;i<=5;i++) {
{ for(c=1; c<=r; c++)
printf("%5d",a++);
for(j=i;j>=1;j--)
{ printf("\n");
printf("%d ",j); }
}
return 0;
printf("\n"); }
}
Some similar pattern programs in C
return 0; Pattern program 15
} Sample input / outputs:
Pattern programs in C for Floyd’s triangle Enter number of rows: 5
Sample input / outputs: 1
3 2
Enter number of rows: 5 4 5 6
1 10 9 8 7
2 3 11 12 13 14 15
4 5 6
Enter number of rows: 10 $*$*
1 *$*$*
3 2
4 5 6 Enter number of rows: 10
10 9 8 7 *
11 12 13 14 15 $*
21 20 19 18 17 16 *$*
22 23 24 25 26 27 28 $*$*
36 35 34 33 32 31 30 29 *$*$*
37 38 39 40 41 42 43 44 45 $*$*$*
55 54 53 52 51 50 49 48 47 46 *$*$*$*
In this pattern, when the row is odd then digits are printed from left to right and when $*$*$*$*
the row is even then digits are printed from right to left. *$*$*$*$*
#include<stdio.h> $*$*$*$*$*
int main() In this pattern, if row number + column number is odd then there is a star (*) symbol at
{ that place else there is a dollar ($) symbol.
int n, r, c; #include<stdio.h>
int a=1, b; int main()
{
printf("Enter number of rows: "); int n, r, c;
scanf("%d",&n);
printf("Enter number of rows: ");
for(r=1; r<=n; r++) scanf("%d",&n);
{
b=a+r-1; for(r=1; r<=n; r++)
for(c=1; c<=r; c++, a++) {
{ for(c=1; c<=r; c++)
if(r%2==1) printf("%5d",a); {
else printf("%5d",b--); if((r+c)%2==0) printf("* ");
} else printf("$ ");
}
printf("\n");
} printf("\n");
}
return 0;
} return 0;
}
Pattern program 16
Sample input / outputs: Pattern program 17
Sample input / outputs:
Enter number of rows: 5
* Enter number of rows: 5
$* A
*$* 1 1
B B B
2 2 2 2 Enter number of rows: 3
C C C C C *
**
Enter number of rows: 10 ***
A
1 1 Enter number of rows: 5
B B B *
2 2 2 2 **
C C C C C ***
3 3 3 3 3 3 ****
D D D D D D D *****
4 4 4 4 4 4 4 4 #include<stdio.h>
E E E E E E E E E int main()
5 5 5 5 5 5 5 5 5 5 {
In this pattern, when row number is odd then the same alphabet is printed in the row int n, r, c, s;
otherwise the same digit is printed in that row.
#include<stdio.h> printf("Enter number of rows: ");
int main() scanf("%d",&n);
{
int n, r, c, a=0; for(r=1;r<=n;r++)
char ch='A'; {
for(s=1;s<=n-r;s++) printf(" ");
printf("Enter number of rows: "); for(c=1;c<=r;c++) printf("*");
scanf("%d",&n);
printf("\n");
for(r=1; r<=n; r++) }
{
for(c=1; c<=r; c++) return 0;
{ }
if(r%2==1) printf("%5c",ch);
else printf("%5d",a); Pattern program 19
} Sample Input/Output:-
printf("\n"); Enter number of rows: 3
if(r%2==0) ch++; *
else a++; **
} ***
return 0; Enter number of rows: 5
} *
**
Pattern program 18 ***
Sample Input/Output:- ****
***** printf("\n");
#include<stdio.h> }
int main()
{ return 0;
int n, r, c, s; }
printf("Enter number of rows: "); Some similar pattern programs in C
scanf("%d",&n); Pattern program 21: Display below pattern
Print below pattern using increment operators only
for(r=1;r<=n;r++) ----*
{ ---**
for(s=1;s<=n-r;s++) printf(" "); --***
for(c=1;c<=r;c++) printf("* "); -****
*****
printf("\n"); This pattern is a combination of the previous two patterns, where the sign is changed
} from * to -.
#include<stdio.h>
return 0; int main()
} {
The difference between the previous program and this program is only one space. int i,j,k;
There is an extra space in line for(c=1;c<=r;c++) printf("* "); in comparison to
previous program. for(i=1;i<=5;i++)
{
Pattern programs in C for inverted half pyramid for(j=i;j<5;j++)
Half pyramid using * and decrement operators {
***** printf("- ");
**** }
***
** for(k=1;k<=i;k++)
* {
#include<stdio.h> printf("* ");
int main() }
{
int i,j; printf("\n");
}
for(i=5;i>=1;i--)
{ return 0;
}
for(j=i;j>=1;j--)
{ Pattern programs in C for full pyramid
printf("* "); Pattern program 22: Display full pyramid
} Sample Input/Output:-
Enter number of rows: 5 ***********
* *************
***
*****
*******
*********
Enter number of rows: 7
*
***
*****
******* Generally, on a computer screen, we can print Maximum 80 characters horizontally.
********* Here we will print full pyramid for n lines.
*********** #include<stdio.h>
************* int main()
#include<stdio.h> {
int main() int n,i,j,k,c=80;
{
int n, r, c, s; printf("Enter number of rows: ");
scanf("%d",&n);
printf("Enter number of rows: ");
scanf("%d",&n); for(i=1;i<=n;i++)
{
for(r=1;r<=n;r++) for(j=1;j<=(c/2-i);j++)
{ {
for(s=1;s<=n-r;s++) printf(" "); printf(" ");//print blank space
for(c=1;c<=(2*r-1);c++) printf("*"); }
printf("\n"); for(k=1;k<=(2*i-1);k++)
} {
printf("*");
return 0; }
}
printf("\n");
Pattern program 23: Full pyramid on the center of the screen }
Problem:- Display full pyramid on the center of the computer screen.
Sample Input/Output:- return 0;
}
Enter number of rows: 7
* Pattern program 24: Full pyramid with numbers
*** Sample Input/Output:-
*****
******* Enter number of rows: 5
********* 1
121 Pattern programs in C for inverted full pyramid
12321 Sample Input/Output:-
1234321
123454321 Enter number of rows: 5
*****
Enter number of rows: 7 ****
1 ***
121 **
12321 *
1234321
123454321 Enter number of rows: 7
12345654321 *******
1234567654321 ******
#include<stdio.h> *****
int main() ****
{ ***
int n, r, c, k, a; **
*
printf("Enter number of rows: "); #include<stdio.h>
scanf("%d",&n); int main()
{
for(r=1;r<=n;r++) int n, r, c, s;
{
for(c=1;c<=n-r;c++) printf(" "); printf("Enter number of rows: ");
scanf("%d",&n);
for(k=1;k<=(2*r-1);k++)
{ for(r=n;r>=1;r--)
if(k<r) printf("%d",k); {
else if(k==r) for(s=1;s<=n-r;s++) printf(" ");
{ for(c=1;c<=r;c++) printf("* ");
printf("%d",k);
a=k; printf("\n");
} }
else printf("%d",--a);
} return 0;
}
printf("\n");
} Some more pattern programs in C
Pattern program 26
return 0; Sample Input/Output:-
}
In this program, when column number (except space) is less than row number than Enter number of rows: 5
digits are continuously increasing and after than digits are decreasing. * *
** **
*** *** ABCDEFGHIHGFEDCBA
**** **** ABCDEFG GFEDCBA
********** ABCDEF FEDCBA
ABCDE EDCBA
Enter number of rows: 7 ABCD DCBA
* * ABC CBA
** ** AB BA
*** *** A A
**** **** #include<stdio.h>
***** ***** int main()
****** ****** {
************** int n;
#include<stdio.h> char ch;
int main()
{ printf("Enter number of lines: ");
int n, r, c; scanf("%d",&n);
printf("Enter number of rows: "); for(int i=0; i<=n; i++)
scanf("%d",&n); {
ch = 'A';
for(r=1;r<=n;r++) for(int j=0; j<=n-i; j++, ch++)
{ {
for(c=1;c<=2*n;c++) printf("%c",ch);
if(c<=r||c>(2*n-r)) printf("* "); }
else printf(" ");
if(i==0)
printf("\n"); {
} printf("%c",ch);
}
return 0; else
} {
for(int k=0; k<(2*i)+1; k++){
Pattern program 27 printf(" ");
Sample Input/Output:- }
}
Enter number of lines: 5
ABCDEFGFEDCBA ch--;
ABCDE EDCBA for(int j=0; j<=n-i; j++, ch--)
ABCD DCBA {
ABC CBA printf("%c",ch);
AB BA }
A A
printf("\n");
Enter number of lines: 7 }
return 0; 30
} 31 printf("\n");
Pattern 5: Full Pyramid pattern using * 32 }
1 * 33
2 * * * 34 return 0;
3 * * * * * 35 }
4 * * * * * * * Expected Output:
5 * * * * * * * * * 1 Enter number of lines: 8
6 * * * * * * * * * * * 2
7 * * * * * * * * * * * * * 3 *
8 * * * * * * * * * * * * * * * 4 * * *
9 * * * * * * * * * * * * * * * * * 5 * * * * *
10 * * * * * * * * * * * * * * * * * * * 6 * * * * * * *
The following is a C program to print full Pyramid pattern using *: 7 * * * * * * * * *
1 /******************************************* 8 * * * * * * * * * * *
2 * C Program to print full pyramid using * 9 * * * * * * * * * * * * *
3 ********************************************/ 10 * * * * * * * * * * * * * * *
4 Pattern 6: Full inverted pyramid pattern using *
5 #include<stdio.h> // include stdio.h 1 * * * * * * * * * * * * * * *
6 2 * * * * * * * * * * * * *
7 int main() 3 * * * * * * * * * * *
8 { 4 * * * * * * * * *
9 int n; 5 * * * * * * *
10 6 * * * * *
11 printf("Enter number of lines: "); 7 * * *
12 scanf("%d", &n); 8 *
13 The following is a C program to print full inverted pyramid pattern using *:
14 printf("\n"); 1 /***************************************************
15 2 * C Program to print full inverted pyramid using *
16 // loop for line number of lines 3 ****************************************************/
17 for(int i = 1; i <= n; i++) 4
18 { 5 #include<stdio.h> // include stdio.h
19 // loop to print leading spaces in each line 6
20 for(int space = 0; space <= n - i; space++) 7 int main()
21 { 8 {
22 printf(" "); 9 int n;
23 } 10
24 11 printf("Enter number of lines: ");
25 // loop to print * 12 scanf("%d", &n);
26 for(int j = 1; j <= i * 2 - 1; j++) 13
27 { 14 printf("\n");
28 printf(" * "); 15
29 } 16 // loop for line number of lines
17 for(int i = n; i >= 1; i--) 1 /**********************************************************
18 { 2 * C Program to print hollow right angled triangle using *
19 // loop to print leading spaces in each line 3 ***********************************************************/
20 for(int space = n-i; space >= 1; space--) 4
21 { 5 #include<stdio.h> // include stdio.h
22 printf(" "); 6
23 } 7 int main()
24 8 {
25 // loop to print * 9 int n;
26 for(int j = i * 2 - 1; j >= 1; j--) 10
27 { 11 printf("Enter number of lines: ");
28 printf(" * "); 12 scanf("%d", &n);
29 } 13
30 14 printf("\n");
31 printf("\n"); 15
32 } 16 // loop for number of lines
33 17 for(int i = 1; i <= n; i++)
34 return 0; 18 {
35 } 19 for(int j = 1; j <= i; j++)
Expected Output: 20 {
1 Enter number of lines: 10 21 // print * only on the first line, last column of every line and on the last line
2 22 if(j == 1 || j == i || i == n)
3 * * * * * * * * * * * * * * * * * * * 23 {
4 * * * * * * * * * * * * * * * * * 24 printf("* ");
5 * * * * * * * * * * * * * * * 25 }
6 * * * * * * * * * * * * * 26
7 * * * * * * * * * * * 27 else
8 * * * * * * * * * 28 {
9 * * * * * * * 29 printf(" ");
10 * * * * * 30 }
11 * * * 31 }
12 * 32 printf("\n");
Pattern 7: Hollow right-angled triangle using * 33 }
1* 34
2** 35 return 0;
3* * 36 }
4* * Expected Output:
5* * 1 Enter number of lines: 10
6* * 2
7* * 3 *
8******** 4 **
The following is a C program to print hollow right-angled triangle using *: 5 * *
6 * *
7 * * }
8 * *
28
9 * * else
29
10 * * {
30
11 * * printf(" ");
31
12 * * * * * * * * * * }
32
Pattern 8: Inverted hollow right-angled triangle using * }
33
1******** printf("\n");
34
2* * }
35
3* * 36
4* * return 0;
5* * }
6* * Expected Output:
7** 1 Enter number of lines: 10
8* 2
The following is a C program to print inverted hollow right-angled triangle using *: 3 **********
1 / 4 * *
2 ******************************************************************* 5 * *
3 * C Program to print inverted hollow right angled triangle using * 6 * *
4 ******************************************************************* 7 * *
5 */ 8 * *
6 9 * *
7 #include<stdio.h> // include stdio.h 10 * *
8 11 * *
9 int main() 12 *
10 { Pattern 9: Full hollow pyramid using *
11 int n; 1 *
12 2 * *
13 printf("Enter number of lines: "); 3 * *
14 scanf("%d", &n); 4 * *
15 5 * *
16 printf("\n"); 6 * *
17 7 * *
18 // loop for number of lines 8 * * * * * * * * * * * * * * *
19 for(int i = n; i >= 1; i--) The following is a C program to print a full hollow pyramid using *:
20 { 1 /*************************************************
21 for(int j = i; j >= 1; j--) 2 * C Program to print full hollow pyramid using *
22 { 3 *************************************************/
23 // print * only on the first line, last line and last column of every line and on 4
24 the 5 #include<stdio.h> // include stdio.h
25 if(j == 1 || j == i || i == n) 6
26 { 7 int main()
27 printf("* "); 8 {
9 int n; 8 * *
10 9 * *
11 printf("Enter number of lines: "); 10 * *
12 scanf("%d", &n); 11 * *
13 12 * * * * * * * * * * * * * * * * * * *
14 printf("\n"); Pattern 10: Full inverted hollow pyramid using *
15 1 * * * * * * * * * * * * * * *
16 // loop for line number of lines 2 * *
17 for(int i = 1; i <= n; i++) 3 * *
18 { 4 * *
19 // loop to print leading spaces in each line 5 * *
20 for(int space = 0; space <= n - i; space++) 6 * *
21 { 7 * *
22 printf(" "); 8 *
23 } The following is a C program to print a full hollow inverted pyramid using *:
24 1 /**********************************************************
25 // loop to print * 2 * C Program to print full inverted hollow pyramid using *
26 for(int j = 1; j <= i * 2 - 1; j++) 3 **********************************************************/
27 { 4
28 5 #include<stdio.h> // include stdio.h
29 if (j == 1 || (j == i * 2 - 1) || i == n ) 6
30 { 7 int main() {
31 printf(" * "); 8 int n;
32 } 9
33 else 10 printf("Enter number of lines: ");
34 { 11 scanf("%d", &n);
35 printf(" "); 12
36 } 13 printf("\n");
37 } 14
38 15 // loop for line number of lines
39 printf("\n"); 16 for (int i = n; i >= 1; i--) {
40 } 17 // loop to print leading spaces in each line
41 18 for (int space = n - i; space >= 1; space--) {
42 return 0; 19 printf(" ");
43 } 20 }
Expected Output: 21
1 Enter number of lines: 10 22 // loop to print *
2 23 for (int j = i * 2 - 1; j >= 1; j--)
3 * 24 {
4 * * 25 if (j == 1 || (j == i * 2 - 1) || i == n)
5 * * 26 {
6 * * 27 printf(" * ");
7 * * 28 }
29 else 7 int main()
30 { 8 {
31 printf(" "); 9 int n;
32 } 10
33 } 11 printf("Enter number of lines: ");
34 12 scanf("%d", &n);
35 printf("\n"); 13
36 } 14 printf("\n");
37 15
38 return 0; 16 // loop to print upper pyramid
39 } 17 for(int i = 1; i <= n; i++)
Expected Output: 18 {
1 Enter number of lines: 10 19 // loop to print leading spaces in each line
2 20 for(int space = 0; space <= n - i; space++)
3 * * * * * * * * * * * * * * * * * * * 21 {
4 * * 22 printf(" ");
5 * * 23 }
6 * * 24
7 * * 25 // loop to print *
8 * * 26 for(int j = 1; j <= i * 2 - 1; j++)
9 * * 27 {
10 * * 28 printf(" * ");
11 * * 29 }
12 * 30
Pattern 11: Diamond pattern using * 31 printf("\n");
1 * 32 }
2 * * * 33
3 * * * * * 34
4 * * * * * * * 35 // loop to print lower pyramid
5 * * * * * * * * * 36 for(int i = n+1; i >= 1; i--)
6 * * * * * * * * * * * 37 {
7 * * * * * * * * * 38 // loop to print leading spaces in each line
8 * * * * * * * 39 for(int space = n-i; space >= 0; space--)
9 * * * * * 40 {
10 * * * 41 printf(" ");
11 * 42 }
43
The following is a C program to print a diamond pattern using *:
44 // loop to print *
1 /*********************************************
45 for(int j = i * 2 - 1; j >= 1; j--)
2 * C Program to print Diamond pattern using *
46 {
3 **********************************************/
47 printf(" * ");
4
48 }
5 #include<stdio.h> // include stdio.h
49
6
50 printf("\n");
51 } int main()
52 {
53 return 0; //1
54 }
char lowerCase = 'a';
Expected Output:
char upperCase = 'A';
1 Enter number of lines: 8
2 int printLowerCase = 0;
3 *
4 * * * //2
5 * * * * * while(lowerCase <= 'z' & upperCase <= 'Z'){
6 * * * * * * * //3
7 * * * * * * * * * if(printLowerCase){
8 * * * * * * * * * * * printf("%c ",lowerCase);
9 * * * * * * * * * * * * *
}else{
10 * * * * * * * * * * * * * * *
11 * * * * * * * * * * * * * * * * * printf("%c ",upperCase);
12 * * * * * * * * * * * * * * * }
13 * * * * * * * * * * * * * //4
14 * * * * * * * * * * * printLowerCase = !printLowerCase;
15 * * * * * * * * * lowerCase ++;
16 * * * * * * * upperCase ++;
17 * * * * * }
18 * * *
19 *
//5
C program to print from A to Z with lower and upper case alternatively : printf("\n");
return 0;
In this C programming tutorial, we will learn how to print from A to Z in an alternative }
case for each character. For example, if the first character is A, second should be b, the Explanation :
third element should be C etc. That means, it will be like A b C d E f…..
To solve this problem, we are going to use the below algorithm : 1. Create two character variables lowerCase and upperCase to store the lower and
upper case value of A. Also create one integer variable printLowerCase and assign it a
The algorithm to solve it : value 0. 0 means print the uppercase and 1 means print the lowercase letter. It will
work as a flag.
1. Create two variables to hold the upper and lower case A character. 2. Run one while loop. It will run till the current value is less than Z(in both lower and
2. Create one flag to detect if the lower case to print or upper case to print. upper case form).
3. Run one loop to print the values each time. Print upper case once and next print 3. Inside the loop, check the value of the [Link] it is 1, print the current uppercase
lower case . Run this loop till Z is printed. character, else print the current lower case character.
4. Inside the loop, always keep changing the value of the [Link], keep the character 4. Change the value of the flag. If it is 1, make it 0. If it is 0, make it 1. Also, increment
value incrementing. the current character. (both lower and upper case character) by one.
Means, lowerCase will hold the next lowercase character and upperCase will hold the
next uppercase character.
#include<stdio.h> 5. After the loop is completed, print one new line.
Output :
AbCdEfGhIjKlMnOpQrStUvWxYz
%4d means 4 spaces and then after print and same %7.2f, but .2 indicate after point
only show two values.
e.g. [Link]
e.g #include <stdio.h>
int main()
{
int a,b,c;
printf("%4d\n",10);
printf("%11.2f",11.2345);
return 0;
}