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

Lab 3 Intro C

The document provides instructions for a laboratory module on introduction to C programming. It includes 6 tasks - the first task outlines the objectives of learning basic C syntax and functions like printf and scanf. The remaining tasks involve writing C code statements and programs to declare variables, calculate products and areas, and separate digits of a number. Pseudocode and flowcharts are provided for some tasks to help students write the programs.

Uploaded by

ariff mohd
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 3 Intro C

The document provides instructions for a laboratory module on introduction to C programming. It includes 6 tasks - the first task outlines the objectives of learning basic C syntax and functions like printf and scanf. The remaining tasks involve writing C code statements and programs to declare variables, calculate products and areas, and separate digits of a number. Pseudocode and flowcharts are provided for some tasks to help students write the programs.

Uploaded by

ariff mohd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

EKT 120 – Computer Programming Laboratory Module

LAB 3
Introduction to C Programming

ANSWER

School of Computer and Communication Engineering


Universiti Malaysia Perlis

1
EKT 120 – Computer Programming Laboratory Module

1. OBJECTIVES:
1.1 To be able to apply basic rules and structures of C in writing a simple program.
1.2 To be able to use printf and scanf functions to display output and read input in a
program.
1.3 To become familiar with fundamental data types.
1.4 To be able to name, declare, assign and print values of variables used in
program.

2. TASKS:

2.1 What data types would you use to hold the following data and write C statements to
declare them all.

a. customer initial
Data type: char
Declaration: char cCustomerInitial;

b. customer name
Data type: array of characters or strings
Declaration: char acName[20];

c. your house number


Data type: integer
Declaration: int iHouseNo;

d. price
Data type: float or double
Declaration: float fPrice; or double dPrice;

e. car registration
Data type: array of characters or strings
Declaration: char acCarReg[8];

f. time
Data type: float
Declaration: float fTime;

g. 6 digit number
Data type: long integer
Declaration: long int lNumber;

2
EKT 120 – Computer Programming Laboratory Module

2.2 Write a statement (or comment) to accomplish each of the following:

a. State that a program will calculate the product of three integers.

/* Program to calculate the product of three integers */


or
// Program to calculate the product of three integers

b. Define the variables x, y, z and result to be type int.

int iX, iY, iZ, iResult;

c. Prompt the user to enter three integers.

printf (“Enter three integers: ”);

d. Read three integers from the keyboard and store them in the variables x, y and
z.

scanf (“%d %d %d”, &iX, &iY, &iZ);

e. Compute the product of the three integers contained in variables x, y and z and
assign the answer to the variable result.

iResult = iX * iY * iZ;

f. Print “The product is” followed by the value of the integer variable result.

printf (“The product is %d\n”, iResult);

2.3 Using the statements you wrote in Task 2.2, write a complete program that
calculates the product of three integers.

/* Program to calculate the product of three integers */

#include <stdio.h>

int main ()
{
int iX, iY, iZ, iResult; //declare variables

printf (“Enter three integers: ”); //prompt


scanf (“%d %d %d”, &iX, &iY, &iZ); //read three integers
iResult = iX * iY * iZ; //multiply values

//display result
printf (“The product is %d\n”, iResult);

return 0;
}

3
EKT 120 – Computer Programming Laboratory Module

2.4 Write a program that calculates marked area of the circle, given input from the user
r1 and r2.

r2

r1

a. Write down the pseudo code and flowchart for the program:

Start
Read radius for big circle and small circle: r2 and r1
Calculate area for big circle
Calculate area for small circle
Calculate area for marked circle: area of big circle – area of small circle
Print area of marked circle
End

Start

Read input: r1 and r2

Calculate area for big


circle
s_20=s_50%20

Calculate area for small


circle

Calculate marked area

Print marked area

End

4
EKT 120 – Computer Programming Laboratory Module

b. Write your program based on pseudo code and flowchart in (a):

#include <stdio.h>
int main()
{
float fR1,fR2;
const float fPi = 3.14;
float fSmallArea, fBigArea, fMarkedArea;

printf("Enter r2 and r1 : ");


scanf("%f %f", &fR2, &fR1);
small_area = fPi * fR1 * fR1;
big_area = fPi * fR2 * fR2;
fMarkedArea = fBigArea - fSmallArea;
printf("Marked area of circle = %5.2f", fMarkedArea);
return 0;
}

2.5 Write a program to find the equivalent series and parallel resistance for 3 resistor
values. Your program should scan the 3 resistor values and then compute the
equivalent series and parallel resistance for all 3 resistors. For example, if the 3
resistor values are r1=100, r2=200 and r3=300 ohms, respectively, their equivalent
series resistance is r1 + r2 + r3 = 100 + 200 + 300 = 600 ohms and their equivalent
parallel resistance = 1 / [1/r1 + 1/r2 + 1/r3] = 1/[0.01+0.005+0.0033] = 54.55 ohms.

a. Write down the pseudo code and flowchart for the program

Start
Read resistance values: R1, R2 and R3
Calculate Equivalent Series Resistance
Calculate Equivalent Parallel Resistance
Print Equivalent Series Resistance
Print Equivalent Parallel Resistance
End

5
EKT 120 – Computer Programming Laboratory Module

Start

Read input: R1, R2 and R3

Calculate Equivalent
Series Resistance

Calculate Equivalent
Parallel Resistance

Print Series Resistance

Print Parallel Resistance

End

b. Write your program based on pseudo code and flowchart in (a):

#include <stdio.h>

