0% found this document useful (0 votes)
5 views

week7-loop

This document covers the topic of loops in programming, specifically focusing on the 'for' repetition structure, its syntax, and how it can be rewritten as a 'while' loop. It includes various programming exercises with solutions that demonstrate the use of loops for tasks such as printing integers, generating triangles, finding prime numbers, calculating factorials, and more. Additionally, it discusses nested loops and provides examples for withdrawal calculations using banknotes.

Uploaded by

Ptt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

week7-loop

This document covers the topic of loops in programming, specifically focusing on the 'for' repetition structure, its syntax, and how it can be rewritten as a 'while' loop. It includes various programming exercises with solutions that demonstrate the use of loops for tasks such as printing integers, generating triangles, finding prime numbers, calculating factorials, and more. Additionally, it discusses nested loops and provides examples for withdrawal calculations using banknotes.

Uploaded by

Ptt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Week 7

Loop
Topic of this week

• Loop
• for Repetition Structure
• Notes and Observations
• Programming Exercises

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
for Repetition Structure

• Structure
for(initialization; loopContinuationTest; increment)
statement

Example:
for( counter = 1; counter <= 10; counter++ )
printf( "%d\n", counter );
No
• Prints the integers from one to ten. semicolon
after last
expression

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
for Repetition Structure

• for loops can usually be rewritten as while loops:


initialization;
while (loopContinuationTest){
statement
increment;
}

• Initialization and increment: can be comma-separated lists


for (int i = 0, j = 0; j + i <= 10; j++, i++)
printf( "%d\n", j + i );

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
for Structure: Notes and Observations

• Arithmetic expressions
• Initialization, loop-continuation, and increment can
contain arithmetic expressions.
if x = 2 and y = 10
for ( j = x; j <= 4 * x * y; j += y / x )

is equivalent to
for ( j = 2; j <= 80; j += 5 )

• "Increment" may be negative (decrement)


• If loop continuation condition initially false
• Body of for structure not performed
• Control proceeds with statement after for
structure
TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG
School of Information and Communication Technology
Exercise 7.1

• Write a program that prints ten integers and


their squares.
11
24
39
...
10 100

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
Solution

#include <stdio.h>

int main()
{
int i;

for(i = 1; i <= 10; i = i + 1)


printf("%d %d\n", i, i * i);

return 0;
}
TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG
School of Information and Communication Technology
Exercise 7.2

• Write a program that prints out a triangle like:


*
**
***
****
*****
******
*******
********
*********
**********
TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG
School of Information and Communication Technology
Solution
#include <stdio.h>

int main()
{
int i, j;

for(i = 1; i <= 10; i = i + 1)


{
for(j = 1; j <= i; j = j + 1)
printf("*");
printf("\n");
}
return 0;
}
TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG
School of Information and Communication Technology
Exercise 7.3

• Write a program to list prime numbers which


are smaller than 100.

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
Solution

#include <stdio.h>
int main() {
int i, j;
for(i = 2; i <= 100; i ++)
{
for(j = 2; j < i; j++)
{
if(i % j == 0) break;
}
if(j == i)

printf("%d\n", i);
}
return 0;
}

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
Exercise 7.4

• Sometimes we need to have loops within loops,


this is called nested loops. This program
demonstrates how this works. By running it you
should see in what sequence the code is called.

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
exercise7_4.c

#include <stdio.h>

