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

Lab 1-5

The document describes a lab experiment on C programming. [1] The objective is to introduce C language fundamentals and debug simple programs. [2] Four program listings are provided with errors for students to detect and fix. [3] The student documents fixing the errors in each program and getting the correct output, demonstrating learning of debugging skills.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Lab 1-5

The document describes a lab experiment on C programming. [1] The objective is to introduce C language fundamentals and debug simple programs. [2] Four program listings are provided with errors for students to detect and fix. [3] The student documents fixing the errors in each program and getting the correct output, demonstrating learning of debugging skills.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINNERING

EEE3123
COMPUTER PROGRAMMING
GROUP 1
LAB 1: INTRODUCTION TO C PROGRAMMING

NAME : MD AZIZUL HOQUE


MATRIC NO : 200191
LECTURER : ASSOC. PROF. DR. WAN ZUHA BIN WAN HASAN
SUBMSSION DATE : 01.12 .2020
Objective:

➢ To introduce C language.
➢ To edit, create, compile, execute and debug a simple text output program written
in C language.

Equipment: Personal Computer with C Program Pre-Installed

Introduction:

A C program is generally formed by a set of functions, which subsequently consist of


many programming statements. Using functions, a large computing task can be broken
into smaller ones. Functions can be created to execute small, frequently-used tasks. In
C, there are predefined functions or sometimes called standard functions, and user -
defined functions.

Methodology:
1. Locate and execute C program in your computer.

2. Enter listing 1.1 into the editor. Compile and run the program. Record errors
encountered.
Listing 1.1
/* This program is to convert the temperature in Celsius to its corresponding
Fahrenheit value. Happy trying! */

#include <stdio.h>

int main (void)

{
/* local declarations */
float celsius;
float fahrenheit;

/* prompt the user to key in the temperature in Fahrenheit */


printf(“Enter temperature in Celcius: “);

/* the value is passed into the variable named Fahrenheit */


scanf(“%f”, &celcius);

/* computation of the conversion */


fahrenheit = ((9.0 / 5.0) * celcius) + 32;

/* displaying the corresponding Celsius value */


printf(“the value in Fahrenheit is: %f”, fahrenhiet);

return 0;
}
3. Redo step 2 using listing 1.2, 1.3 and 1.4.
Listing 1.2

/* this is an example program


*
#include <stdio.h>
main
() {int num; printf (“This is”);
printf (“a program”
); printf
(
“that does not”
);printf(
“look too good.”
);
Printf
(“Enter a number >”);scanf(
“%d”,&num);printf
(“This is number%d”,
num);return
0;}

Listing 1.3
/* this program has error(s)
#include <stdio.h>

int main (void)


{
printf “hello THERE !!”);
printf(‘we are to find’];
printf (‘the error’);
return 0
}

Listing 1.4

/this is another program with some errors in it and needs to be corrected/

#include (stdio.h)

void main (void)


{
/*local variable declarations

a = 3 int;
b = 5.3 float, double;
c,d = a,b character;

/*statements
printf (The values are: , a,b,c,d);
printf(“the end of the program’)

return 0;
}/*main*/
Listing 1.1:

CODING

#include <stdio.h>

int main (void)

/* local declarations */

float Celsius;

float Fahrenheit;

/* prompt the user to key in the temperature in Fahrenheit */

printf("Enter temperature in Celsius: ");

/* the value is passed into the variable named Fahrenheit */

scanf("%f", & Celsius);

/* computation of the conversion */

Fahrenheit = ((9.0 / 5.0) * Celsius) + 32;

/* displaying the corresponding Celsius value */

printf("the value in Fahrenheit is: %f", Fahrenheit);

return 0;

}
Figure 1: Listing1.1, Program code after the detection of errors.

Figure 2: Listing 1.1, output of the amended program.


Listing 1.2:

CODING
#include <stdio.h>
int main()
{
int num;
printf ("This is ");
printf ("a program ");
printf("that does not ");
printf("look too good.");
printf("Enter a number > ");
scanf("%d",&num);
printf("This is number %d",num);
return 0;
}

Figure 3: Listing1.2, Program code after the detection of errors.


Figure 4: Listing 1.2, output of the amended program.

