0% found this document useful (0 votes)
92 views13 pages

Program 4.1 Input: Laboratory 5

This document discusses loops and repetition structures in programming. It includes examples of different loop structures like for, while, and do-while loops. It also compares the differences between loop structures and discusses how loops allow programs to repeat simple operations to produce complex outputs. The examples calculate sums and display numbers in different patterns to demonstrate how loops work.

Uploaded by

Davidz Gan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views13 pages

Program 4.1 Input: Laboratory 5

This document discusses loops and repetition structures in programming. It includes examples of different loop structures like for, while, and do-while loops. It also compares the differences between loop structures and discusses how loops allow programs to repeat simple operations to produce complex outputs. The examples calculate sums and display numbers in different patterns to demonstrate how loops work.

Uploaded by

Davidz Gan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Laboratory 5 Program 4.

1 Input

Output

Change the program to display output number 2 to 5 Input

Output

Change the program to display output number 6 to 10 Input

Output

Program 4.2 Input

Output

Change the program to sum number 3 to 7 Input

Output

Change the program to sum number 5 to 11 Input

Output

Program 4.3 Input

Output

Change the program to display output number 1 to 50 Input

Output

Change the program to display output number 10 to 100 Input

Output

Program 4.4 Input

Output

Change the program to make outside loop 6times and inside loop 3 times Input

Output

Exercise 1 1. Write a program to calculate sum of number and multiple of number. Example output: Sum of number 1 to 7 = 28 Multiply of number 1 to 7 = 5040

Input

Output

Exercise 2 1. Write a program asking user to insert how many number they want to display symbol $. Example: How many tome to display symbol,,$?: 7 $$$$$$$

Input

Output

Exercise 3

Input

Output

Questions 2. What is the function of ,,for statement. For: For statement are also called entry controlled or close ended because the condition for iteration of loop is check at the string of the loop and loop will not execute even once if the condition will to satisfy even once. 3. Write a discussion in every program above. Program 4.2 -We can learn the function of dowhile. This is a program for loop control structure. This program will automatically loop without need to input anything. It can loop anything such as number. If we want to calculate the sum of number 1 to number 10, we can use this statement to calculate it. We must use additional operation in order to calculate the sum of number 1 to number 7. The output of this program should be: The sum of 1 to 10 is 55. Program 4.3 -This program using for statement. For and dowhile statement has the same function which is loop control structure. But for statement is more simple than dowhile and very easy to use. The output of this program is 1,2,3,4,5,6,7,8,9 because we choose to display digit 1 to 9. Program 4.4 - This program also using for statement but is has two times of loop control structure. There are two variables in this program which is in_loop and out_loop. In_loop control the number of output in a row and out_loop control the number of row. 4. Compare the differences between the various loop structure.

In this lab, we use three types of loop control structure which is while, dowhile and for. All of these statement has a same function but different format and for statement is the easiest to use. For an example: for(digit=0;digit<=9;++digit) do { total +=n; n++; } while (n<=limit);

Conclusion After do this LAB 5 (Repetition Structure) we know how to writing C program using looping statements. Loops are used to repeat a block of code. Being able to have your program repeatedly execute a block of code is one of the most basic but useful tasks in programming -- many programs or websites that produce extremely complex output (such as a message board) are really only executing a single task many times. (They may be executing a small number of tasks, but in principle, to produce a list of messages only requires repeating the operation of reading in some data and displaying it.) Now, think about what this means: a loop lets you write a very simple statement to produce a significantly greater result simply by repetition.

Next, we know how to describe the different type of looping structures: For While, dowhile Nested looping We also know how to compare the difference between the various loop structures.

You might also like