SlideShare a Scribd company logo
1 of 30
Download to read offline
Computing Fundamentals
Dr. Muhammad Yousaf Hamza
Deputy Chief Engineer, PIEAS
Examples
Write a program that prints 1 star as
Dr. Yousaf, PIEAS
Examples
Write a program that prints 1 star as
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
printf("*");
getchar();
return 0;
}
Examples
Write a program that prints 10 stars as
Dr. Yousaf, PIEAS
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;
}
Dr. Yousaf, PIEAS
Write a program that prints 10 stars in 2 Rows as
#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
Dr. Yousaf, PIEAS
Write a program that prints 10 stars in 3 Rows as
#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
Dr. Yousaf, PIEAS
Write a program that prints 10 stars in 4 Rows as
#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
Dr. Yousaf, PIEAS
Write a program that prints 10 stars in 5 Rows as
#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
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???
Nested Loops
Dr. Yousaf, PIEAS
Code should be efficient
10 stars in 5 rows
(The realistic approach)
Dr. Yousaf, PIEAS
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;
}
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;
}
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
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
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
(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;
}
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
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; }
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
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;
}
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
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;
}
Try to write the programs to print the
following patterns
Dr. Yousaf, PIEAS
Do These Yourself
Try to write the program to print the
following pattern
Dr. Yousaf, PIEAS
* * * * *
* * * * * ** * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
•* * * * *
* *
Do It Yourself

More Related Content

Similar to C Language Lecture 8

SPL 11.1 | Problems on Loop , Nested Loop
SPL 11.1 | Problems on Loop , Nested LoopSPL 11.1 | Problems on Loop , Nested Loop
SPL 11.1 | Problems on Loop , Nested LoopMohammad Imam Hossain
 
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)Make Mannan
 
Programa.eje
Programa.ejePrograma.eje
Programa.ejeguapi387
 
BPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdf
BPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdfBPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdf
BPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdfSyed Mustafa
 
Array programs
Array programsArray programs
Array programsALI RAZA
 

Similar to C Language Lecture 8 (20)

C Language Lecture 7
C Language Lecture 7C Language Lecture 7
C Language Lecture 7
 
C Language Lecture 5
C Language Lecture  5C Language Lecture  5
C Language Lecture 5
 
C Language Lecture 19
C Language Lecture 19C Language Lecture 19
C Language Lecture 19
 
C Language Lecture 17
C Language Lecture 17C Language Lecture 17
C Language Lecture 17
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
 
SPL 11.1 | Problems on Loop , Nested Loop
SPL 11.1 | Problems on Loop , Nested LoopSPL 11.1 | Problems on Loop , Nested Loop
SPL 11.1 | Problems on Loop , Nested Loop
 
C Language Lecture 4
C Language Lecture  4C Language Lecture  4
C Language Lecture 4
 
C Language Lecture 16
C Language Lecture 16C Language Lecture 16
C Language Lecture 16
 
C Language Lecture 6
C Language Lecture 6C Language Lecture 6
C Language Lecture 6
 
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
 
C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
 
C arrays
C arraysC arrays
C arrays
 
C
CC
C
 
Begin with Python
Begin with PythonBegin with Python
Begin with Python
 
Programa.eje
Programa.ejePrograma.eje
Programa.eje
 
BPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdf
BPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdfBPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdf
BPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdf
 
Array programs
Array programsArray programs
Array programs
 
Os lab 1st mid
Os lab 1st midOs lab 1st mid
Os lab 1st mid
 
Os lab upto_1st_mid
Os lab upto_1st_midOs lab upto_1st_mid
Os lab upto_1st_mid
 
Os lab upto 1st mid
Os lab upto 1st midOs lab upto 1st mid
Os lab upto 1st mid
 

More from Shahzaib Ajmal

More from Shahzaib Ajmal (10)

C Language Lecture 21
C Language Lecture 21C Language Lecture 21
C Language Lecture 21
 
C Language Lecture 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
 
C Language Lecture 14
C Language Lecture 14C Language Lecture 14
C Language Lecture 14
 
C Language Lecture 13
C Language Lecture 13C Language Lecture 13
C Language Lecture 13
 
C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
 
C Language Lecture 11
C Language Lecture  11C Language Lecture  11
C Language Lecture 11
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
C Language Lecture 3
C Language Lecture  3C Language Lecture  3
C Language Lecture 3
 
C Language Lecture 2
C Language Lecture  2C Language Lecture  2
C Language Lecture 2
 
C Language Lecture 1
C Language Lecture  1C Language Lecture  1
C Language Lecture 1
 

Recently uploaded

Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxLimon Prince
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptxPoojaSen20
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint23600690
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptxPoojaSen20
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMELOISARIVERA8
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code ExamplesPeter Brusilovsky
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 

Recently uploaded (20)

Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 

C Language Lecture 8

  • 1. Computing Fundamentals Dr. Muhammad Yousaf Hamza Deputy Chief Engineer, PIEAS
  • 2. Examples Write a program that prints 1 star as Dr. Yousaf, PIEAS
  • 3. Examples Write a program that prints 1 star as Dr. Yousaf, PIEAS #include<stdio.h> int main() { printf("*"); getchar(); return 0; }
  • 4. Examples Write a program that prints 10 stars as Dr. Yousaf, PIEAS
  • 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; }
  • 6. Dr. Yousaf, PIEAS Write a program that prints 10 stars in 2 Rows as
  • 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
  • 8. Dr. Yousaf, PIEAS Write a program that prints 10 stars in 3 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
  • 10. Dr. Yousaf, PIEAS Write a program that prints 10 stars in 4 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
  • 12. Dr. Yousaf, PIEAS Write a program that prints 10 stars in 5 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