Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.

Slides:



Advertisements
Similar presentations
For loops For loops are controlled by a counter variable. for( c =init_value;c
Advertisements

CSE1301 Computer Programming Lecture 4: C Primitives I.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)
FIT Objectives By the end of this lecture, students should: understand iteration statements understand the differences of for and while understand.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)
לולאות 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 1 Department of Computer Science-BGU.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
If () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop 1.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Real World Applications: Statistical Measures Problem (page 95-98) Read a series of floating-point numbers from the standard input, and print a statistical.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
Do-while loop Syntax do statement while (loop repetition condition)
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
WEEK 4 Class Activities Lecturer’s slides.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Introduction to C Programming CE Lecture 4 Further Control Structures in C.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Computer Programming Control Structure
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
H1-1 University of Washington Computer Programming I Lecture 9: Iteration © 2000 UW CSE.
Computer Programming for Engineers
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Problem Solving and Program Design in C Chap. 5 Repetition and Loop Statement Chow-Sing Lin.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
ECE Application Programming
CSE 220 – C Programming Loops.
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
ECE Application Programming
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Iteration statement while do-while
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Control Structures Lecture 7.
INC 161 , CPE 100 Computer Programming
Looping.
C Programming Variables.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Loops in C.
Chapter 2.1 Repetition.
UMBC CMSC 104 – Section 01, Fall 2016
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Introduction to Computing Lecture 04: Booleans & Selection
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering

Topics  break statement  continue statement  Infinite loops  Nested loops  while and for  Macros  Example  Factorization

The break statement Implements the "exit loop" primitive Implements the "exit loop" primitive Causes flow of control to leave a loop block ( while or for ) immediately Causes flow of control to leave a loop block ( while or for ) immediately

The continue statement Causes flow of control to start immediately the next iteration of a loop block ( while or for ) Causes flow of control to start immediately the next iteration of a loop block ( while or for )

Infinite Loops while ( 1 ) while ( 1 ) {...etc...etc...etc......etc...etc...etc... } for ( ; 1 ; ) for ( ; 1 ; ) {...etc...etc...etc......etc...etc...etc... } for ( ; ; ) for ( ; ; ) {...etc...etc...etc......etc...etc...etc... } Use an: if ( condition ) if ( condition ) { break; break; } statement to break the loop

Example: addpos.c Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop{ input number input number if (number is negative) if (number is negative) { begin next iteration begin next iteration } else if ( number is zero) else if ( number is zero) { exit loop exit loop } add number to sum add number to sum} output sum

include include /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; float num, sum = 0.0; printf("sum = %f\n", sum); printf("sum = %f\n", sum); return 0; return 0;} Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop{ input number input number if (number is negative) if (number is negative) { begin next iteration begin next iteration } else if ( number is zero) else if ( number is zero) { exit loop exit loop } add number to sum add number to sum} output sum Example: addpos.c (cont)

include include /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; float num, sum = 0.0; while (1) while (1) { scanf("%f", &num); scanf("%f", &num); sum += num; sum += num; } printf("sum = %f\n", sum); printf("sum = %f\n", sum); return 0; return 0;} Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop{ input number input number if (number is negative) if (number is negative) { begin next iteration begin next iteration } else if ( number is zero) else if ( number is zero) { exit loop exit loop } add number to sum add number to sum} output sum Example: addpos.c (cont)

include include /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; float num, sum = 0.0; while (1) while (1) { scanf("%f", &num); scanf("%f", &num); if (num < 0) if (num < 0) continue; continue; else if (num == 0) else if (num == 0) break; break; sum += num; sum += num; } printf("sum = %f\n", sum); printf("sum = %f\n", sum); return 0; return 0;} Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop{ input number input number if (number is negative) if (number is negative) { begin next iteration begin next iteration } else if ( number is zero) else if ( number is zero) { exit loop exit loop } add number to sum add number to sum} output sum Example: addpos.c (cont)

include include /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; float num, sum = 0.0; while (1)) while (1)) { scanf("%f", &num); scanf("%f", &num); if (num < 0) if (num < 0) continue; continue; else if (num == 0) else if (num == 0) break; break; sum += num; sum += num; } printf("sum = %f\n", sum); printf("sum = %f\n", sum); return 0; return 0;} Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop{ input number input number if (number is negative) if (number is negative) { begin next iteration begin next iteration } else if ( number is zero) else if ( number is zero) { exit loop exit loop } add number to sum add number to sum} output sum Example: addpos.c (cont)

