Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

ENG3202 Computer Programming 2022-2023

LAB 1: INTRODUCTION TO C PROGRAMMING

Listing 1.1
/* This program is to convert the temperature in Celsius to its corresponding
Fahrenheit value. Happy trying! */

#include stdio.h

int main
{
/* local declarations */
float Celsius,
int 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;
}

PSEUDOCODE

Start the program.


Let value of Celsius be a float number and Fahrenheit an integer number.
Put the Celsius value.
State the Celsius to Fahrenheit conversion formula.
Value in Fahrenheit will be calculated.
Value in float number will be print.
End program

1
ERROR

2
3
AFTER DEBUG

OUTPUT

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;}

PSEUDOCODE

Start the program.


Let the number be integer.
Write some character.
Put the number.
Print the character and the number.
End program.

ERROR

5
AFTER DEBUG

OUTPUT

6
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
}

ERROR

7
DEBUG

OUTPUT

8
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*/

ERROR

9
DEBUG

OUTPUT

10
ANALYSIS/DISCUSSION

Listing 1.1

LINE OLD NEW REASON


3 stdio.h <stdio.h> As header contains function
prototypes for the standard
input/output library functions,
and information used by them.
4 int main int main (void) After the main indicate that main is
a program building block called a
function.
7 float Celsius, float Celsius; The semicolon indicates the current
statement has been terminated and
other statements following are new
statements.
10 Celcius Celsius The spelling is wrong.
10 (“Enter (“Enter
temperature in temperature in
Celcius: “); Celcius: );
16 fahrenhiet Fahrenheit The spelling is wrong.
16 %f %d The value for Fahrenheit was
initially declare as integer.

Listing 1.2

LINE OLD NEW REASON


1 /* this is an /* this is an Without ‘*/’, the entire program is
example program example program detected as comment.
*/
2 main int main (void) The main function.

Listing 1.3

LINE OLD NEW REASON


1 /* this program /* this program Without ‘*/’, the entire program is
has error(s) has error(s)*/ detected as comment.
5 printf “hello printf (“hello Without ‘(’ the character in the
THERE !!”) THERE !!”) double quote will not be print.
6 printf(‘we are printf(“we are Double quote indicates what
to find’]; to find”); character print.
7 printf (‘the printf (“the Double quote indicates what
error’); error”); character print.

11
LISTING 1.4

LINE OLD NEW REASON


2 (stdio.h) <stdio.h> As header contains function
prototypes for the standard
input/output library functions,
and information used by them.
3 void main (void) int main (void) To indicate integer.

12

You might also like