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

Eng3202 Lab Report 6 - Loop Structures [Do-while Statement]

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

Eng3202 Lab Report 6 - Loop Structures [Do-while Statement]

Eng3202 Lab report
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Lab-Report 6

ENG3202
Name: Fakhrul Hakim bin Anwar
No. Matrik: 228724

December 04

COMPUTER PROGRAMMING

1
LAB 6: LOOP STRUCTURES [DO-WHILE STATEMENT]

Objective To study and apply the concept of repetitive statement


To distinguish between the decision making tool in the form of do..while

Equipment Personal Computer with C Program Pre-Installed

General guideline for all lab exercises and reports:


a. Generate a pseudocode for the program.
b. Generate a flow chart for the program (Only if flowchart has already been
covered in lecture)
c. Provide relevant comments on the codes
d. In the case of compiling given program, run the program and record any errors
encountered.
e. Debug the error, compile and run the program. Record the program output.
f. If you are required to design a program, design working codes, debug and run as well
as show the output.
g. Provide discussion and analysis of your results.
h. You may copy the program and results images or screenshot from the IDE and paste it
into your report

Questions

1. Write a program that calculates two values, v1 and v2 from numbers 0 to 10 using
do.. while loop and uses tabs to print the values. The formula and calculated values
are shown in Table 6.1.

Table 6.1
Number (n) v1=3(n+n) v2=3(n+n)+n
0 0 0
1 6 7
2 12 14
3 18 21
4 24 28
5 30 35
6 36 42
7 42 49
8 48 56
9 54 63
10 60 70

2. Write a loop that will calculate the sum of every second integer, beginning with i =
1 (i.e, calculate the sum 1+3+5+7+…) for all values of i that are less than 250. Write
the loop using a do.. while statement.

-end-
2
3. Write a program that calculates two values, v1 and v2 from numbers 0 to 10 using
do.. while loop and uses tabs to print the values. The formula and calculated values
are shown in Table 6.1.

Table 6.1
Number (n) v1=3(n+n) v2=3(n+n)+n
0 0 0
1 6 7
2 12 14
3 18 21
4 24 28
5 30 35
6 36 42
7 42 49
8 48 56
9 54 63
10 60 70

Pseudocode:
1) Start the program
2) Set the variable n, v1, and v2 as an integer and set n=0, v1=0, v2=0.
3) Print, n (TABS) v1 (TABS) v2 (NEW LINE)"
4) Do print n (TABS) v1 where v1 is v1=3(n+n) (TABS) v2= v1 + n
5) Adds n by 1
6) While n is less or equal to 10.

7) End the program.

3
Flow Chart

4
5
1. Write a loop that will calculate the sum of every second integer, beginning with i =
1 (i.e, calculate the sum 1+3+5+7+…) for all values of i that are less than 250. Write
the loop using a do.. while statement.

Pseudocode
1) Start the program
2) Set the variable i and sum as integer and set it initial value as i=1 and sum=0
3) Do calculate sum = sum + i
4) Calculate i = i + 2
5) While i is less than 250

6) Print the value of sum

7) End program

6
Flow Chart

7
8
Discussion and analysis

Question 1
The program uses do…while to calculate, this way we can calculate v1 and v2 easily and easy to
understand than using while only. Beginners can easily understand the code. It is efficient and we
don’t need to calculate it over and over , we just have to simply put one formula and it will loop
until n is less or equal to 10 and calculate v1 and v2 for all value of n.

Strengths:
• Clear structure:
Uses of do…while loop provides a simple and intuitive structure, making the code easy
to understand for beginners.
• Accurate calculation:
V1 and v2 are computed directly using basic arithmetic operations, ensuring
correctness without the need for additional libraries or complex logic.
• Formatted Output:
Tab characters (\t) are used to align the output into neat columns, improving
readability and making the results visually appealing.
Limitation:
• Number of outputs:
We can make it to show how many outputs as much as we can, not just n<=10
only.

Question 2
This program calculates the sum of a sequence of numbers starting at 1 and adding every second
number (e.g., 1, 3, 5, 7, ...) until the numbers exceed 250. It uses a do…while loop to handle the
iteration and stops when the condition i > 250 is reached.

Strengths:
• Simple and Effective Logic:
The program is straightforward. It starts with i = 1 and increments i by 2 in each loop,
adding the value to sum. This makes it easy to understand and implement.
• Clear Output:
The result is printed at the end of the loop, showing the total sum in a way that’s
simple to interpret.
Limitation:
• Hardcoded Range:
The condition i <= 250 is fixed in the code, which limits flexibility. If a different
upper bound is desired, it requires modifying the source code. Making the upper
bound configurable (e.g., through user input) could improve usability.

9
Conclusion:
1) The programs work fine, the do…while and the output does exactly what it should be
doing. A bit of improvement would be the number of outputs depends on the user,
The user can make the output as many output they want.
2) This program works fine , but it limits the sum only when i is <=250, we can try
making it for the users to easily just how much of sum they want. Or this can be done
using Arithmethic progression formula , but we don’t have the number of terms ,of
the value of i that is closest to the <= 250, So the AP formula wasn’t so helpful. With
this do…While we can make it happens.

10

You might also like