Lab 3 Intro C
Lab 3 Intro C
LAB 3
Introduction to C Programming
ANSWER
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];
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
d. Read three integers from the keyboard and store them in the variables x, y and
z.
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.
2.3 Using the statements you wrote in Task 2.2, write a complete program that
calculates the product of three integers.
#include <stdio.h>
int main ()
{
int iX, iY, iZ, iResult; //declare variables
//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
End
4
EKT 120 – Computer Programming Laboratory Module
#include <stdio.h>
int main()
{
float fR1,fR2;
const float fPi = 3.14;
float fSmallArea, fBigArea, fMarkedArea;
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
Calculate Equivalent
Series Resistance
Calculate Equivalent
Parallel Resistance
End
#include <stdio.h>
int main ()
{
float fR1,fR2,fR3;
float fSeriesR, fParallelR;
fSeriesR = fR1+fR2+fR3;
fParallelR = 1/(1/fR1 + 1/fR2 + 1/fR3);
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
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
End
7
EKT 120 – Computer Programming Laboratory Module
#include <stdio.h>
int main(void)
{
int iValue, iDigit1, iDigit2, iDigit3, iDigit4, iDigit5;
//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
End
/***************************** *****************************
* 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
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*/
Relevant Formulas
Celcius = 10 (depth) + 20 /*Celsius temperature at depth in km*/
a. Write down the pseudo code and flowchart for the program
Start
End
10
EKT 120 – Computer Programming Laboratory Module
/***************************** *****************************
* 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 */
11