int main ()
{
float fR1,fR2,fR3;
float fSeriesR, fParallelR;

printf ("\nEnter resistors values: ");


scanf("%f %f %f",&fR1,&fR2,&fR3);

fSeriesR = fR1+fR2+fR3;
fParallelR = 1/(1/fR1 + 1/fR2 + 1/fR3);

printf ("\nSeries resistance = %5.2fohms\n",fSeriesR);


printf ("Parallel resistance = %5.2fohms\n",fParallelR);

return 0;
}

6
EKT 120 – Computer Programming Laboratory Module

2.6 Write a program that inputs one five-digit number, separates the number into its
individual digits and prints separated from one another by three spaces each.[Hint :
Use combinations of integer division and the remainder operation.] For example, if
the user types in 42139, the program should print:

Sample Output:
Enter value : 42139

Answer :
4 2 1 3 9

a. Write down the pseudo code and flowchart for the program

Start

Read one five digit number, Value

Digit1 = Value/10000
Start Value = Value%10000
Read one five digit number
Divide the Value by 10000 and store (Digit 1).
Take the remainder and divide by 1000 and store
(Digit 2).
Digit2 = Value/1000
Take the remainder and divide by 100 and store
(Digit 3).
Value = Value%1000
Take the remainder and divide by 10 and store
(Digit 4).
Divide by 10 and store the remainder (Digit 5) Digit3 = Value/100
Print out Digit 1, Digit 2, Digit 3, Digit 4 & Digit 5. Value = Value%100
End

Digit4 = Value/10

Digit5 = Value%10

Print : Digit1, Digit2,


Digit3, Digit 4 & Digit5

End

7
EKT 120 – Computer Programming Laboratory Module

b. Write your program based on pseudo code and flowchart in (a):

#include <stdio.h>
int main(void)
{
int iValue, iDigit1, iDigit2, iDigit3, iDigit4, iDigit5;

printf("Enter a 5-digit value : ");


scanf("%d", &iValue); //read value

//calculation to separate digits


iDigit1 = iValue / 10000;
iValue = iValue % 10000;
iDigit2 = iValue / 1000;
iValue = iValue % 1000;
iDigit3 = iValue / 100;
iValue = iValue % 100;
iDigit4 = iValue / 10;
iDigit5 = iValue % 10;

//print result
printf("Answer:\n%d %d %d %d %d\n", iDigit1, iDigit2,
iDigit3, iDigit4, iDigit5);
return 0;
}

Additional Exercises:

2.7 Write a program to calculate the distance travel using the given formula below:
S = vt + ½ at2
In your program ask user to input through keyboard the value of:
v = initial velocity
a = acceleration
t = time of travel

c. Write down the pseudo code and flowchart for the program

Start
Read input: velocity, acceleration & travel time
Calculate the distance travel
Display the distance travel
End

8
EKT 120 – Computer Programming Laboratory Module

Start

Read input: Velocity, acceleration


& travel time

Calculate the distance


Travel

Print the distance Travel

End

d. Write your program based on pseudo code and flowchart in (a):

/***************************** *****************************
* Lab 3 Intro c
* program to calculate the distance travel
*
*/

#include <stdio.h>
int main ( )
{
/* Velocity in km/h, acceleration in km/h*h, time in
second,s */
float fVelocity, fAcceleration, fTravelTime,
fTravelDistanceS

/* Get velocity, acceleration and travel time from user */


printf (“Initial Velocity, Acceleration and the time travel
: “);
scanf (“%f %f %f”, &fVelocity, &fAcceleration,
&fTravelTime);

/* Calculate and display the distance travel */


fTravelDistanceS = fVelocity * fTravelTime + 0.5 *
fAcceleration * fTravelTime * fTravelTime;

printf(“The distance you have traveled is : %”,


fTravelDistanceS);
return 0;
}

9
EKT 120 – Computer Programming Laboratory Module

2.8 Write a program to take a depth (in kilometers) inside the earth as input data;
compute and print the temperature at this depth in degrees Celsius and degrees
Fahrenheit.

Data Requirements

Problem Input
double dDepth /*depth in km*/

Problem Outputs
double cCelcius /*temperature in degrees Cescius*/

double dFahr /*temperature in degrees Fahrenheit*/

Relevant Formulas
Celcius = 10 (depth) + 20 /*Celsius temperature at depth in km*/

Fahrenheit = 1.8 (Celsius) + 32;

a. Write down the pseudo code and flowchart for the program

Start

Read input: depth

Start Calculate the temperature in


Read input: depth Celsius
Calculate the temperature in Celsius
Calculate the temperature in Fahrenheit
Display the depth, Celsius & Fahrenheit Calculate the temperature in
End Fahrenheit

Print the depth, Celsius &


Fahrenheit

End

10
EKT 120 – Computer Programming Laboratory Module

b. Write your program based on pseudo code and flowchart in (a):

/***************************** *****************************
* Lab 3 Intro c
* Determine the temperature (in Celsius and Fahrenheit) at a
given depth (km) inside
* the earth
*/

#include <stdio.h>
int main()
{
double dDepth; /* depth in km */
double dCelsius; /* temperature in degrees Celsius */
double dFahr; /* temperature in degrees Fahrenheit */

/* Get depth from user */


printf("Enter depth (in km) inside earth: ");
scanf("%lf", &dDepth);

/* Calculate and display the temperature at this depth */


dCelsius = 10 * dDepth + 20;
dFahr = 1.8 * dCelsius + 32;
printf("At a depth of %.1f km, the temperature is %.1f
degrees Celsius\n", dDepth, dCelsius);
printf("(%.1f degrees Fahrenheit).\n", dFahr);
return 0;
}

11

You might also like