include include /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() { float num, sum = 0.0; float num, sum = 0.0; while (1) while (1) { scanf("%f", &num); scanf("%f", &num); if (num < 0) if (num < 0) continue; continue; else if (num == 0) else if (num == 0) break; break; sum += num; sum += num; } printf("sum = %f\n", sum); printf("sum = %f\n", sum); return 0; return 0;} Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop{ input number input number if (number is negative) if (number is negative) { begin next iteration begin next iteration } else if ( number is zero) else if ( number is zero) { exit loop exit loop } add number to sum add number to sum} output sum Example: addpos.c (cont)

for and continue In the case of a for statement, control passes to the “update” expression. In the case of a for statement, control passes to the “update” expression. for (a = 0; a < 100; a++) { if (a%4) continue; printf(“%d\n”,a); } Example:

Nested Loops - Example: rect.c Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row}

Example: rect.c (cont) #include #include /* Print an m-by-n rectangle of /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); return 0; return 0;} Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row}

#include #include /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); for (i=0; i < n; i++) for (i=0; i < n; i++) { } return 0; return 0;} Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row} Example: rect.c (cont)

#include #include /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); for (i=0; i < n; i++) for (i=0; i < n; i++) { for (j=0; j < m; j++) for (j=0; j < m; j++) { } } return 0; return 0;} Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row} Example: rect.c (cont)

#include #include /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); for (i=0; i < n; i++) for (i=0; i < n; i++) { for (j=0; j < m; j++) for (j=0; j < m; j++) { printf("*"); printf("*"); } } return 0; return 0;} Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row} Example: rect.c (cont)

#include #include /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); for (i=0; i < n; i++) for (i=0; i < n; i++) { for (j=0; j < m; j++) for (j=0; j < m; j++) { printf("*"); printf("*"); } printf("\n"); printf("\n"); } return 0; return 0;} Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row} Example: rect.c (cont)

#include #include /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); for (i=0; i < n; i++) for (i=0; i < n; i++) { for (j=0; j < m; j++) for (j=0; j < m; j++) { printf("*"); printf("*"); } printf("\n"); printf("\n"); } return 0; return 0;} Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row} Example: rect.c (cont)

Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row} #include #include /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); for (i=0; i < n; i++) for (i=0; i < n; i++) { for (j=0; j < m; j++) for (j=0; j < m; j++) { printf("*"); printf("*"); } printf("\n"); printf("\n"); } return 0; return 0;} program algorithm Example: rect.c (cont)

#include #include /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); i = 0; i = 0; while (i < n) while (i < n) { for (j=0; j < m; j++) for (j=0; j < m; j++) { printf("*"); printf("*"); } printf("\n"); printf("\n"); i++; i++; } return 0; } Variation: rect2.c Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row}

#include #include /* Print an m-by-n rectangle of asterisks */ asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); for (i=0; i < n; i++) for (i=0; i < n; i++) { j = 0; j = 0; while (1) while (1) { printf("*"); printf("*"); j++; j++; if (j == m) if (j == m) break; break; } printf("\n"); printf("\n"); } return 0; return 0;} Print an m by n rectangle of asterisks input width and height for each row { for each column in the current row for each column in the current row { print an asterisk print an asterisk } start next row start next row} Variation: rect3.c

#include #include /* Print an m-by-n rectangle of asterisks */ int main() { int i, j, m, n; int i, j, m, n; printf("\nEnter width: "); printf("\nEnter width: "); scanf("%d", &m); scanf("%d", &m); printf("\nEnter height: "); printf("\nEnter height: "); scanf("%d", &n); scanf("%d", &n); for (i=0; i < n; i++) for (i=0; i < n; i++) { j = 0; j = 0; while (1) while (1) { printf("*"); printf("*"); j++; j++; if (j == m) if (j == m) break; break; } printf("\n"); printf("\n"); } return 0; return 0;} The innermost enclosing loop for this break is the while-loop Variation: rect3.c (cont)

while and for A for loop can always be rewritten as an equivalent while loop, and vice-versa A for loop can always be rewritten as an equivalent while loop, and vice-versa The continue statement in a for loop passes control to the “update” expression The continue statement in a for loop passes control to the “update” expression

ASCII (American Standard Code for Information Interchange) Is a character encoding based on the English alphabet. Is a character encoding based on the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that work with text ASCII codes represent text in computers, communications equipment, and other devices that work with text There are 128 characters (0-127) There are 128 characters (0-127) First 32 (0-31) are control characters and are not printable First 32 (0-31) are control characters and are not printable

