Professional Documents
Culture Documents
ANSWER
Objectives
1. To be able to write simple computer programs in C based on pseudo code and
flowchart.
2. To be able to apply the “printf” and “scanf” functions to write simple input and
output statements.
3. To be able to use arithmetic operators and understand the arithmetic operators
precedence.
Introduction
The text that you want to display must be within the double quote “the text”.
You can send parameter to the printf function. The printf function enables us to
display the dynamic value after we have done the data processing, for example after
calculating mathematical problem and we want to display the answer to the screen.
The command we use is as below. For example, to calculate area of triangle using
the equation: area = ½*(base * height) :
area is the variable that contains the answer value, and it is passed to the printf
function.
the symbol of % must be used to tell the printf function where to print the answer
value.
The format “f” is used if we define the variable area as “float” data type. Other format
types are:
%d – for integer data type
%c – for character data type
%s – for string data type
%f – for floating points data type
and many more.
In the C language, scanf is used to accept the user input from the keyboard. The
command of scanf is as below:
scanf (“%f”,&fBase);
%f is the format of data type that will be entered. For example, if the variable “base”
is defined as float the format “f” is used.
The “%f” must be in the double quote “ ”.
The symbol “&” must be used with scanf command. This is to tell the compiler the
address of variable “base”, thus the keyed in data will be located to the address of
“base” in the computer memory.
Part A
Q1.This program will calculate the area of triangle and display the answer. User needs
to enter values of base and height of the triangle. Type the program and complete
the questions:
#include <stdio.h>
int main ()
{
//variables declaration
float fArea, fBase, fHeight;
return 0;
} //end of main
Height = 5.000000
Base = 4.000000
0.5 x Height x Base = Area
0.5 x 5.000000 x 4.000000 = 10.000000
Q2. Below is a segment of the above program.
b. Compile and run the new amended program. Write down your observation and
comment.
b. Compile and run the new amended program. Write down your observation and
comment.
i. \t – horizontal tab
ii. %5.2f – floating point format
iii. formula or calculation can be inserted/done in printf statement.
Q4. Learning to use the escape sequence. Below are several escape sequences. Use
the escape sequence and printf format given below to format your output to look
presentable.
- \n
- \a
- \t
- %
And print few lines to make the output looks nicer. To print lines, insert the below
command in your program:
printf (“----------------------------------“);
Part B
Task 1. Write a program to convert the Celsius value input by the user to the
Fahrenheit value. Use the formula below to calculate the conversion:
F = (9.0/5.0 x C) + 32
a. Write the pseudo code and draw the flowchart of the solution:
Start Begin
Get input or temperature value in Celsius: C
Print temperature in
Fahrenheit: F
End
#include <stdio.h>
int main ()
{
// variables declaration
float fCelsius,fFahrenheit;
return 0;
} // end of main
Write a program to convert the Fahrenheit value entered by the user to the
Celsius value. Modify the above formula to find the value C.
c. Write the pseudo code and draw the flowchart of the solution:
Task 2. Write a program to calculate area and perimeter of a circle. Declare PI as const
double PI = 3.141594. The program should ask the user to enter the radius of
the circle in inches. The program should then display the radius, area and
perimeter of a circle in centimeters. Discuss the formula with your colleague.
a. Write the pseudo code and draw the flowchart of the solution:
Start
Calculate perimeter
End
b. Write down your program.
#include <stdio.h>
int main ()
{
// variables declaration
const double PI = 3.141594;
float fR, fArea, fPerimeter;