You are on page 1of 7

Universiti Malaysia Perlis

LAB 2: PROBLEM SOLVING TECHNIQUES, ALGORITHM:


PSEUDO CODE AND FLOWCHART

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

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);

 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;

// 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);
area = 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.

Program to calculate the area of triangle

Please enter the value of base: 4

Please enter the value of height: 5

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.

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.

can read more than 1 input in single sentence/command/instruction.

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.

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

Read temperature Perform conversion from C to F using


value in Celsius: C formula: F = (9.0/5.0 x C) + 32

Print output or temperature value in


Perform conversion from C to F Fahrenheit, F
using formula:F= (9.0/5.0 * C) + 32 End

Print temperature in
Fahrenheit: F

End

b. Write down your program:

#include <stdio.h>

int main ()
{
// variables declaration
float fCelsius,fFahrenheit;

// to display messages to user


printf("\nProgram to convert from Celsius to Fahrenheit");
printf(“\n\nPlease enter the value of the temperature in Celsius: ”);
scanf(“%f”, &fCelsius);

fFahrenheit=(9.0/5.0 * fCelsius) + 32;

printf(“\nThe value of temperature in Fahrenheit = %5.2f\n”,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:

please refer Task 1a..

d. Write down your program:

please refer Task 1b..


formula: celsius=(fahrenheit-32)*5.0/9.0;

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

Read input (radius in


inches):R

Calculate area using R

Calculate perimeter

Perform unit conversion from


inches to cm

Print area and


perimeter in cm

End
b. Write down your program.
#include <stdio.h>

int main ()
{
// variables declaration
const double PI = 3.141594;
float fR, fArea, fPerimeter;

// to display messages to user


printf("\nProgram to calculate area and perimeter of a circle");
printf("\n\nPlease enter the value of the radius in inches: ");
scanf("%f", &fR);

//1 inches = 2.54 cm


fArea=PI*(fR*2.54)*(fR*2.54);
fPerimeter=2*PI*fR*2.54;

printf("\nThe area of the circle in cm = %5.2f cm",fArea);


printf("\nThe perimeter of the circle in cm = %5.2f cm\n",fPerimeter);
return 0;
} // end of main

You might also like