SlideShare a Scribd company logo
Lecture 08
Repetition Structures
CSE115: Computing Concepts
Example
• Calculate the sum of the following series (𝑥 and 𝑚 are
user inputs).
𝑥0 + 𝑥1 + 𝑥2 + 𝑥3 + ⋯ + 𝑥 𝑚
Example
• Calculate the sum of the following series (𝑥 and 𝑚 are
user inputs).
𝑥0 + 𝑥1 + 𝑥2 + 𝑥3 + ⋯ + 𝑥 𝑚
#include <stdio.h>
int main()
{
int x, m, term = 1, sum = 1;
scanf("%d%d", &x, &m);
for(i=1; i<=m; i++)
{
term = term * x;
sum = sum + term;
}
printf("Sum = %lf", sum);
return 0;
}
•Read a positive integer and determine if it is
a prime number.
•Pseudo-code:
• Read integer and store it in number
• Set flag to 1
• For i = 2 to (number-1)
• If number is divisible by i then
• Set flag to 0
• If flag equals 1, then the number is a prime
number, otherwise not
Selection Inside Loop
•Read a positive integer and determine if it is
a prime number.
Selection Inside Loop
#include <stdio.h>
int main()
{
int number, i, flag = 1;
scanf("%d", &number);
for(i=2; i<number; i++)
{
if(number % i == 0)
flag = 0;
}
if(flag == 1)
printf("%d is a prime number",number);
else printf("%d is not a prime number",number);
return 0;
}
Example
• Calculate the sum of the following series (𝑛 is user input).
1
1
−
1
2
+
1
3
−
1
4
+
1
5
−
1
6
+
1
7
− ⋯ ±
1
𝑛
Example
• Calculate the sum of the following series (𝑛 is user input).
1
1
−
1
2
+
1
3
−
1
4
+
1
5
−
1
6
+
1
7
− ⋯ ±
1
𝑛
#include <stdio.h>
int main()
{
int n, i;
double term, sum = 0;
scanf("%d", &n);
for(i=1; i<=n; i++)
{
term = 1.0 / i;
if(i%2 == 0)
sum = sum - term;
else
sum = sum + term;
}
printf("Sum = %lf", sum);
return 0;
}
Using break Inside Loop
• In the prime number example, we do not need to
continue the loop till the end once the value of flag is set
to zero.
#include <stdio.h>
int main()
{
int number, i, flag = 1;
scanf("%d", &number);
for(i=2; i<number; i++)
{
if(number % i == 0)
{
flag = 0;
break;
}
}
if(flag == 1)
printf("%d is a prime number",number);
else printf("%d is not a prime number",number);
return 0;
}
The break statement makes
the loop terminate
prematurely.
• Read 10 integers from the user and calculate the sum of the
positive numbers.
Using continue Inside Loop
#include <stdio.h>
int main()
{
int number, i, sum = 0;
for(i=0; i<10; i++)
{
printf("Enter a number: ");
scanf("%d", &number);
if(number < 0)
continue;
sum = sum + number;
printf("%d is addedn", number);
}
printf("Total = %d",sum);
return 0;
}
The continue
statement forces next
iteration of the loop,
skipping any remaining
statements in the loop
• Read 10 integers from the user and calculate the sum of the
positive numbers.
Using continue Inside Loop
#include <stdio.h>
int main()
{
int number, i, sum = 0;
for(i=0; i<10; i++)
{
printf("Enter a number: ");
scanf("%d", &number);
if(number < 0)
continue;
sum = sum + number;
printf("%d is addedn", number);
}
printf("Total = %d",sum);
return 0;
}
Output:
Enter a number: 1
1 is added
Enter a number: 2
2 is added
Enter a number: 3
3 is added
Enter a number: -4
Enter a number: -5
Enter a number: 6
6 is added
Enter a number: 7
7 is added
Enter a number: 8
8 is added
Enter a number: -9
Enter a number: 10
10 is added
Total = 37
• Suppose a man (say, A) stands at (0, 0) and waits for user to give
him the direction and distance to go.
• User may enter N E W S for north, east, west, south, and any value
for distance.
• When user enters 0 as direction, stop and print out the location
where the man stopped
N
E
S
W
Example: A Travelling Man
float x=0, y=0;
char dir;
float mile;
while (1) {
printf("Please input the direction as N,S,E,W (0 to exit): ");
scanf("%c", &dir); fflush(stdin);
if (dir=='0'){ /*stop input, get out of the loop */
break;
}
if (dir!='N' && dir!='S' && dir!='E' && dir!='W') {
printf("Invalid direction, re-enter n");
continue;
}
printf("Please input the mile in %c direction: ", dir);
scanf ("%f",&mile); fflush(stdin);
if (dir == 'N'){ /*in north, compute the y*/
y+=mile;
} else if (dir == 'E'){ /*in east, compute the x*/
x+=mile;
} else if (dir == 'W'){ /*in west, compute the x*/
x-=mile;
} else if (dir == 'S'){ /*in south, compute the y*/
y-=mile;
}
}
printf("nCurrent position of A: (%4.2f,%4.2f)n",x,y); /* output
Nested Loop
• What is the output of the following program?
for (i=1; i<=5; i++)
{
for (j=1; j<=4; j++)
{
printf("*");
}
printf("n");
}
Nested Loop
• What is the output of the following program?
for (i=1; i<=5; i++)
{
for (j=1; j<=4; j++)
{
printf("*");
}
printf("n");
}
Output
****
****
****
****
****
Nested Loop
• What is the output of the following program?
for (i=1; i<=5; i++)
{
for (j=1; j<=i; j++)
{
printf("*");
}
printf("n");
}
Nested Loop
• What is the output of the following program?
for (i=1; i<=5; i++)
{
for (j=1; j<=i; j++)
{
printf("*");
}
printf("n");
}
Output
*
**
***
****
*****
Nested Loop
• Write a program that generates the following pattern.
Output
*
++
***
++++
*****
Nested Loop
• Write a program that generates the following pattern.
int i, j;
for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
{
if (i % 2 == 0)
printf("+");
else
printf("*");
}
printf("n");
}
Output
*
++
***
++++
*****
Nested Loop
• Write a program that generates the following pattern.
Output
*
**
***
****
*****
Nested Loop
• Write a program that generates the following pattern.
for(i=1; i<=5; i++)
{
for(j=5; j>i; j--)
printf(" ");
for(j=1; j<=i; j++)
printf("*");
printf("n");
}
Output
*
**
***
****
*****
Home-works
1. Calculate the sum of the following series, where 𝑛 is
provided as user input.
1 + 2 + 3 + 4 + ⋯ + 𝑛
2. Write a program that calculates the factorial of a positive
integer n provided as user input.
3. Write a program that calculates 𝑎 𝑥, where 𝑎 and 𝑥 are
provided as user inputs.
4. Calculate the sum of the following series, where 𝑥 and 𝑛
is provided as user input.
1 +
𝑥
1!
+
𝑥2
2!
+
𝑥3
3!
+
𝑥4
4!
+ ⋯ +
𝑥 𝑛
𝑛!
Home-works
5. Write programs that generate the following patterns. In
each case, the number of lines is the input.
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
*
* *
* *
* *
* *
* *
* *
* *
*
*
**
***
****
*****
****
***
**
*
*****
****
***
**
*
*
***
*****
*******
*********
*********
*******
*****
***
*

More Related Content

PDF
Cse115 lecture04introtoc programming
PDF
Cse115 lecture02overviewofprogramming
PDF
Cse115 lecture03problemsolving
PDF
Cse115 lecture01numbersystems
PPT
Basics of c++ Programming Language
PPT
C language programming
PPT
C programming
PPT
C++ Language
Cse115 lecture04introtoc programming
Cse115 lecture02overviewofprogramming
Cse115 lecture03problemsolving
Cse115 lecture01numbersystems
Basics of c++ Programming Language
C language programming
C programming
C++ Language

What's hot (20)

PPTX
Cs1123 3 c++ overview
PPT
Basics of c++
PPTX
What is c
PPTX
C++ AND CATEGORIES OF SOFTWARE
PDF
Python unit 2 as per Anna university syllabus
PPT
Basic concept of c++
PDF
C Programming Storage classes, Recursion
PPTX
PPT
C++ programming program design including data structures
PPTX
C++ Programming Language Training in Ambala ! Batra Computer Centre
ODP
(2) cpp imperative programming
PPTX
C language basics
PPTX
C++ PROGRAMMING BASICS
PDF
Lec04-CS110 Computational Engineering
PDF
C++ Tokens
PDF
C programming_MSBTE_Diploma_Pranoti Doke
PPTX
Intro to c++
Cs1123 3 c++ overview
Basics of c++
What is c
C++ AND CATEGORIES OF SOFTWARE
Python unit 2 as per Anna university syllabus
Basic concept of c++
C Programming Storage classes, Recursion
C++ programming program design including data structures
C++ Programming Language Training in Ambala ! Batra Computer Centre
(2) cpp imperative programming
C language basics
C++ PROGRAMMING BASICS
Lec04-CS110 Computational Engineering
C++ Tokens
C programming_MSBTE_Diploma_Pranoti Doke
Intro to c++
Ad

Similar to Cse115 lecture08repetitionstructures part02 (20)

PPT
12 lec 12 loop
PPTX
Looping statements.pptx for basic in c language
PPTX
Looping statements c language basics ppt with example
PPTX
C-LOOP-Session-2.pptx
PDF
04-Looping( For , while and do while looping) .pdf
PPT
Iteration
PPT
Lecture 13 Loops1 with C++ programming.PPT
DOCX
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
DOCX
PDF
SPL 8 | Loop Statements in C
PPT
PPT
Mesics lecture 7 iteration and repetitive executions
PPT
Chapter06.PPT
PPTX
Programming ppt files (final)
PDF
5 c control statements looping
PDF
Programming in C Lab
PDF
C lab programs
12 lec 12 loop
Looping statements.pptx for basic in c language
Looping statements c language basics ppt with example
C-LOOP-Session-2.pptx
04-Looping( For , while and do while looping) .pdf
Iteration
Lecture 13 Loops1 with C++ programming.PPT
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
SPL 8 | Loop Statements in C
Mesics lecture 7 iteration and repetitive executions
Chapter06.PPT
Programming ppt files (final)
5 c control statements looping
Programming in C Lab
C lab programs
Ad

Recently uploaded (20)

PPTX
Welding lecture in detail for understanding
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Geodesy 1.pptx...............................................
PPTX
Construction Project Organization Group 2.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
DOCX
573137875-Attendance-Management-System-original
PDF
Digital Logic Computer Design lecture notes
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Welding lecture in detail for understanding
UNIT-1 - COAL BASED THERMAL POWER PLANTS
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
OOP with Java - Java Introduction (Basics)
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Geodesy 1.pptx...............................................
Construction Project Organization Group 2.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
573137875-Attendance-Management-System-original
Digital Logic Computer Design lecture notes
CH1 Production IntroductoryConcepts.pptx
additive manufacturing of ss316l using mig welding
Foundation to blockchain - A guide to Blockchain Tech
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk

Cse115 lecture08repetitionstructures part02

  • 2. Example • Calculate the sum of the following series (𝑥 and 𝑚 are user inputs). 𝑥0 + 𝑥1 + 𝑥2 + 𝑥3 + ⋯ + 𝑥 𝑚
  • 3. Example • Calculate the sum of the following series (𝑥 and 𝑚 are user inputs). 𝑥0 + 𝑥1 + 𝑥2 + 𝑥3 + ⋯ + 𝑥 𝑚 #include <stdio.h> int main() { int x, m, term = 1, sum = 1; scanf("%d%d", &x, &m); for(i=1; i<=m; i++) { term = term * x; sum = sum + term; } printf("Sum = %lf", sum); return 0; }
  • 4. •Read a positive integer and determine if it is a prime number. •Pseudo-code: • Read integer and store it in number • Set flag to 1 • For i = 2 to (number-1) • If number is divisible by i then • Set flag to 0 • If flag equals 1, then the number is a prime number, otherwise not Selection Inside Loop
  • 5. •Read a positive integer and determine if it is a prime number. Selection Inside Loop #include <stdio.h> int main() { int number, i, flag = 1; scanf("%d", &number); for(i=2; i<number; i++) { if(number % i == 0) flag = 0; } if(flag == 1) printf("%d is a prime number",number); else printf("%d is not a prime number",number); return 0; }
  • 6. Example • Calculate the sum of the following series (𝑛 is user input). 1 1 − 1 2 + 1 3 − 1 4 + 1 5 − 1 6 + 1 7 − ⋯ ± 1 𝑛
  • 7. Example • Calculate the sum of the following series (𝑛 is user input). 1 1 − 1 2 + 1 3 − 1 4 + 1 5 − 1 6 + 1 7 − ⋯ ± 1 𝑛 #include <stdio.h> int main() { int n, i; double term, sum = 0; scanf("%d", &n); for(i=1; i<=n; i++) { term = 1.0 / i; if(i%2 == 0) sum = sum - term; else sum = sum + term; } printf("Sum = %lf", sum); return 0; }
  • 8. Using break Inside Loop • In the prime number example, we do not need to continue the loop till the end once the value of flag is set to zero. #include <stdio.h> int main() { int number, i, flag = 1; scanf("%d", &number); for(i=2; i<number; i++) { if(number % i == 0) { flag = 0; break; } } if(flag == 1) printf("%d is a prime number",number); else printf("%d is not a prime number",number); return 0; } The break statement makes the loop terminate prematurely.
  • 9. • Read 10 integers from the user and calculate the sum of the positive numbers. Using continue Inside Loop #include <stdio.h> int main() { int number, i, sum = 0; for(i=0; i<10; i++) { printf("Enter a number: "); scanf("%d", &number); if(number < 0) continue; sum = sum + number; printf("%d is addedn", number); } printf("Total = %d",sum); return 0; } The continue statement forces next iteration of the loop, skipping any remaining statements in the loop
  • 10. • Read 10 integers from the user and calculate the sum of the positive numbers. Using continue Inside Loop #include <stdio.h> int main() { int number, i, sum = 0; for(i=0; i<10; i++) { printf("Enter a number: "); scanf("%d", &number); if(number < 0) continue; sum = sum + number; printf("%d is addedn", number); } printf("Total = %d",sum); return 0; } Output: Enter a number: 1 1 is added Enter a number: 2 2 is added Enter a number: 3 3 is added Enter a number: -4 Enter a number: -5 Enter a number: 6 6 is added Enter a number: 7 7 is added Enter a number: 8 8 is added Enter a number: -9 Enter a number: 10 10 is added Total = 37
  • 11. • Suppose a man (say, A) stands at (0, 0) and waits for user to give him the direction and distance to go. • User may enter N E W S for north, east, west, south, and any value for distance. • When user enters 0 as direction, stop and print out the location where the man stopped N E S W Example: A Travelling Man
  • 12. float x=0, y=0; char dir; float mile; while (1) { printf("Please input the direction as N,S,E,W (0 to exit): "); scanf("%c", &dir); fflush(stdin); if (dir=='0'){ /*stop input, get out of the loop */ break; } if (dir!='N' && dir!='S' && dir!='E' && dir!='W') { printf("Invalid direction, re-enter n"); continue; } printf("Please input the mile in %c direction: ", dir); scanf ("%f",&mile); fflush(stdin); if (dir == 'N'){ /*in north, compute the y*/ y+=mile; } else if (dir == 'E'){ /*in east, compute the x*/ x+=mile; } else if (dir == 'W'){ /*in west, compute the x*/ x-=mile; } else if (dir == 'S'){ /*in south, compute the y*/ y-=mile; } } printf("nCurrent position of A: (%4.2f,%4.2f)n",x,y); /* output
  • 13. Nested Loop • What is the output of the following program? for (i=1; i<=5; i++) { for (j=1; j<=4; j++) { printf("*"); } printf("n"); }
  • 14. Nested Loop • What is the output of the following program? for (i=1; i<=5; i++) { for (j=1; j<=4; j++) { printf("*"); } printf("n"); } Output **** **** **** **** ****
  • 15. Nested Loop • What is the output of the following program? for (i=1; i<=5; i++) { for (j=1; j<=i; j++) { printf("*"); } printf("n"); }
  • 16. Nested Loop • What is the output of the following program? for (i=1; i<=5; i++) { for (j=1; j<=i; j++) { printf("*"); } printf("n"); } Output * ** *** **** *****
  • 17. Nested Loop • Write a program that generates the following pattern. Output * ++ *** ++++ *****
  • 18. Nested Loop • Write a program that generates the following pattern. int i, j; for(i=1; i<=5; i++) { for(j=1; j<=i; j++) { if (i % 2 == 0) printf("+"); else printf("*"); } printf("n"); } Output * ++ *** ++++ *****
  • 19. Nested Loop • Write a program that generates the following pattern. Output * ** *** **** *****
  • 20. Nested Loop • Write a program that generates the following pattern. for(i=1; i<=5; i++) { for(j=5; j>i; j--) printf(" "); for(j=1; j<=i; j++) printf("*"); printf("n"); } Output * ** *** **** *****
  • 21. Home-works 1. Calculate the sum of the following series, where 𝑛 is provided as user input. 1 + 2 + 3 + 4 + ⋯ + 𝑛 2. Write a program that calculates the factorial of a positive integer n provided as user input. 3. Write a program that calculates 𝑎 𝑥, where 𝑎 and 𝑥 are provided as user inputs. 4. Calculate the sum of the following series, where 𝑥 and 𝑛 is provided as user input. 1 + 𝑥 1! + 𝑥2 2! + 𝑥3 3! + 𝑥4 4! + ⋯ + 𝑥 𝑛 𝑛!
  • 22. Home-works 5. Write programs that generate the following patterns. In each case, the number of lines is the input. ********** **** **** *** *** ** ** * * ** ** *** *** **** **** ********** * * * * * * * * * * * * * * * * * ** *** **** ***** **** *** ** * ***** **** *** ** * * *** ***** ******* ********* ********* ******* ***** *** *