int main()
{
int i, j; /* loop control variables */
printf(” i j\n"); /* prints column labels */
for (i = 1; i < 4; i = i + 1) /* heading of outer for loop */
{
printf("Outer %6d\n", i);
for (j = 0; j < i; j = j + 1) /* heading of inner loop */
{

printf(" Inner%9d\n", j);


} /* end of inner loop */
} /* end of outer loop */
return 0;
}

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
Exercise 7.5

• Write a program that uses for structure to


calculate the factorial of n (i.e., n!).
• Some outputs:

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
Solution

# include <stdio.h>
int main ()
{
int i, n, f;
printf ("Enter n: ");
scanf ("%d", &n);
f = 1; /* 0! */
for (i = 1; i <= n; ++i) {
f *= i; /* Now , f = i! */
}
printf ("%d! = %d\n", n, f);
return 0;
}

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
Exercise 7.6

• In mathematics, a perfect number is a positive integer that


equals the sum of the positive divisors, not including the
number itself. e.g.: 6=1+2+3
• Write a program that lists perfect numbers which is
smaller than inputed N.

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
Solution
#include <stdio.h>
int main()
{
int n, i, j, sum;
printf("\nEnter N= : ");
scanf("%d", &n);
for (i=1; i<=n; i++)
{
sum=0;
for (j=1; j<=i/2; j++)
if (i%j == 0)
sum += j;
if (sum == i)
printf(”%d\n", i);
}
}

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
Exercise 7.7. Reverse number

• Write a program that asks an interger number


and then displays its reverse number. For
example: 34321 → 12343
• The program needs to use for structure

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
Solution
#include <stdio.h>
int main()
{
int number, temp;
int reversednumber;
printf(“Input the number you want to reverse:”);
scanf(“%d”, &number);

for(temp = number, reversednumber=0; temp>0; ) {


reversednumber = reversednumber*10 + temp %10;
temp = temp /10;
}

printf("The reverse number of %d is %d \n", number,


reversednumber);
return 0;
}

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
Solution
#include <stdio.h>
int main()
{
int number, temp =0;
int reversednumber =0;
printf(“Input the number you want to reverse:”);
scanf(“%d”, &number);
temp = number;
do {
reversednumber = reversednumber*10 + temp %10;
temp = temp /10;
}
while (temp >0);
printf(“The reverse number of %d is %d \n”, number,
reversednumber);
return 0;
}

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
Exercise 7.8

• Given banknotes of 500,000, 200,000,


100,000, 50,000, 20,000, 10,000 and 5,000
• Write a program to list the possibilities of
withdrawals. For example:
• 25000 = 1 banknote 20000 + 1 banknote 5000
= 1 banknote 10000 + 3 banknotes 5000
= 2 banknotes 10000 + 1 banknote 5000
= 5 banknotes 5000

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
Solution

#include <stdio.h>
int main()
{
int count=0, number;
int n500, n200, n100, n50, n20, n10, n5;
printf("Input the amount you want to withdraw:");
scanf("%d", &number);
for(n500=0; n500<=number/500000; n500++)
for(n200=0; n200<=(number-500000*n500)/200000; n200++)
for(n100=0; n100<=(number-500000*n500-200000*n200)/100000; n100++)
for(n50=0; n50<=(number-500000*n500-200000*n200-100000*n100)/50000; n50++)
for(n20=0; n20<=(number-500000*n500-200000*n200-100000*n100-50000*n50)/20000; n20++)
for(n10=0; n10<=(number-500000*n500-200000*n200-100000*n100-50000*n50-
20000*n20)/10000; n10++){

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
{
n5= (number-500000*n500-200000*n200-100000*n100-50000*n50-20000*n20-10000*n10)/5000;
if(number==-500000*n500+200000*n200+100000*n100+50000*n50+20000*n20+10000*n10+5000*n5){
count++;
printf("Cach %d: ",count);
if(n500>0) printf("%d to 500.000\t", n500);
if(n200>0) printf("%d to 200.000\t", n200);
if(n100>0) printf("%d to 100.000\t", n100);
if(n50>0) printf("%d to 50.000\t", n50);
if(n20>0) printf("%d to 20.000\t", n20);
if(n10>0) printf("%d to 10.000\t", n10);
if(n5>0) printf("%d to 5.000\t",n5);
printf("\n");
}
}
return 0;
}

}
TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG
School of Information and Communication Technology
Exercise 7.9

• Modify the program you have developed for the


exercise 7.3 so that:
• It asks the user to input the number n
• If n = -1 then exit the program
• Otherwist, list all prime number from 1 to n

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
Exercise 7.10

• Modify the program you have developed for the


exercise 7.9 so that:
• Before exiting the program, ask the user to continue
or stop the program
• If the user chooses Y or y, repeat the program
• If the user does not choose Y or y, exit the program

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
Control the for loop with character input

char ch=‘y’;
for ( ; ch==‘y’ || ch!=‘Y’; ){
// do something

printf(“Continue or not (y/n):”);


ch=getchar();
}

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
Exercise 7.11

• Given banknotes of 500,000, 200,000,


100,000, 50,000, 20,000, 10,000 and 5,000
• Write a program that lists the possibilities of
withdrawals with the least amount of
banknotes. For example:
• 25000 = 1 banknote 20000 + 1 banknote 5000

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG


School of Information and Communication Technology
Exercise 7.12

• Write a program that has the following menu

Withdrawal program
1. List all possibilities
2. List all possibilities that use the smallest number of
banknotes
3. Exit
What is your selection:

If the user selects 1, then does the exercise 7.8


If the user selects 2, then does the exercise 7.10
If the user selects 3, then exits the program
TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG
School of Information and Communication Technology

You might also like