5. Examples
Write a program that prints 10 stars as
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
int i;
for (i = 0; i<10; i++)
printf("*");
getchar();
return 0;
}
7. #include<stdio.h>
int main()
{
int i; // use of 2 for loops
for (i = 0; i<10; i++)
{
printf("*");
}
printf("n");
for (i = 0; i<10; i++)
{
printf("*");
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Write a program that prints 10 stars in 2 Rows as
9. #include<stdio.h>
int main()
{
int i; // use of 3 for loops
for (i = 0; i<10; i++)
{
printf("*");
}
printf("n");
for (i = 0; i<10; i++)
{
printf("*");
}
printf("n");
for (i = 0; i<10; i++)
{
printf("*");
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Write a program that prints 10 stars in 3 Rows as
11. #include<stdio.h>
int main()
{
int i; // use of 4 for loops
for (i = 0; i<10; i++)
{
printf("*");
}
printf("n");
for (i = 0; i<10; i++)
{
printf("*");
}
printf("n");
for (i = 0; i<10; i++)
{
printf("*");
}
printf("n");
for (i = 0; i<10; i++)
{
printf("*");
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Write a program that prints 10 stars in 4 Rows as
13. #include<stdio.h>
int main()
{
int i; // use of 5 for loops
for (i = 0; i<10; i++)
{
printf("*");
}
printf("n");
for (i = 0; i<10; i++)
{
printf("*");
}
printf("n");
for (i = 0; i<10; i++)
{
printf("*");
}
printf("n");
for (i = 0; i<10; i++)
{
printf("*");
}
printf("n");
for (i = 0; i<10; i++)
{
printf("*");
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Write a program that prints 10 stars in 5 Rows as
14. Dr. Yousaf, PIEAS
It’s not efficient/realistic approach
Write a program that prints 10 stars in 50 Rows as
Use of 50 for loops???
16. Code should be efficient
10 stars in 5 rows
(The realistic approach)
Dr. Yousaf, PIEAS
17. Code should be efficient
10 stars in 5 rows
(The realistic approach)
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
int i, j;
for (i = 0; i<5; i++) // only two for loops
{
for (j = 0; j < 10; j++)
{
printf("*");
}
printf("n");
}
getchar();
return 0;
}
18. Code should be efficient
10 stars in 50 rows
(The realistic approach)
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
int i, j;
for (i = 0; i<50; i++) // only two for loops
{
for (j = 0; j < 10; j++)
{
printf("*");
}
printf("n");
}
getchar();
return 0;
}
19. Nested Loops --- Example
#include<stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <=3; j++)
printf(“ %dn", i*j);
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
20. Nested Loops --- Example
#include<stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <=3; j++)
printf(“ %dn", i*j);
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
21. Nested Loops --- Example
#include<stdio.h> //Nesting of for and while loops
int main()
{
int i, j;
for (i = 1; i <= 5; i++)
{
j= 1;
while(j <=3)
{
printf(“%dn", i*j);
j++;
}
}
getchar(); return 0;} Dr. Yousaf, PIEAS
22. (Once again see this example, 5x5
Stars)
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
int i, j;
for (i = 0; i<5; i++) // only two for loops
{
for (j = 0; j < 5; j++)
{
printf("*");
}
printf("n");
}
getchar();
return 0;
}
23. Try to modify the previous program such that
the program prints the following pattern
0 0 0 0 0 0
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
Dr. Yousaf, PIEAS
24. Try to modify the previous program such that
the program prints the following pattern
0 0 0 0 0 0
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
Dr. Yousaf, PIEAS
#include<stdio.h>
int main ()
{
int i, j;
for(i=0; i<5; i++)
{
for(j=0; j<6; j++)
printf("%dt",i);
printf("n");
}
getchar();
return 0; }
25. Try to modify the previous program such that
the program prints the following pattern
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
Dr. Yousaf, PIEAS
26. Try to modify the previous program such that
the program prints the following pattern
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
Dr. Yousaf, PIEAS
#include<stdio.h>
int main ()
{
int i, j;
for(i=0; i<6; i++)
{
for(j=0; j<5; j++)
printf("%dt",j);
printf("n");
}
getchar(); return 0;
}
27. Try to modify the previous program such that
the program prints the following pattern
Dr. Yousaf, PIEAS
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
28. Try to modify the previous program such that
the program prints the following pattern
Dr. Yousaf, PIEAS
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
#include<stdio.h>
int main ()
{
int i,j;
for(i=0; i<6; i++)
{
for(j=0; j<=i; j++)
printf("%dt",j);
printf("n");
}
getchar();return 0;
}
29. Try to write the programs to print the
following patterns
Dr. Yousaf, PIEAS
Do These Yourself
30. Try to write the program to print the
following pattern
Dr. Yousaf, PIEAS
* * * * *
* * * * * ** * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
•* * * * *
* *
Do It Yourself