LISTING 1.3:

Coding
#include <stdio.h>

int main (void)


{
printf("hello THERE !!");
printf("we are to find ");
printf("the error.");
return 0;
}
Figure 5: Listing1.3, Program code after the detection of errors.

Figure 6: Listing 1.3, output of the amended program.

Listing 1.4:
Coding
#include<stdio.h>
int main(void)
{
int a=3;
double b=5.3;
float d=b;
char c=a;
printf ("the values are:%d ,%f, %d, %f ",a , b, c, d);
printf (" the end of the program ");
return 0;

Figure 1: Listing1.4, Program code after the detection of errors.

Figure 8: Listing 1.4, output of the amended program.


Discussion:

In the listing 1.1, was given a program mainly with so many spelling errors. In the figure
1, all the spelling errors were fixed and after the fixation of the program, I was able to run
the program and I got the mathematically accurate output which is given in the figure 2.
In the listing 2.2, was given a program which is not organized in order to run the program,
I had to organize the program in right c programming manner else it would not run
properly. I was able to run the program once I organized the coding program and fixed all
the existing errors in the listing 2.2. In figure 3, correct coding of the program is written
and once I run the program, I got the desired output which is stated in the figure 4. In the
listing 1.3, there was an error in the line 5 where a first bracket was missing after the printf
and also in the line 6, there was a third bracket instead of the first bracket and there was
some problem with the right spacing which was also amended. Once I corrected all the
errors, I was able to run the program which is stated in the figure 5 and 6. At last in the
listing 1.4, there was errors in in the beginning of the program and also in the declaration
of integer value, float value and char value. Those values were not declared properly after
the correction of the program, I was able to run it also able to get the desired output which
implies that I have detected all the errors in given flawed program and able to run it
properly.

Conclusion:

In this lab, I was introduced to the basic fundamentals of the C programming and I was
able to detect the errors and solve the problem in C programming.

REFERENCE:

1.Lab manual 1, EEE3123- Computer Programming.


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINNERING
EEE3123
COMPUTER PROGRAMMING
GROUP 1
LAB 2: USER INPUT

NAME : MD AZIZUL HOQUE


MATRIC NO : 200191
LECTURER : ASSOC. PROF. DR. WAN ZUHA BIN WAN HASAN
SUBMSSION DATE : 01.12 .2020
Objective: To create a program in C language that allows numerical user input.
To create a program in C language that calculates a value from user input.

Equipment: Personal Computer with C Program Pre-Installed

Introduction:

Arithmetic operators, in C, are operators used to perform arithmetic operations that


include multiplication, division, addition and subtraction. With the exception of the
subtraction operator, where "-" is used to indicate a negative number, arithmetic operators
are binary operators that take two operands. The operands are of numeric data type, and
perform in a similar manner as in other languages such as C and C++.

Arithmetic operators "+" and "-" are used to manipulate pointers by adding or subtracting
the numeric value to or from the pointers without generating any exception during
overflow of the of the pointer's domain. Arithmetic operators can be overloaded when
used with user-defined types to extend the nature of normal operators, thus providing
additional functionalities.

Methodology:

1. Write a program that will prompt the user to input the height, length and width of a
box in centimeters. From the user input the program must calculate and display
box’s volume.

2. Write a program that simulates an odometer. The user inputs the speed (in kmph)
and time the vehicle has traveled (in seconds!!). From the user input, the
program must calculate and display the distance traveled. The program must
also display the speed and time entered by the user.

3. Given by a user are the x- and y- coordinates of two points in a plain: (x1, y1) and
(x2, y2). Write a C program that will compute and print the distance between
these two points using given below.

Distance = (x1 − x2 )2 + ( y1 − y 2 ) 2
QUESTION 1

PSEUDOCODE:

1.START

2. Enter the value of height, width and length

3. Calculate the volume

4. End

CODING:

#include<stdio.h>

#include<stdlib.h>

int main ()

float Height,Length, Width, TotalVolume;

printf("calculate a box volume");

printf("\n make sure the input value is in centimeters");

printf("\ninput the box height");

scanf("%f",&Height);

printf("input the box length");

scanf("%f",&Length);

printf("input the box width");

scanf("%f",&Width);

TotalVolume=Height*Width*Length;
printf("the total volume of the box is %f", TotalVolume);

return 0;

Listing 1.1: Program code for the arithmetic operation.

Listing 1.2: Total volume of the box.


QUESTION 2:

PSEDUCODE:

1. START
2. ENTER THE SPEED, TIME
3. CALCULATE THE DISTANCE
4. END

CODING

#include <stdio.h>
#include <stdlib.h>
int main ()
{
float speed, time,T,distance;

printf("Input speed in km/h\n");


scanf("%f",&speed);
printf("Input time in seconds\n");
scanf("%f",&time);
T=time/3600;

distance=speed/T;

printf("your distance in km is %f",distance);

return 0;
}
Listing 2.1: Program code for the arithmetic operation.

Listing 2.2: calculating the distance using the arithmetic operation.


QUESTION 3:

PSEUDOCODE

START

ENTER THE VALUE OF x1, y1, x2 AND y2

CALCULATE THE DISTANCE

a = pow((x1-x2),2);

b = pow((y1-y2),2);

dis = pow((a+b),0.5);

PRINT DISTANCE

END

CODING:

#include <stdio.h>

#include <math.h>

int main()

int x1,y1,x2,y2;

float a,b,dis;

printf("Input X1: ");

scanf("%d",&x1);

printf("Input Y1: ");

scanf("%d",&y1);

printf("Input X2: ");


scanf("%d",&x2);

printf("Input Y2: ");

scanf("%d",&y2);

a = pow((x1-x2),2);

b = pow((y1-y2),2);

dis = pow((a+b),0.5);

printf("Distance: %f",dis);

return 0;

Listing 3.1: Program code to calculate distance between the x and y coordinates using
the arithmetic operation in C.
Listing 3.2: The distance between x and coordinates.

DISCUSSION:

In the question 1, I input the value of the height, length and width in the program using
the function of Scanf and used the function of the Printf to print the result. According to
the instruction to calculate the volume which is height*width*length were declared in the
program later, I used the function of Printf to print or display the output. In the listing 1.1,
full coding of the program to calculate volume is given and once it executed, desired
output achieved in the listing 1.2. In the question 2, to calculate the distance, the float
value of time and speed entered in the program using the scanf function. As, the
instruction asked the speed were given kmph and time were given in the program in
seconds. After establishing the full program, I was able to calculate distance which is
stated in the Listing 2.1 and Listing 2.2. In the question 3, to calculate the distance
between two coordinate x and y at first declared the function respectively a = pow((x1-
x2),2), b = pow((y1-y2),2) and dis = pow((a+b),0.5 to make it easy to solve the arithmetic
function of c program. Scanf function allowed to input value of x1, x2, y1 and y2. Finally,
I used the printf function to print out the distance between two coordinates which is given
in the listing 3.1 and 3.2. In this lab, I was able to perform the arithmetic operation using
the scanf and printf function.

CONCLUSION:

In conclusion, arithmetic operations enable us to operate numerical user input and


make calculations using the input given by the user, rendering many fruitful future
possibilities with various benefits such as efficient yet convenient calculation process.

REFERENCE:

1.Lab manual 2, EEE3123- Computer Programming.


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINNERING
EEE3123
COMPUTER PROGRAMMING
GROUP 1
LAB 3: SELECTION STRUCTURES

NAME : MD AZIZUL HOQUE


MATRIC NO : 200191
LECTURER : ASSOC. PROF. DR. WAN ZUHA BIN WAN HASAN
SUBMSSION DATE : 01.12 .2020
Objective: To write, compile and execute a program that uses floating point numbers.

Equipment: Personal Computer with C Program Pre-Installed

Introduction:

If else statements in C is also used to control the program flow based on some
condition, only the difference is: it's used to execute some statement if the expression is
evaluated to true, otherwise executes else statement in the C programme.

Methodology:

1. Write, compile and execute a C program that will

Prompt the user for 5 floating numbers as shown below.

Enter number 1:

Enter number 2:

….

Enter Number 5:

Find the minimum and maximum values among these 5 numbers using if statements

Compute the average from these numbers

Prints “You entered 5 numbers. The minimum is xx, the maximum is yy and the average
is zz”
2. A program that takes the x-y coordinates of a point in the Cartesian coordinate
system and displays the points entered while informing the user in which
quadrant (refer to Fig. 3.1) the point lies in is written.

QUESTION 1

PSEUDOCODE

1.ENTER

2. INPUT FIVE NUMBER

3. FIND MINIMUM AND MAXIMUM NUMBER

CALCULATE AVERAGE OF 5 NUMBERS

4.END
CODING:

#include <stdio.h>

int main()

float a,b,c,d,e,avg;

printf("Input 1st number: ");

scanf("%f",&a);

printf("Input 2nd number: ");

scanf("%f",&b);

printf("Input 3rd number: ");

scanf("%f",&c);

printf("Input 4th number: ");

scanf("%f",&d);

printf("Input 5th number: ");

scanf("%f",&e);

if(a<=b && a<=c && a<=d && a<=e)

printf("Minimum value is: %f",a);

}
else if(b<=a && b<=c && b<=d && b<=e)

printf("Minimum value is: %f",b);

else if(c<=a && c<=b && c<=d && c<=e)

printf("Minimum value is: %f",c);

else if(d<=a && d<=b && d<=c && d<=e)

printf("Minimum value is: %f",d);

else if(e<=a && e<=c && e<=d && e<=b)

printf("Minimum value is: %f",e);

printf("\n");

if(a>=b && a>=c && a>=d && a>=e)

{
printf("Maximum value is: %f",a);

else if(b>=a && b>=c && b>=d && b>=e)

printf("Maximum value is: %f",b);

else if(c>=a && c>=b && c>=d && c>=e)

printf("Maximum value is: %f",c);

else if(d>=a && d>=b && d>=c && d>=e)

printf("Maximum value is: %f",d);

else if(e>=a && e>=c && e>=d && e>=b)

printf("Maximum value is: %f",e);

avg = (a+b+c+d+e)/5;

printf("\nAverage is: %f",avg);

}
Listing 1.1: Ques no: 1, C coding using the If… else if statement to calculate the
minimum and maximum value
Listing 1.2: Ques no: 1, C coding using the If… else if statement to calculate the
minimum and maximum value

Listing 1.3: The maximum and the minimum value using the if. else if statement
QUESTION- 2

1)

