web viewwrite a program which displays the word ‘hello’ 4 times on different lines....

5
Chapter 5 : Iteration You often want to perform the same set of instruction several times. This is done by setting up a loop. Sometimes you know how many times you need to repeat the loop. If so, you would use a FOR loop. For counter = start to end Statements Next Type in the following programs and run them Example 1 This program will produce the output shown.

Upload: trinhkiet

Post on 26-Mar-2018

216 views

Category:

Documents


2 download

TRANSCRIPT

Chapter 5 : Iteration

You often want to perform the same set of instruction several times. This is done by setting up a loop.

Sometimes you know how many times you need to repeat the loop. If so, you would use a FOR loop.

For counter = start to end

Statements

Next

Type in the following programs and run them

Example 1

This program will produce the output shown.

Example 2

Now try this program. It will produce the following output

Example 3

Now try this program. It will produce this output.

Example 4

Type in the following program. It uses a FOR loop to enter 8 numbers and add them together.

Exercises

1. Write a program which displays the word ‘Hello’ 4 times on different lines. Remove the word ‘Line’ from the Console.WriteLine statement. What difference does it make?

2. Write a program which will print the squares of all the numbers from 1 to 10. 3. Write a program which will let the user enter a number and will print out a suitable message

this number of times.4. Write a program that prints out the 5 times table.

Amend you program so that you can enter a number (up to 12) and it prints the table for this number

5. n factorial, written n! is the product of all the numbers from 1 up to the number. So 5!=1*2*3*4*5Write a program which calculates and prints 5!.Amend the program so that the user enters a number and calculates and prints the factorial of that number.

6. Write a program which will as the user to enter a number of stars per row and the number of rows to be displayed. Then print this grid of stars.e.g. if they enter 5 and 3, it will print

***************Hint: you will have to use a FOR loop within a FOR loop. Nested loops.