Structured Programming in C
Structured Programming in C
▪ Explanation :
- A while loop evaluates the condition
- If the condition evaluates to true, the code inside
the while loop is executed.
- The condition is evaluated again.
- This process continues until the condition is false.
- When the condition evaluates to false, the loop
terminates.
{
Output :
int i, j, rows; Enter the number of rows: 5
printf("Enter the number of 1
rows: "); 22
scanf("%d", &rows); 333
4444
36 Nested Control Structures 55555
Program 4 :
Q. WAP to print following pattern. for (i = 1; i <= rows; i++) //outer for loop
A {
AB ch='A’;
ABC for (j = 1; j <= i; j++) // inner for loop
ABCD {
printf("%c ",ch); // inner loop
ABCDE
statement
#include <stdio.h> ch++;
#include<conio.h> }
void main() printf("\n");
{ }
int i, j, rows; getch();
char ch; }
printf("Enter the number of rows: Output :
"); Enter the number of rows: 5
scanf("%d", &rows); A
AB
ABC
37 Nested Control Structures
ABCD
ABCDE
Program 5 :
Q. WAP to print following pattern. for (i = 1; i <= rows; i++) //outer for loop
A {
BB for (j = 1; j <= i; j++) // inner for loop
CCC {
printf("%c ",ch); // inner loop
DDDD
statement
EEEEE }
#include <stdio.h> ch++;
#include<conio.h> printf("\n");
void main() } // end of outer for loop statement
{ getch();
int i, j, rows; }
char ch=‘A’;
Output :
printf("Enter the number of rows: Enter the number of rows: 5
"); A
scanf("%d", &rows); BB
CCC
DDDD
38 Nested Control Structures EEEEE
Program 6 :
Q. WAP to print following for (i = 1; i <= rows; i++) //outer for loop
pattern. {
A for (j = 1; j <= i; j++) // inner for loop
BC {
DEF printf("%c ",ch); // inner loop
GHIJ statement
KLMNO ch++;
#include <stdio.h> }
#include<conio.h> printf("\n");
void main() } // end of outer for loop statement
{ getch();
int i, j, rows; } Output :
char ch=‘A’; Enter the number of rows: 5
printf("Enter the number of A
rows: "); BC
scanf("%d", &rows); DEF
GHIJ
39 Nested Control Structures KLMNO
Program 7 :
Q. WAP to print following pattern. for(i = rows; i >= 1; i--) //outer for loop
{
*****
for(j = 1; j <= i; j++) // inner for loop
**** {
*** printf(“* “); // inner loop statement
** }
printf("\n"); // outer loop statement, to
* // start printing next row on new line
#include <stdio.h> } // end of outer loop
#include<conio.h> getch();
}
void main()
{
Output :
int i, j, rows; Enter the number of rows: 5
printf("Enter the number of *****
rows: "); ****
scanf("%d", &rows); ***
**
40 Nested Control Structures *
Program 8 :
Q. WAP to print following pattern.
12345 for (i = rows; i >= 1; i--)
1234 {
123 for (j = 1; j <= i; j++)
{
12
printf("%d ",j);
1 }
#include <stdio.h> printf("\n");
#include<conio.h> }
void main() getch(); Output :
{ } Enter the number of rows: 5
12345
int i, j, rows;
1234
printf("Enter the number of 123
rows: "); 12
scanf("%d", &rows); 1
41 Nested Control Structures
Program 9 :
Q. WAP to print following pattern.
54321 for (i = rows; i >= 1; i--)
4321 {
for (j =i; j >= 1; j--)
321
{
21 printf("%d ",j);
1 }
#include <stdio.h> printf("\n");
#include<conio.h> }
void main() getch();
} Output :
{ Enter the number of rows: 5
int i, j, rows; 54321
printf("Enter the number of 4321
rows: "); 321
21
scanf("%d", &rows);
1
42 Nested Control Structures
Program 10 :
Q. WAP to print following pattern.
1 for(i = 1; i <= rows; i++)
{
1 2
for(space=i; space<rows; space++) //for
1 2 3 spacing
printf(" ");
1 2 3 4
for(j = 1; j <= i; j++)
1 2 3 4 5 printf("%d ", j);
#include <stdio.h> printf("\n");
#include<conio.h> } Output :
void main() getch(); Enter the number of rows: 5
{ } 1
int i, j, space, rows; 12
printf("Enter the number of 123
rows: "); 1234
scanf("%d", &rows);
12345
43 Nested Control Structures
Program 11 :
Q. WAP to print following pattern. for (i = 1; i <= rows; i++)
{
* for(space=i; space<rows; space++) //for
spacing
* * * printf(" ");
* * * * * for(j = 1; j <= i; j++) // for left triangle
printf("* ");
* * * * * * * for(k=1;k<(j-1);k++) // for right triangle
printf("* ");
* * * * * * * * * printf("\n");
#include <stdio.h> }
#include<conio.h> getch();
void main() }
{
Output :
int i, j, k, space, rows;
Enter the number of rows: 3
printf("Enter the number of rows: ");
*
scanf("%d", &rows);
***
*****
44 Nested Control Structures
Program 12 :
Q. WAP to print following pattern. for (i = 1; i <= rows; i++)
1 {
for(space=i; space<rows; space++)
1 2 1 printf(" ");
1 2 3 2 1 for(j = 1; j <= i; j++)
printf("%d ",j);
1 2 3 4 3 2 1 for(k=1;k<(j-1);k++)
1 2 3 4 5 4 3 2 1 printf("%d ",k);
printf("\n");
#include <stdio.h> }
#include<conio.h> getch();
void main() }
{ Output :
int i, j, k, space, rows; Enter the number of rows: 3
printf("Enter the number of rows: 1
"); 121
scanf("%d", &rows); 12321
45 Nested Control Structures
Program 13 :
Q. WAP to print following pattern. for (i = 1; i <= rows; i++) {
A ch='A';
for(space=i; space<rows; space++)
A B A printf(" ");
A B C B A for(j = 1; j <= i; j++)
{
A B C D C B A
printf("%c ",ch);
#include <stdio.h> ch++;
#include<conio.h> }
void main() for(k=1;k<(j-1);k++)
{
{
int i, j, k, space, rows;
char ch; printf("%c ",(ch-2));
printf("Enter the number of ch--;
rows: "); }
scanf("%d", &rows); printf("\n"); }
getch(); }
46 Nested Control Structures
Program 14 :
Q. WAP to Floyd's triangle. for (i = 1; i <= rows; i++)
1 {
for (j = 1; j <= i; ++j)
2 3 {
4 5 6 printf("%d ", k);
k++;
7 8 9 10 }
printf("\n");
#include <stdio.h> }
#include<conio.h> getch();
void main() }
{ Output :
int i, j, rows, k=1; Enter the number of
printf("Enter the number of rows: 4
rows: "); 1
scanf("%d", &rows); 23
456
47 Nested Control Structures 7 8 9 10
Practice Programs
Q. Write C programs for the following
pattern.
1. 5 4 3 2 1 4. A
5432 BCB
543 CDEDC
54 DEFGFED
5
5. 1
2. 5 5 5 5 5
4444 22
333 333
22 4444
1 55555
3. 1 2 3 4 5
2345 6. 1
345 232
45 34543
5 4567654
567898765