10 nested Loop pattern examples in c
1) Print a Square of Stars
*****
*****
*****
*****
*****
Solutions
#include <stdio.h>
int main() {
int i, j;
for(i = 1; i <= 5; i++) {
for(j = 1; j <= 5; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
2) Right Triangle Star Pattern
*
* *
* **
* ***
* ****
#include <stdio.h>
int main() {
int i, j;
for(i = 1; i <= 5; i++) {
for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
3) Inverted Right Triangle
* ****
* ***
* **
* *
*
#include <stdio.h>
int main() {
int i, j;
for(i = 5; i >= 1; i--) {
for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
4) Number Square Pattern
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
#include <stdio.h>
int main() {
int i, j;
for(i = 1; i <= 4; i++) {
for(j = 1; j <= 4; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
5) Increasing Number Triangle
#include <stdio.h>
int main() {
int i, j;
for(i = 1; i <= 5; i++) {
for(j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
6) Floyd’s Triangle
#include <stdio.h>
int main() {
int i, j, num = 1;
for(i = 1; i <= 4; i++) {
for(j = 1; j <= i; j++) {
printf("%d ", num++);
}
printf("\n");
}
return 0;
}
Output
1
23
456
7 8 9 10
7) Multiplication Table (1–5)
#include <stdio.h>
int main() {
int i, j;
for(i = 1; i <= 5; i++) {
for(j = 1; j <= 10; j++) {
printf("%d x %d = %d\n", i, j, i*j);
}
printf("\n");
}
return 0;
}
8) Alphabet Pattern
#include <stdio.h>
int main() {
int i, j;
char ch = 'A';
for(i = 1; i <= 4; i++) {
for(j = 1; j <= i; j++) {
printf("%c ", ch++);
}
printf("\n");
}
return 0;
}
9) Hollow Square Pattern
#include <stdio.h>
int main() {
int i, j;
for(i = 1; i <= 5; i++) {
for(j = 1; j <= 5; j++) {
if(i == 1 || i == 5 || j == 1 || j == 5)
printf("* ");
else
printf(" ");
}
printf("\n");
}
return 0;
}
10) Matrix Input & Output Using Nested Loops
#include <stdio.h>
int main() {
int a[2][2], i, j;
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
scanf("%d", &a[i][j]);
}
}
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
11) Hollow, diamond shape in c
*
**
* *
* *
* *
* *
* *
**
*
#include <stdio.h>
int main() {
int n = 5; // height of upper half
int i, j;
for(i = 1; i <= n; i++) {// Upper half
for(j = i; j < n; j++)
printf(" ");
for(j = 1; j <= (2*i - 1); j++) {
if(j == 1 || j == (2*i - 1))
printf("*");
else
printf(" ");
}
printf("\n");
}
for(i = n - 1; i >= 1; i--) { // Lower half
for(j = n; j > i; j--)
printf(" ");
for(j = 1; j <= (2*i - 1); j++) {
if(j == 1 || j == (2*i - 1))
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
12 Character Square Pattern (A, B, C, ...)
Testcase:
6
Description:
You are given an integer n.
Using nested loops, print an n x n pattern where:
1) Each row contains alphabets starting from A
Pattern repeats the same sequence for each row
Example: if n = 4, output:
ABCD
ABCD
ABCD
ABCD
Input Format:
n
Output Format:
Print n rows containing alphabetical characters separated by spaces.
Sample Input:
3
Sample Output:
ABC
ABC
ABC
Solution
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
printf("%c ", 'A' + j);
}
printf("\n");
}
return 0;
}
13)Centered Palindromic Number Pyramid
Description:
You are given an integer n.
Using nested loops, print a centered pyramid of numbers:
Each row i prints numbers increasing from 1 to i
Then decreasing from i - 1 back to 1
Spaces are used to center the pyramid
Input Format:
n
Output Format:
Centered number pyramid of height n.
Sample Input:
3
Sample Output:
1 2 1
1 2 3 2 1
Solutions
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) { // print leading spaces
for (int s = 1; s <= n - i; s++) {
printf(" ");
}
for (int j = 1; j <= i; j++) { // print increasing numbers
printf("%d", j);
if (j < i) printf(" ");
}
// print space between inc and dec part only if decreasing exists
if (i > 1) printf(" ");
for (int j = i - 1; j >= 1; j--) {// print decreasing numbers
printf("%d", j);
if (j > 1) printf(" ");
}
printf("\n");
}
return 0;
}
14)Hollow Right-Angled Triangle Pattern
Description:
You are given an integer n.
Using nested loops, print a hollow right-angled triangle made of * characters.
Rules:
First row has one *
Last row has n stars
For rows in between:
Print * at the beginning and end
Fill inner space with spaces
Input Format:
n
Output Format:
Hollow right-angled triangle of height n.
Sample Input:
4
Sample Output:
*
**
* *
****
solution
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
if (i == 1) {
printf("*");
}
else if (i == n) {
for (int j = 1; j <= n; j++) {
printf("*");
if (j < n) printf(" ");
}
} else {
printf("* ");
for (int j = 1; j <= i - 2; j++) {
printf(" ");
}
printf("*");
}
printf("\n");
}
return 0;
}
15)Hollow Right-Angled Triangle Pattern with numbers
1
22
3 3
4 4
55555
#include <stdio.h>
int main(){
int n=5,i,j;
for(i=1;i<= 5;i++){
if(i==1){
printf("%d",i);
}else if(i==n){
for(j=1;j<=n;j++){
printf("%d",n);
if(j<n){
printf(" ");
}
}
}else{
printf("%d ",i);
for(j=1;j<=i-2;j++){
printf(" ");
}
printf("%d",i);
}
printf("\n");
}
}
16)Hollow Right-Angled Triangle Pattern with sequence numbers
1
2 3
4 5
6 7
8 9 10 11 12
Solutions
#include <stdio.h>
int main(){
int n=5,i,count=1,j;
for(i=1;i<= 5;i++){
if(i==1){
printf("%d",count++);
}else if(i==n){
for(j=1;j<=n;j++){
printf("%d",count++);
if(j<n){
printf(" ");
}
}
}else{
printf("%d ",count++);
for(j=1;j<=i-2;j++){
printf(" ");
}
printf("%d",count++);
}
printf("\n");
}
}