PSEUDOCODE

1. START

2. INPUT THE VALUE OF X AND Y COORDINATE

3. IF (x==0 && y==0)

THEN PRINTF (Center point);

ELSE IF(x == 0)

PRINTF (Over Y axiS);

ELSE IF (y == 0)

PRINTF (Over X axis);

ELSE IF(x>0 && y>0)

PRINTF ("Quadrant 1);

ELSE IF(x<0 && y>0)

PRINTF ("Quadrant 2);

ELSE IF(x<0 && y<0)

PRINTF (Quadrant 3)

ELSE IF(x>0 && y<0)

PRINTF (Quadrant 4);

4.END
CODING:

#include <stdio.h>

int main ()

int x,y;

printf("Input X coordinate: ");

scanf("%d",&x);

printf("Input Y coordinate: ");

scanf("%d",&y);

if(x==0 && y==0)

printf("Center point\n");

else if(x == 0)

printf("Over Y axis\n");

else if(y == 0)

{
printf("Over X axis\n");

else if(x>0 && y>0)

printf("Quadrant 1\n");

else if(x<0 && y>0)

printf("Quadrant 2\n");

else if(x<0 && y<0)

printf("Quadrant 3\n");

else if(x>0 && y<0)

printf("Quadrant 4\n");

return 0;

}
Listing 2.1: Ques no: 2, C coding using the if..else if statement to print the value of x
and y in the right quadrant.
Listing 2.2: Ques no: 2, C coding using the if..else if statement to print the
value of x and y in the right quadrant.

Listing 2.3: Ques no: 2, output value of the x and y in the right quadrant.
Discussion:

In the question1, 5 input numbers were given such as a, b,c,d, and e . To calculate the
minimum and maximum values, I have used the if ...else if statement to check the value
which one larger or which one is smaller. Such as if a>b,c,d, and e. then the Printf will
print the maximum number a and the minimum number is e . The program was written
in a way that it had checked all the value one by one using the else if statement
respectively. So, once we run the program and input the value for a, b,c, d and e it prints
the maximum value and the minimum value in the program which is stead in the listing
no 1.1 and 1.2. In the question 2, was asked to write a program to see where the
coordinate x and y value lies in the quadrant. So, to put the value in the right quadrant,
right condition was given using the if else statement. So , when both x==0 and y==0 it
lies in the center point of the graph else when the X==0 , it will be over the x axis , y==0
it will be over the Y axis and finally if the value of the x>0 and y>0 , the values will be
printed in the first quadrant . I made such condition for the remaining quadrant to check
the where the value of x and y lies in which quadrant. In the listing 2.1 and 2.2, the coding
of the program and the output to print in right quadrant stated. All the asked question was
found in the given lab so, the purpose of finding out the true numbers using the if…else
if statement were achieved.

Conclusion:

As conclusion, I can say that the ‘if-else’ statement is crucial to providing convenience
when it comes to writing, compiling and executing a program with utmost efficiency.

Reference:

1.Lab manual 3, EEE3123- Computer Programming.


DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINNERING
EEE3123
COMPUTER PROGRAMMING
GROUP 1
LAB 4: SWITCH

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 selection statement.


➢ To distinguish between the decision-making tool in the form of if...else and
switch…... Case.

Equipment: Personal Computer with C Program Pre-Installed.

Introduction:

A switch statement allows a variable to be tested for equality against a list of values.
Each value is called a case, and the variable being switched on is checked for
each switch case.

Methodology:

1. Write a program using switch..case that will display a corresponding color code
according to a user input number. The corresponding color code is given in Table 4.1
below.

Table 4.1
0 = Black 3 = Orange 6 = Blue 9 = White
1 = Brown 4 = Yellow 7 = Violet
2 = Red 5 = Green 8 = Gray

2. Rewrite the program in Question 1 using nested if..else statement

Table 4.2
Selangor/Faderal Territory of Kuala Lumpur & Price per m3
Putrajaya

Home (H)
0-20 m3 RM 0.57
More than 20 m3 RM 0.91
Minimum Charge RM 5.00
Industrial (I)
0-35 m3 RM 1.80
More than 35 m3 RM 1.92
Minimum Charge RM 30.00

Commercial (C)
0-35 m3 RM 2.00
More than 35 m3 RM 2.87
Minimum Charge RM 20.00

3. Write a program that will calculate and print out bills for Syarikat Air Selangor Sdn.
Bhd. (SYABAS). The water rates vary depending on whether the bill is for home use,
commercial use, or industrial use (refer to Table 4.2). A code of h means home use, a
code of c means commercial use and a code of I means industrial use. Any other code
should be treated as an error. The water rates are computed as given in the table.

Your program should prompt the user to enter an account number (type int), the code
(type char) and the volume (m3) of water used (type float). Your program should echo
the input data and print the amount due from the user. You must use both if and switch
statements.
RESULT:

QUESTION 1:

PSEUDOCODE

1. START
2. INPUT NUMBER 0 TO 9
3. CHECK THE COLOUR
4. END

CODING:

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

int main()
{
int x;

printf("Input a number from 0-9\n");


scanf("%d",&x);

switch(x)
{
case 0:
printf("Black");
break;
case 1:
printf("Brown");
break;
case 2:
printf("Red");
break;
case 3:
printf("Orange");
break;
case 4:
printf("Yellow");
break;
case 5:
printf("Green");
break;
case 6:
printf("Blue");
break;
case 7:
printf("Violet");
break;
case 8:
printf("Gray");
break;
case 9:
printf("White");
break;
default:
printf("Invalid input");
break;
}

return 0;
}
Listing 1.1: C program code to display corresponding color code using the switch case
statement.
List 1.2: C program code to display corresponding color code using the switch case
statement.

Listing 1.3: Color code display using the switch case statement.
Listing 1.4: Color code display using the switch case statement.

Question 2:
PSEUDOCODE

1. START
2. ENTER THE COLOUR CODE
3. PRINT THE COLOUR

CODING:

#include <stdio.h>
#include <stdlib.h>
int main()
{
int x;

printf("Input a number from 0-10.\n");


scanf("%d",&x);
if(x==0)
printf("Black");
else if(x==1)
printf("Brown");
else if(x==2)
printf("Red");
else if(x==3)
printf("Orange");
else if(x==4)
printf("Yellow");
else if(x==5)
printf("Green");
else if(x==6)
printf("Blue");
else if(x==7)
printf("Violet");
else if(x==8)
printf("Gray");
else if(x==9)
printf("White");
else
printf("Invalid input.");

return 0;
}
Listing 2.1: Program code for the display of corresponding color code using the if…else
if statement.
Listing 2.2: Color code display using the if…. else if statement.

QUESTION 3:

PSEUDOCODE:

1. START
2. CHOOSE ANY CATEGORY LIKE HOME, INDUSTRIAL,
COMMERCIAL
3. ENTER THE VALUE OF USED ELECTRICITY
4. PRINT CALCULATE BILLS
5. END
CODING:

#include <stdlib.h>

char main ()

int acc;

char building,h,i,c;

float vol,x,y,z;

printf("Enter your account number\n");

scanf("%d",&acc);

printf("Your account number is > %d\n",acc);

printf("Type (h)ome / (i)ndustrial / (c)ommercial for your corresponding category.\n");

scanf(" %c",&building);

printf("Your building category is > %c\n",building);

printf("Input volume used(m^3)\n");

scanf("%f",&vol);

printf("Your total volume given is > %f\n\n",vol);

switch(building)

case 'h':

if(vol<=20)

printf("Your total fee for (h)ome is RM5.00");

else if(vol>=20 && vol<=40)


{

x=5+((vol-20)*0.57);

printf("Your total fee for (h)ome is %f",x);

else if(vol>40)

x=5+(20*0.57)+((vol-40)*0.91);

printf("Your total fee for (h)ome is %f",x);

break;

case 'i':

if(vol<=35)

printf("Your total fee for (i)ndustrial is RM30.00");

else if(vol>=35 && vol <=70)

y=30+((vol-35)*1.8);

printf("Your total fee for (i)ndustrial is %f",y);

else if(vol>70)

y=30+(35*1.8)+((vol-70)*1.92);

printf("Your total fee for (i)ndustrial is %f",y);

break;
case 'c':

if(vol<=35)

printf("Your total fee for (c)ommercial is RM20.00");

else if(vol>=35 && vol <=70)

z=20+((vol-35)*2);

printf("Your total fee for (c)ommercial is %f",z);

else if(vol>70)

z=20+(35*2)+((vol-70)*2.87);

printf("Your total fee for (c)ommercial is %f",z);

break;

default:

printf("Your category is invalid.Try again.");

break;

return 0;

}
Listing 3.1: program code to calculate the electricity bills using the switch case and if….
else if statement.
Listing 3.2: Program code to calculate the electricity bills using the switch case and if….
else if statement.
Listing 3.3: Calculation of electric bills for the home using the switch case and if…else if
statement.

Discussion:
In the question 1, instructed to display corresponding color code that can be done using
the “switch case” creating a case for each color which will display the color when the
numbers are pressed or clicked. So, 10 cases were programmed for 10 different colors
which is given in the listing 1.1 and the output of the program is given in the listing 1,2
and 1,3. We can do the program using the if else statement too so , in the if else statement
we could make 10 else if statement or condition so that whenever we press the number
or click them , the right color according the program condition will appear in the output. In
the listing 2.1 and 2.2 shows the program and the output color appears in the screen
when we press any number. In the question 3, instructed to write a program that would
print the electricity bills of home, industry and commercial. Since, we have categories and
the prices varies on the categories we must use the case and also else if statement to
print the bills for the respective categories. The values were entered according to the
graph and it prices. Application of the case and the else if statement, I was able to print
electricity bills for (H)ome and (I)ndustries and (C) ommercial. In the listing 3.1,3.2 and
3.3 stated the written program and the output.
Conclusion:

In this lab, I have learned how we can switch statements to make decision
and setting condition.

Reference:

1.Lab manual 4, EEE3123- Computer Programming.


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