while (1) while (1) { printf("Enter bounds (low high): "); printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); scanf("%d %d", &low, &high); if ((low >= 0) && (high = 0) && (high <= 127) && (low < high)) { break; break; } else else { printf("Bad bounds. Try again.\n"); printf("Bad bounds. Try again.\n"); } } Example: asciiCheck.c

while (1) while (1) { printf("Enter bounds (low high): "); printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); scanf("%d %d", &low, &high); if ((low >= 0) && (high = 0) && (high <= 127) && (low < high)) { break; break; } else else { printf("Bad bounds. Try again.\n"); printf("Bad bounds. Try again.\n"); } } Example: asciiCheck.c

Example: asciiPrint Print out a section of the ASCII table for each character from the lower bound to higher bound { print its ascii value and ascii character print its ascii value and ascii character} for ( ch = low; ch <= high; ch++ ) for ( ch = low; ch <= high; ch++ ) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); } asciiPrint1.c ch = low; ch = low; while ( ch <= high ) while ( ch <= high ) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); ch++; ch++; } asciiPrint2.c

for ( ch = low; ch <= high; ch++ ) for ( ch = low; ch <= high; ch++ ) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); } ch = low; ch = low; while (1) while (1) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); if (ch < high) if (ch < high) { ch++; ch++; continue; continue; } else else { break; break; } } asciiPrint3.c asciiPrint1.c Example: asciiPrint (cont)

for ( ch = low; ch <= high; ch++ ) for ( ch = low; ch <= high; ch++ ) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); } ch = low; ch = low; for (;;) for (;;) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); ch++; ch++; if (ch > high) if (ch > high) { break; break; } } asciiPrint4.c Example: asciiPrint (cont) asciiPrint1.c

while (1) while (1) { printf("Enter bounds (low high): "); printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); scanf("%d %d", &low, &high); if ((low >= MIN) && (high = MIN) && (high <= MAX) && (low < high)) && (low < high)) { break; break; } else else { printf("Bad bounds. Try again.\n"); printf("Bad bounds. Try again.\n"); } } for (ch=low; ch <= high; ch++) for (ch=low; ch <= high; ch++) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); } return 0; return 0;} Example: ascii1.c #include #include /* Print a section of the ASCII table */ the ASCII table */ #define MIN 0 #define MAX 127 int main() { int low, high; int low, high; char ch; char ch;

while (1) while (1) { printf("Enter bounds (low high): "); printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); scanf("%d %d", &low, &high); if ((low >= MIN) && (high = MIN) && (high <= MAX) && (low < high)) && (low < high)) { break; break; } else else { printf("Bad bounds. Try again.\n"); printf("Bad bounds. Try again.\n"); } } for (ch=low; ch <= high; ch++) for (ch=low; ch <= high; ch++) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); } return 0; return 0;} #include #include /* Print a section of the ASCII table */ the ASCII table */ #define MIN 0 #define MAX 127 int main() { int low, high; int low, high; char ch; char ch; Example: ascii1.c (cont)

while (1) while (1) { printf("Enter bounds (low high): "); printf("Enter bounds (low high): "); scanf("%d %d", &low, &high); scanf("%d %d", &low, &high); if ((low >= MIN) && (high = MIN) && (high <= MAX) && (low < high)) && (low < high)) { break; break; } else else { printf("Bad bounds. Try again.\n"); printf("Bad bounds. Try again.\n"); } } for (ch=low; ch <= high; ch++) for (ch=low; ch <= high; ch++) { printf("%d: %c\n", ch, ch); printf("%d: %c\n", ch, ch); } return 0; return 0;} #include #include /* Print a section of the ASCII table */ the ASCII table */ #define MIN 0 #define MAX 127 int main() { int low, high; int low, high; char ch; char ch; Macro definition: #define identifier tokens All subsequent instances of identifier are replaced with its token Example: ascii1.c (cont)

