Sunteți pe pagina 1din 7

Universiti Malaysia Perlis

LAB 2: PROBLEM SOLVING TECHNIQUES, ALGORITHM:


PSEUDO CODE AND FLOWCHART

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
In C language, printf command is used to display any message or output to the
screen. The format of printf is:
printf(The text to be displayed);

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) :
printf(The area of triangle is = %f, fArea);

fArea 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;
// to display messages to user
printf("\nProgram to calculate the area of triangle");
printf(\n\nPlease enter the value of base: );
scanf(%f, &fBase);
printf(\nPlease enter the value of height: );
scanf(%f, &fHeight);
fArea = 0.5 * fBase * fHeight;
//formula to calculate area is written here
printf(\n\nHeight = %f, fHeight);
printf(\nBase = %f, fBase);
printf(\n0.5 x Height x Base = Area);
printf(\n0.5 x %f x %f = %f\n, fHeight, fBase, fArea); //display answer for area
return 0;
} //end of main

a.

Write down the output of the program.

Q2. Below is a segment of the above program.


printf(\nPlease enter the value of base: );
scanf(%f, &fBase);
printf(\nPlease enter the value of height: );
scanf(%f, &fHeight);
a. Change the segment to the command below:
printf(\nPlease enter values of base and height: );
scanf(%f %f, &fBase, &fHeight);
b. Compile and run the new amended program. Write down your observation and
comment.
________________________________________________________________
_______________________________________________________________

Q3. Below is a segment of the above initial program.


fArea = 0.5 * fBase * fHeight;
printf(\n\nHeight = %f, fHeight);
printf(\nBase = %f, fBase);
printf(\n0.5 x Height x Base = Area);
printf(\n0.5 x %f x %f = %f, fHeight, fBase, fArea);
a. Change the segment to the command below:
printf(\n\n\tHeight = %f, fHeight);
printf(\n\tBase = %f, fBase);
printf(\n\t0.5 x Height x Base = Area);
printf(\n0.5 x %5.2f x %5.2f = %5.2f, fHeight, fBase, 0.5*fHeight*fBase);
b. Compile and run the new amended program. Write down your observation and
comment.
_______________________________________________________________
________________________________________________________________
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:

b.

Write down your program:

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:

d.

Write down your program:

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:

b.

Write down your program.

S-ar putea să vă placă și