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

Lab06 C#

lab06 c#

Uploaded by

ahmad bataineh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lab06 C#

lab06 c#

Uploaded by

ahmad bataineh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

204111 – Computer and Programming Section 451

Lab #6 – Repetition: Part II

Student ID Name Signature


Sheet’s Owner
Group partner

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:

1. Counter initialization – usually executed immediately before the loop is entered

2. Body loop – the main part consisting of one or more statements to be executed repeatedly

3. Condition – used to determine whether the body loop should be repeated

4. Counter update – usually a single statement to increase or decrease the value of the counter

Consider the following code for example:


using System ;
class CountDown {
static void Main () {
int i ;
i = 10; // (1) Counter initialization
while ( i >= 10) { // (3) Condition
Console . WriteLine ( i ); // (2) Body loop
i - -; // (4) Counter update
}
}
}

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 ;

Console . Write ( " Enter N : " );


N = int . Parse ( Console . ReadLine ());
for ( i = 1; i <= N ; i ++) {
if ( N % i == 0) Console . WriteLine ( i );
}
}
}

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;

Console . Write ( " Enter N : " );


N = int . Parse ( Console . ReadLine ());
for ( i = 1; i <= N ; i ++) {
if ( N % i == 0) ___ ( a ) ___ ;
}
Console . WriteLine ( " The number of divisors of {0} is {1}. " ,
__ ( b ) __ , __ ( c ) __ );
}
}

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

Task 2.1: Prime Number

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

Write your program in the box below:

Lab #6 5 of 6
204111 – Computer and Programming Section 451

Task 2.2: Triangle Stars

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

Write your program in the box below:

Lab #6 6 of 6

You might also like