SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Downloaden Sie, um offline zu lesen
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.
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
*
* *
* *
* *
* *
* *
* *
* *
*
*
**
***
****
*****
****
***
**
*
*****
****
***
**
*
*
***
*****
*******
*********
*********
*******
*****
***
*

Weitere Àhnliche Inhalte

Was ist angesagt?

Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overviewTAlha MAlik
 
Python unit 2 as per Anna university syllabus
Python unit 2 as per Anna university syllabusPython unit 2 as per Anna university syllabus
Python unit 2 as per Anna university syllabusDhivyaSubramaniyam
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, RecursionSreedhar Chowdam
 
C++ programming program design including data structures
C++ programming program design including data structures C++ programming program design including data structures
C++ programming program design including data structures Ahmad Idrees
 
C++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer CentreC++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer Centrejatin batra
 
(2) cpp imperative programming
(2) cpp imperative programming(2) cpp imperative programming
(2) cpp imperative programmingNico Ludwig
 
C language basics
C language basicsC language basics
C language basicsMilind Deshkar
 
C++ PROGRAMMING BASICS
C++ PROGRAMMING BASICSC++ PROGRAMMING BASICS
C++ PROGRAMMING BASICSAami Kakakhel
 
Lec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringLec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringSri Harsha Pamu
 
C++ Tokens
C++ TokensC++ Tokens
C++ TokensAmrit Kaur
 
C programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokeC programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokePranoti Doke
 

Was ist angesagt? (20)

Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
What is c
What is cWhat is c
What is c
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
C++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWAREC++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWARE
 
Python unit 2 as per Anna university syllabus
Python unit 2 as per Anna university syllabusPython unit 2 as per Anna university syllabus
Python unit 2 as per Anna university syllabus
 
Basic concept of c++
Basic concept of c++Basic concept of c++
Basic concept of c++
 
C++ Ch3
C++ Ch3C++ Ch3
C++ Ch3
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, Recursion
 
c++
 c++  c++
c++
 
C++ ch2
C++ ch2C++ ch2
C++ ch2
 
C++ programming program design including data structures
C++ programming program design including data structures C++ programming program design including data structures
C++ programming program design including data structures
 
C++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer CentreC++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer Centre
 
(2) cpp imperative programming
(2) cpp imperative programming(2) cpp imperative programming
(2) cpp imperative programming
 
C language basics
C language basicsC language basics
C language basics
 
C++ PROGRAMMING BASICS
C++ PROGRAMMING BASICSC++ PROGRAMMING BASICS
C++ PROGRAMMING BASICS
 
Lec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringLec04-CS110 Computational Engineering
Lec04-CS110 Computational Engineering
 
C++ Tokens
C++ TokensC++ Tokens
C++ Tokens
 
C programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokeC programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti Doke
 
Intro to c++
Intro to c++Intro to c++
Intro to c++
 

Ähnlich wie Cse115 lecture08repetitionstructures part02

12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop kapil078
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...DR B.Surendiran .
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statementsMomenMostafa
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements loopingMomenMostafa
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02Wingston
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptxDEEPAK948083
 
Input output functions
Input output functionsInput output functions
Input output functionshyderali123
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020vrgokila
 

Ähnlich wie Cse115 lecture08repetitionstructures part02 (20)

12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 
9.C Programming
9.C Programming9.C Programming
9.C Programming
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
Code optimization
Code optimization Code optimization
Code optimization
 
Code optimization
Code optimization Code optimization
Code optimization
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
C programming
C programmingC programming
C programming
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Introduction to c part -1
Introduction to c   part -1Introduction to c   part -1
Introduction to c part -1
 
Unit2 C
Unit2 C Unit2 C
Unit2 C
 
Unit2 C
Unit2 CUnit2 C
Unit2 C
 
pointers 1
pointers 1pointers 1
pointers 1
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Vcs5
Vcs5Vcs5
Vcs5
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
 
Input output functions
Input output functionsInput output functions
Input output functions
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 

KĂŒrzlich hochgeladen

Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniquesugginaramesh
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Study on Air-Water & Water-Water Heat Exchange in a Finned ï»żTube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned ï»żTube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned ï»żTube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned ï»żTube ExchangerAnamika Sarkar
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 

KĂŒrzlich hochgeladen (20)

Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniques
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Study on Air-Water & Water-Water Heat Exchange in a Finned ï»żTube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned ï»żTube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned ï»żTube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned ï»żTube Exchanger
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 

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. ********** **** **** *** *** ** ** * * ** ** *** *** **** **** ********** * * * * * * * * * * * * * * * * * ** *** **** ***** **** *** ** * ***** **** *** ** * * *** ***** ******* ********* ********* ******* ***** *** *