0% found this document useful (0 votes)
3 views5 pages

Nested For Loops in C Programming

Uploaded by

riddhi.jain0414
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)
3 views5 pages

Nested For Loops in C Programming

Uploaded by

riddhi.jain0414
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

Nested For Loops

By: Dimple Bohra


* 1 1
12 21
** 123 321

***
for (i=1;i<=n;i++) for (i=1;i<=n;i++) for (i=1;i<=n;i++)
{ { {
for (j=1;j<=i;j++) for (j=1;j<=i;j++) for (j=i;j>=1;j--)
{ { {
printf(“ *”); printf(“%d”, j); printf(“%d”, j);
} } }
printf(“\n”); printf(“\n”); printf(“\n”);
} } }
A A
A ABA
AB BA
ABC BA CBA
ABC ABCD CBA

for (i=1;i<=n;i++) for (i=1;i<=n;i++)


for (i=1;i<=n;i++) { {
{ for (j=1;j<=2*(n-i);j++) for (j=i;j>=1;j--)
for (j=1;j<=i;j++) { printf(“”); {
{ } printf(“ %c”, (char)j+64);
printf(“ %c”, (char)j+64); for(j=1;j<=i;j++) }
} { printf(“\n”);
printf(“\n”); printf(“ %c”,(char)j+64); }
} }
for (j=i-1;j>=1;j--)
{
printf(“ %c”, (char)j+64);
}
printf(“\n”);
}
Pascal Triangle
#include <stdio.h>
int main() {
int rows, coef = 1, space, i, j;
printf("Enter the 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");
}
return 0;
}
Floyd's Triangle

#include <stdio.h>
int main() {
int rows, i, j, number = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; ++j) {
printf("%d ", number);
++number;
}
printf("\n");
}
return 0;
}

You might also like