Lab06 C#
Lab06 C#
1. for Loops
It can be observed that when using a while or do-while loop to implement a counting loop,
the C# code can be divided into four parts:
2. Body loop – the main part consisting of one or more statements to be executed repeatedly
4. Counter update – usually a single statement to increase or decrease the value of the counter
Since counting loops are widely used in many programs, C# provides programmers a special
looping statement for conveniently handling this type of loops, called the for statement. The
structure of the for statement is as follows:
for ( init_stmt ; condition ; update_stmt )
statement ;
Use of multiple statements in the body loop is allowed by putting the statements in a block.
for ( init_stmt ; condition ; update_stmt ) {
statement1 ;
statement2 ;
:
statementN ;
}
Note that the condition must be evaluated as true before the first execution of the body loop,
so the operation of the for loop is more similar to the while loop than the do-while loop.
Lab #6 1 of 6
204111 – Computer and Programming Section 451
Example 1.1: The following program will display the numbers 1,2,3,. . .,20 on the screen.
using System ;
class Counting {
static void Main () {
int i ;
for ( i = 1; i <= 20; i ++)
Console . WriteLine ( i );
}
}
Exercise 1.1: The following incomplete program is meant for printing out the num-
ber 0, -1, -2, . . ., -56. What should be put in place of (a), (b), (c) and (d)?
class ForEx {
static void Main () {
int k ;
for ( __ ( a ) __ ; __ ( b ) __ ; __ ( c ) __ ) {
____ ( d ) ____ ;
}
}
}
Blank Expression/Statement
___(a)___
___(b)___
___(c)___
___(d)___
Exercise 1.2: From Exercise 1.1, what should (a), (b), (c) and (d) be to make the
program display 7,14,21,. . .,70 on screen?
Blank Expression/Statement
___(a)___
___(b)___
___(c)___
___(d)___
Lab #6 2 of 6
204111 – Computer and Programming Section 451
Example 1.2: The following program takes an input integer N from the user. Then it will
display all positive integers that are divisors of N .
using System ;
class Divisors {
static void Main () {
int i , N ;
Sample output
Enter N: 100
1
2
4
5
10
20
25
50
100
Exercise 1.3: We now modify the program used in the previous example so that it
counts the number of divisors of N rather than displaying all those divisors. De-
termine an appropriate expression/statement for each of the blanks marked as (a),
(b) and (c) in the incomplete program shown below.
using System ;
class Divisors {
static void Main () {
int i , N , count = 0;
Lab #6 3 of 6
204111 – Computer and Programming Section 451
Sample output
Enter N: 100
The number of divisors of 100 is 9.
Blank Expression/Statement
___(a)___
___(b)___
___(c)___
Lab #6 4 of 6
204111 – Computer and Programming Section 451
2. Programming Tasks
A prime number (simply called a prime for short) is a positive integer that has exactly two
divisors: 1 and itself. For example, 7 is a prime number because it is divisible by only 1 and 7.
The number 10 is not a prime because it has 4 divisors: 1, 2, 5 and 10. And 1 is not a prime
either because it has only one divisor, i.e., 1.
Your task is to write a program that takes a positive integer N and determines whether N
is a prime. (Hint: Modify the program used in Exercise 1.3)
Sample output
Enter N: 10
10 is not a prime
Enter N: 19
19 is a prime
Enter N: 1
1 is not a prime
Lab #6 5 of 6
204111 – Computer and Programming Section 451
Write a program that draws a triangle with stars, whose size is specified by the user’s input.
(Hint: determine the amount of preceding spaces and stars to be printed out on each line.)
Sample output
Enter N: 2
*
**
Enter N: 5
*
**
***
****
*****
Lab #6 6 of 6