Output of Ascii1.c Enter bounds (low high): : 33:! 34:" 35:# 36:$ 37:% 38:& 39:' 40:( 41:) 32: 33:! 34:" 35:# 36:$ 37:% 38:& 39:' 40:( 41:) 42:* 43:+ 44:, 45:- 46:. 47:/ 48:0 49:1 50:2 51:3 42:* 43:+ 44:, 45:- 46:. 47:/ 48:0 49:1 50:2 51:3 52:4 53:5 54:6 55:7 56:8 57:9 58:: 59:; 60:< 61:= 52:4 53:5 54:6 55:7 56:8 57:9 58:: 59:; 60:< 61:= 62:> 63:? 65:A 66:B 67:C 68:D 69:E 70:F 71:G 62:> 63:? 65:A 66:B 67:C 68:D 69:E 70:F 71:G 72:H 73:I 74:J 75:K 76:L 77:M 78:N 79:O 80:P 81:Q 72:H 73:I 74:J 75:K 76:L 77:M 78:N 79:O 80:P 81:Q 82:R 83:S 84:T 85:U 86:V 87:W 88:X 89:Y 90:Z 91:[ 82:R 83:S 84:T 85:U 86:V 87:W 88:X 89:Y 90:Z 91:[ 92:\ 93:] 94:^ 95:_ 96:` 97:a 98:b 99:c 100:d 101:e 92:\ 93:] 94:^ 95:_ 96:` 97:a 98:b 99:c 100:d 101:e 102:f 103:g 104:h 105:i 106:j 107:k 108:l 109:m 110:n 111:o 102:f 103:g 104:h 105:i 106:j 107:k 108:l 109:m 110:n 111:o 112:p 113:q 114:r 115:s 116:t 117:u 118:v 119:w 120:x 121:y 112:p 113:q 114:r 115:s 116:t 117:u 118:v 119:w 120:x 121:y 122:z 123:{ 124:| 125:} 126:~ 122:z 123:{ 124:| 125:} 126:~

Example : Factorization Write a program which prints out the prime factorization of a number (treat 2 as the first prime) Write a program which prints out the prime factorization of a number (treat 2 as the first prime) For example, For example, on input 6, desired output is: 2 3 on input 6, desired output is: 2 3 " " 24, " " : " " 24, " " : " " 14, " " : 2 7 " " 14, " " : 2 7 " " 23, " " : 23 (23 is prime) " " 23, " " : 23 (23 is prime)

input n set factor to 2 Algorithm

input n set factor to 2 while(some factor yet to try) { } Algorithm (cont)

input n set factor to 2 while(some factor yet to try) { if (n is divisible by factor) { output factor set n to n / factor } } Algorithm (cont)

input n set factor to 2 while(some factor yet to try) { if (n is divisible by factor) { output factor set n to n / factor } else { increment factor } Algorithm (cont)

input n set factor to 2 while(some factor yet to try) { if (n is divisible by factor) { output factor set n to n / factor } else { increment factor } while(some factor yet to try) { if (n is divisible by factor) { output factor set n to n / factor } increment factor } Algorithm (cont) Why not?

#include /* Print out the prime factors of a number */ int main() { int n, factor ; return 0; } Program

#include /* Print out the prime factors of a number */ int main() { int n, factor ; printf("\nEnter integer: ") ; scanf("%d", &n) ; return 0; } Program (cont)

#include /* Print out the prime factors of a number */ int main() { int n, factor ; printf("\nEnter integer: ") ; scanf("%d", &n) ; printf("\nThe prime factors of %d are: ", n) ; /* Try each possible factor in turn */ printf("\n\n"); return 0; } Program (cont)

/* Try each possible factor in turn */ factor = 2; while ( factor 1 ) { }

/* Try each possible factor in turn */ factor = 2; while ( factor 1 ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; }

/* Try each possible factor in turn */ factor = 2; while ( factor 1 ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor */ factor++ ; }

/* Try each possible factor in turn */ factor = 2; while ( factor 1 ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor */ factor++ ; }

#include /* Print out the prime factors of a number */ int main() { int n, factor ; printf("\nEnter integer: ") ; scanf("%d", &n) ; printf("\nThe prime factors of %d are: ", n) ; /* Try each possible factor in turn */ factor = 2; while ( factor 1 ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor. */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor */ factor++ ; } printf("\n\n"); return 0; } factor1.c

/* Try each possible factor in turn */ factor = 2; while ( factor 1 ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor */ factor++ ; } Change from while -loop to for - loop?

/* Try each possible factor in turn */ for ( factor = 2; factor 1 ; ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor */ factor++ ; }

#include /* Print out the prime factors of a number. */ int main() { int n, factor ; printf("\nEnter integer: ") ; scanf("%d", &n) ; printf("\nThe prime factors of %d are: ", n) ; /* Try each possible factor in turn. */ for ( factor = 2; factor 1 ; ) { if (n % factor == 0) { /* n is a multiple of factor, ** so print factor and divide n by factor. */ printf(" %d", factor) ; n = n / factor ; } else { /* n is not a multiple of factor; ** try next possible factor. */ factor++ ; } printf("\n\n"); return 0; } factor2.c