Lab 1-5
Lab 1-5
EEE3123
COMPUTER PROGRAMMING
GROUP 1
LAB 1: INTRODUCTION TO C PROGRAMMING
➢ To introduce C language.
➢ To edit, create, compile, execute and debug a simple text output program written
in C language.
Introduction:
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>
{
/* local declarations */
float celsius;
float fahrenheit;
return 0;
}
3. Redo step 2 using listing 1.2, 1.3 and 1.4.
Listing 1.2
Listing 1.3
/* this program has error(s)
#include <stdio.h>
Listing 1.4
#include (stdio.h)
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>
/* local declarations */
float Celsius;
float Fahrenheit;
return 0;
}
Figure 1: Listing1.1, Program code after the detection of errors.
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;
}
LISTING 1.3:
Coding
#include <stdio.h>
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;
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:
Introduction:
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
4. End
CODING:
#include<stdio.h>
#include<stdlib.h>
int main ()
scanf("%f",&Height);
scanf("%f",&Length);
scanf("%f",&Width);
TotalVolume=Height*Width*Length;
printf("the total volume of the box is %f", TotalVolume);
return 0;
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;
distance=speed/T;
return 0;
}
Listing 2.1: Program code for the arithmetic operation.
PSEUDOCODE
START
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;
scanf("%d",&x1);
scanf("%d",&y1);
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:
REFERENCE:
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:
Enter number 1:
Enter number 2:
….
Enter Number 5:
Find the minimum and maximum values among these 5 numbers using if statements
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
4.END
CODING:
#include <stdio.h>
int main()
float a,b,c,d,e,avg;
scanf("%f",&a);
scanf("%f",&b);
scanf("%f",&c);
scanf("%f",&d);
scanf("%f",&e);
}
else if(b<=a && b<=c && b<=d && b<=e)
printf("\n");
{
printf("Maximum value is: %f",a);
avg = (a+b+c+d+e)/5;
}
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
ELSE IF(x == 0)
ELSE IF (y == 0)
PRINTF (Quadrant 3)
4.END
CODING:
#include <stdio.h>
int main ()
int x,y;
scanf("%d",&x);
scanf("%d",&y);
printf("Center point\n");
else if(x == 0)
printf("Over Y axis\n");
else if(y == 0)
{
printf("Over X axis\n");
printf("Quadrant 1\n");
printf("Quadrant 2\n");
printf("Quadrant 3\n");
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:
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
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;
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;
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;
scanf("%d",&acc);
scanf(" %c",&building);
scanf("%f",&vol);
switch(building)
case 'h':
if(vol<=20)
x=5+((vol-20)*0.57);
else if(vol>40)
x=5+(20*0.57)+((vol-40)*0.91);
break;
case 'i':
if(vol<=35)
y=30+((vol-35)*1.8);
else if(vol>70)
y=30+(35*1.8)+((vol-70)*1.92);
break;
case 'c':
if(vol<=35)
z=20+((vol-35)*2);
else if(vol>70)
z=20+(35*2)+((vol-70)*2.87);
break;
default:
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:
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.
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: