lab 3: introduction to c programming

advertisement
DKT 121 – Basic Computer Programming
Laboratory Module
LAB 3
INTRODUCTION TO C PROGRAMMING
School of Computer and Communication Engineering
Universiti Malaysia Perlis
1
DKT 121 – Basic 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:
Declaration:
b. customer name
Data type:
Declaration:
c. your house number
Data type:
Declaration:
d. price
Data type:
Declaration:
e. car registration
Data type:
Declaration:
f.
time
Data type:
Declaration:
g. 6 digit number
Data type:
Declaration:
2
DKT 121 – Basic 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.
b)
Define the variables x, y, z and result to be type int.
c)
Prompt the user to enter three integers.
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.
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.
3
DKT 121 – Basic 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) Design the algorithm, i.e write the pseudo code and draw the flowchart of the
solution :
b) Implement the algorithm designed in (a) in your program. Test and verify the
completed program and write down your completed program.
4
DKT 121 – Basic Computer Programming
Laboratory Module
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.
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
5
Download