1 cse1301 computer programming lecture 11: iteration (part 2)
Post on 20-Dec-2015
220 views
TRANSCRIPT
![Page 1: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/1.jpg)
1
CSE1301Computer Programming
Lecture 11:Iteration (Part 2)
![Page 2: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/2.jpg)
2
Topics
• Infinite loops • while and for• Macros
• Input from a file
• Examples
– CountConsonantsAndVowels
– Factorization
![Page 3: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/3.jpg)
3
Infinite Loops
while ( 1 ) { ...etc...etc...etc... }
for ( ; 1 ; ) { ...etc...etc...etc... }
for ( ; ; ) { ...etc...etc...etc... }
![Page 4: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/4.jpg)
4
Infinite Loops
while ( 1 ) { ...etc...etc...etc... }
for ( ; 1 ; ) { ...etc...etc...etc... }
for ( ; ; ) { ...etc...etc...etc... }
Use an:
if ( condition ) { break; }
statement to break the loop
![Page 5: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/5.jpg)
5
while (1) { printf("Enter bounds (low high): "); scanf("%d %d", &low, &high);
if ((low >= 0) && (high <= 127) && (low < high)) { break; } else { printf("Bad bounds. Try again.\n"); } }
Example: asciiCheck.c
![Page 6: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/6.jpg)
6
while (1) { printf("Enter bounds (low high): "); scanf("%d %d", &low, &high);
if ((low >= 0) && (high <= 127) && (low < high)) { break; } else { printf("Bad bounds. Try again.\n"); } }
Example: asciiCheck.c
![Page 7: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/7.jpg)
7
while and for
• 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
![Page 8: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/8.jpg)
8
Example: asciiPrintPrint out a section of the ASCII table
for each character from the lower bound to higher bound{ print its ascii value and ascii character}
for ( ch = low; ch <= high; ch++ ) { printf("%d: %c\n", ch, ch); } asciiPrint1.c
ch = low; while ( ch <= high ) { printf("%d: %c\n", ch, ch); ch++; } asciiPrint2.c
![Page 9: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/9.jpg)
9
for ( ch = low; ch <= high; ch++ ) { printf("%d: %c\n", ch, ch); }
ch = low; while (1) { printf("%d: %c\n", ch, ch); if (ch < high) { ch++; continue; } else { break; } } asciiPrint3.c
asciiPrint1.c
Example: asciiPrint (cont)
![Page 10: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/10.jpg)
10
for ( ch = low; ch <= high; ch++ ) { printf("%d: %c\n", ch, ch); }
ch = low; for (;;) { printf("%d: %c\n", ch, ch); ch++; if (ch > high) { break; } }
asciiPrint4.c
Example: asciiPrint (cont)
asciiPrint1.c
![Page 11: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/11.jpg)
11
while (1) { printf("Enter bounds (low high): "); scanf("%d %d", &low, &high);
if ((low >= MIN) && (high <= MAX) && (low < high)) { break; } else { printf("Bad bounds. Try again.\n"); } }
for (ch=low; ch <= high; ch++) { printf("%d: %c\n", ch, ch); }
return 0;}
Example: ascii1.c
#include <stdio.h>
/* Print a section of the ASCII table */
#define MIN 0#define MAX 127
int main(){ int low, high; char ch;
![Page 12: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/12.jpg)
12
while (1) { printf("Enter bounds (low high): "); scanf("%d %d", &low, &high);
if ((low >= MIN) && (high <= MAX) && (low < high)) { break; } else { printf("Bad bounds. Try again.\n"); } }
for (ch=low; ch <= high; ch++) { printf("%d: %c\n", ch, ch); }
return 0;}
#include <stdio.h>
/* Print a section of the ASCII table */
#define MIN 0#define MAX 127
int main(){ int low, high; char ch;
Example: ascii1.c (cont)
![Page 13: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/13.jpg)
13
while (1) { printf("Enter bounds (low high): "); scanf("%d %d", &low, &high);
if ((low >= MIN) && (high <= MAX) && (low < high)) { break; } else { printf("Bad bounds. Try again.\n"); } }
for (ch=low; ch <= high; ch++) { printf("%d: %c\n", ch, ch); }
return 0;}
#include <stdio.h>
/* Print a section of the ASCII table */
#define MIN 0#define MAX 127
int main(){ int low, high; char ch;
Macro definition:
#define identifier tokens
All subsequent instances of identifier are replaced with its token
Example: ascii1.c (cont)
![Page 14: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/14.jpg)
14
Example 1: CountConsonantsAndVowels
• Count the number of consonants and the number of vowels in a file
• Non-alphabetic characters should not be counted
![Page 15: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/15.jpg)
15
open file for inputset consonantCount to 0set vowelCount to 0loop{ input ch if (end of file) { exit loop }
if (ch is a vowel) { increment vowelCount } else if (ch is a consonant) { increment consonantCount }}close fileoutput consonantCount, vowelCount
Algorithm
![Page 16: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/16.jpg)
16
open file for inputset consonantCount to 0set vowelCount to 0loop{ input ch if (end of file) { exit loop }
if (ch is a vowel) { increment vowelCount } else if (ch is a consonant) { increment consonantCount }}close fileoutput consonantCount, vowelCount
Algorithm (cont)
![Page 17: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/17.jpg)
17
open file for inputset consonantCount to 0set vowelCount to 0loop{ input ch if (end of file) { exit loop }
if (ch is a vowel) { increment vowelCount } else if (ch is a consonant) { increment consonantCount }}close fileoutput consonantCount, vowelCount
Algorithm (cont)
![Page 18: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/18.jpg)
18
open file for inputset consonantCount to 0set vowelCount to 0loop{ input ch if (end of file) { exit loop }
if (ch is a vowel) { increment vowelCount } else if (ch is a consonant) { increment consonantCount }}close fileoutput consonantCount, vowelCount
Algorithm (cont)
![Page 19: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/19.jpg)
19
open file for inputset consonantCount to 0set vowelCount to 0loop{ input ch if (end of file) { exit loop }
if (ch is a vowel) { increment vowelCount } else if (ch is a consonant) { increment consonantCount }}close fileoutput consonantCount, vowelCount
Algorithm (cont)
![Page 20: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/20.jpg)
20
open file for inputset consonantCount to 0set vowelCount to 0loop{ input ch if (end of file) { exit loop }
if (ch is a vowel) { increment vowelCount } else if (ch is a consonant) { increment consonantCount }}close fileoutput consonantCount, vowelCount
Algorithm (cont)
![Page 21: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/21.jpg)
21
open file for inputset consonantCount to 0set vowelCount to 0loop{ input ch if (end of file) { exit loop }
if (ch is a vowel) { increment vowelCount } else if (ch is a consonant) { increment consonantCount }}close fileoutput consonantCount, vowelCount
Read input from file?
Algorithm (cont)
![Page 22: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/22.jpg)
22
open file for inputset consonantCount to 0set vowelCount to 0loop{ input ch if (end of input) { exit loop }
if (ch is a vowel) { increment vowelCount } else if (ch is a consonant) { increment consonantCount }}close fileoutput consonantCount, vowelCount
First, implement with input from stdin stream. Once working,
modify to get input from file
Algorithm (cont)
![Page 23: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/23.jpg)
23
#include <stdio.h>
int main(){ int consonantCount, vowelCount ; char ch ;
consonantCount = 0 ; vowelCount = 0 ;
printf("\nInput has %d consonants and %d vowels.\n", consonantCount, vowelCount) ;
return 0;}
Program
![Page 24: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/24.jpg)
24
consonantCount = 0 ; vowelCount = 0 ; /* For each character in the file in turn, test if it is a consonant or a vowel, and adjust the total accordingly */
while ( scanf("%c", &ch) != EOF ) {
}
printf("\nInput has %d consonants and %d vowels.\n", consonantCount, vowelCount) ;
Program
![Page 25: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/25.jpg)
25
/* For each character in the file in turn, test if it is a consonant or vowel, and if so, adjust total accordingly */
while ( scanf("%c", &ch) != EOF ) { if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U') { /* Vowel */ vowelCount++ ; } else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { /* Consonant, since vowels already dealt with */ consonantCount++ ; } }
Program
![Page 26: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/26.jpg)
26
#include <stdio.h>
/* Count the number of vowels, and the number of consonants in the input. */
int main(){ int consonantCount, vowelCount ; char ch ;
consonantCount = 0 ; vowelCount = 0 ;
/* For each character in the file in turn, test if it is a consonant or vowel, and if so, adjust total accordingly. */
while ( scanf("%c", &ch) != EOF ) { if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U') { /* Vowel. */ vowelCount++ ; } else if ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ) { /* Consonant, since vowels already dealt with. */ consonantCount++ ; } }
printf("\nInput has %d consonants and %d vowels.\n", consonantCount, vowelCount) ;
return 0;}
vowel1.c
Modify to get input from file?
![Page 27: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/27.jpg)
27
#include <stdio.h>
int main(){ int consonantCount, vowelCount ; char ch ;
consonantCount = 0 ; vowelCount = 0 ;
printf("\nFile has %d consonants and %d vowels.\n", consonantCount, vowelCount) ;
return 0;}
while ( scanf("%c", &ch) != EOF ){
}
...etc...etc...etc...
![Page 28: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/28.jpg)
28
Input file stream (aka “file pointer”)
#include <stdio.h>
int main(){ FILE *inputFile ; int consonantCount, vowelCount ; char ch ;
consonantCount = 0 ; vowelCount = 0 ;
inputFile = fopen("yourFile.txt", "r") ;
printf("\nFile has %d consonants and %d vowels.\n", consonantCount, vowelCount) ;
fclose(inputFile) ; return 0;}
while ( fscanf(inputFile, "%c", &ch) != EOF ){
}
...etc...etc...etc...
![Page 29: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/29.jpg)
29
#include <stdio.h>
int main(){ FILE *inputFile ; int consonantCount, vowelCount ; char ch ;
consonantCount = 0 ; vowelCount = 0 ;
inputFile = fopen("yourFile.txt", "r") ;
printf("\nFile has %d consonants and %d vowels.\n", consonantCount, vowelCount) ;
fclose(inputFile) ; return 0;}
while ( fscanf(inputFile, "%c", &ch) != EOF ){
}
...etc...etc...etc...
Declare file pointer
Open file for input
Read input from file
Close file
![Page 30: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/30.jpg)
30
#include <stdio.h>/* Count the number of vowels, and the number of consonants in the file. */
int main(){ FILE *inputFile ; int consonantCount, vowelCount ; char ch ;
inputFile = fopen("yourFile.txt", "r") ;
consonantCount = 0 ; vowelCount = 0 ;
/* For each character in the file in turn, test if it is a consonant or vowel, and if so, adjust total accordingly. */
while ( fscanf(inputFile, "%c", &ch) != EOF ) { if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U') { /* Vowel. */ vowelCount++ ; } else if ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ) { /* Consonant, since vowels already dealt with. */ consonantCount++ ; } }
printf("\nFile has %d consonants and %d vowels.\n", consonantCount, vowelCount) ;
fclose(inputFile) ;
return 0;}
vowel2.c
More on file input/output in
Lecture 14
![Page 31: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/31.jpg)
31
Example 2: Factorization
• Write a program which prints out the prime factorization of a number (treat 2 as the first prime)
• For example,
on input 6, desired output is: 2 3
" " 24, " " : 2 2 2 3
" " 14, " " : 2 7
" " 23, " " : 23 (23 is prime)
![Page 32: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/32.jpg)
32
input n
set factor to 2
Algorithm
![Page 33: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/33.jpg)
33
input n
set factor to 2
while(some factor yet to try){
}
Algorithm (cont)
![Page 34: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/34.jpg)
34
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)
![Page 35: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/35.jpg)
35
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)
![Page 36: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/36.jpg)
36
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?
![Page 37: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/37.jpg)
37
#include <stdio.h>
/* Print out the prime factors of a number */
int main(){ int n, factor ;
return 0;}
Program
![Page 38: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/38.jpg)
38
#include <stdio.h>
/* Print out the prime factors of a number */
int main(){ int n, factor ;
printf("\nEnter integer: ") ; scanf("%d", &n) ;
return 0;}
Program (cont)
![Page 39: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/39.jpg)
39
#include <stdio.h>
/* 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)
![Page 40: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/40.jpg)
40
/* Try each possible factor in turn */
factor = 2; while ( factor <= n && n > 1 ) {
}
![Page 41: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/41.jpg)
41
/* Try each possible factor in turn */
factor = 2; while ( factor <= n && n > 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 ; }
}
![Page 42: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/42.jpg)
42
/* Try each possible factor in turn */
factor = 2; while ( factor <= n && n > 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++ ; } }
![Page 43: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/43.jpg)
43
/* Try each possible factor in turn */
factor = 2; while ( factor <= n && n > 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++ ; } }
![Page 44: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/44.jpg)
44
#include <stdio.h>
/* 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 <= n && n > 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
![Page 45: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/45.jpg)
45
/* Try each possible factor in turn */
factor = 2; while ( factor <= n && n > 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?
![Page 46: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/46.jpg)
46
/* Try each possible factor in turn */
for ( factor = 2; factor <= n && n > 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++ ; } }
![Page 47: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/47.jpg)
47
#include <stdio.h>
/* 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 <= n && n > 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
![Page 48: 1 CSE1301 Computer Programming Lecture 11: Iteration (Part 2)](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d4a5503460f94a276e7/html5/thumbnails/48.jpg)
48
Reading
• Deitel &Deitel Chapter 4, – Exercises 4.16, 4.36