You are on page 1of 7

DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINNERING

EEE3123
COMPUTER PROGRAMMING
GROUP 1
LAB 5: LOOP STRUCTURES [WHILE STATEMENT]

NAME : MD AZIZUL HOQUE


MATRIC NO : 200191
LECTURER : ASSOC. PROF. DR. WAN ZUHA BIN WAN HASAN
SUBMSSION DATE : 01.12 .2020
Objective: To study and apply the concept of a repetitive statement.
To distinguish between the decision-making tool in the form of while loop.

Equipment: Personal Computer with C Program Pre-Installed.

Introduction:
Loop is used to execute the block of code several times according to the
condition given in the loop. It means it execute same code multiple times so it saves
code and also helps to traverse the elements of array. While loop execute the code until
condition is false.

Methodology:

1. Write a program that calculates the squares and cubes of the numbers from 0 to 10
using while loop and uses tabs to print the values shown in Table 5.1.

Table 5.1: Square and Cube of a Number.


Number (n) Square (n2) Cube (n3)
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000

2. Write a loop that will calculate the sum of every third integer, beginning with i = 2
(i.e, calculate the sum 2+5+8+11+…) for all values of i that are less than 100. Write
the loop using a while statement.
QUESTION 1:
1. START
2. INPUT THE VALUE OF X
3. IF THE VALUE OF X<11, CALCULATE THE VALUE OF a, b
4. END

CODING:
#include <stdio.h>
#include <stdlib.h>

int main ()
{
int x,a,b;
x=0;

while(x<11)
{
a=x*x;
b=x*x*x;
printf("%d\t%d\t%d\n",x,a,b);
x=x+1;
}

return 0;
}
Listing 1.1: Program code for calculating the total of the given function using the while
statement.

Listing 1.2: Total value of the given function using the while statement.
QUESTION 2:
1.START
2. INPUT THE VALE OF I, j
3. CALCULATE THE TOTAL
4 ENDS

CODING
#include <stdio.h>
#include <stdlib.h>

int main ()
{
int i=2;
int j=3;
int total=0;

while(i<100)
{
printf("%d\t”, i);
total=total+i;
i=i+j;
}
printf("\nThe total value for all of these values are %d",total);
return 0;
}
Listing 2.1: Program code for the calculating total the third integer until 100 using the
while statement.

Listing 2.2: Calculating total of the third integer until 100 using the while statement.
Discussion:
In the question1, instructed to find the value for 0-10 in square & m^3. To find the value
for each statement, we have to use the while loop because we know in while loop the
function can be executively until 11 to achieve our goal. So, in the first loop, we have
entered the value for a and b with one condition. To repeat the program until 10 so, we
made another while loop with condition x>11 which means that the program will end or
will stop executing when it reaches 10 that’s how we have got the desired output given
in the questions which is shown in the listing 1.2.

In the question 2, asked to find the third integer value where value starts with =2 given.
Here we can make a loop with a condition where have entered two integer value with
while loop condition x>100 so it executes the program until it reaches 100. Had to make
another loop because the instruction asked to calculate the total value of the third
integer until it reaches 10. So, while loop has been used to perform repeated instruction
in the second question which is shown in the figure 2.1 and 2.2.

CONCLUSION:

In conclusion, adept use of loops can enable the process that requires repetition of
statements which can be very useful when it comes to programs that involve the
process of counting, timers, and many more.

REFERENCE:

1.Lab manual 5, EEE3123- Computer Programming.

You might also like