To Use Arithmetic Operators and Understand The Arithmetic Operators Precedence

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

LAB 2

INTRODUCTION TO C PROGRAMMING AND


PROBLEM SOLVING TECHNIQUES

Aim:
1. Learning the Open Source IDE Platform: CodeBlock Environment
2. Writing simple C Program based on Pseudo Code and Flow chart
3. To apply the “printf” and “scanf” functions to write simple input and output statements.
4. To use arithmetic operators and understand the arithmetic operators precedence

INTRODUCTION TO CODE BLOCK

1. Open Code Block application as shown in Figure 1.

Figure. 1: Code Block Environment

2. Click at new file and choose empty file


LAB 2

Figure. 3: Code Block Editor

3. Type “Hello World” program source code in the code block editor.

Figure. 4: Program "Hello World"

4. To compile the C code press F9 button or go to Build → build.


5. Observe the lower window – this is where the information whether your code
is successfully compiled.
6. If there were no errors display, to run and execute your program press F10 button
or go to Run → run
7. The execution of your program will look like Figure 5

Figure. 5: Execution of program "Hello World"


LAB 2

INTRODUCTION TO C

1. 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”);
2. The text that you want to display must be within the double quote “the text”.
3. 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 sum of two numbers
using the equation, sum=num1 + num2
printf(“The sum is %f”, sum);
 sum 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.
4. In the C language, scanf is used to accept the user input from the keyboard.
The command of scanf is as below:
scanf (“%f”,&base);
5. %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.

6. The “%f” must be in the double quote “ ”.


7. 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.
LAB 2

LAB EXERCISES

PART I: SHORT ANSWERED QUESTIONS

1. Write the output of the following program.

#include<stdio.h>

int main(void)
{
printf("Introduction to Computer Programming\n");
printf("Total credit: \t 3 \n");
printf("Consist of 5 topics which are \nIntroduction \nSelection\n");
printf("Function \nRepetition \nArray \nPointers\n");

return 0;
}

Figure 1
OUPUT:

Introduction to Computer Programming


Total credit: 3
Consist of 5 topics which are
Introduction
Selection
Repetition
Array
Pointers
LAB 2

2. The following question are based on C code in Figure 2. Answer all questions.

#include<stdio.h>

int main()
{
//declaration of variables
int sum=10;
char letter='Z';
float set1=10;
double num1=10;
double num2=10e+10;

printf("Integer variable is %d\n\n",sum);


printf("Character is %c\n\n",letter);

printf("Float variable is %f\n",set1);

printf("Double variable for floating number is %lf\n",num1);


printf("Double variable for exponential is %e\n",num2);

return 0;

Figure 2

i. Write the output of the program.

OUTPUT:
Integer Variable is 10

Character is Z
Float variable is 10.000000
Double variable for floating number is 10.000000
Double variable for exponential is 1.000000e+011

PROOF:
ii. Using the same C code in Figure 2, set the decimal place for float variable,
set1 to 3 decimal points, and double variable, num1 to 9 decimal points.

Algorithm
#include<stdio.h>

int main()
{
//declaration of variables
int sum=10;
char letter='Z';
float set1=10;
double num1=10;
double num2=10e+10;

printf("Integer variable is %d\n\n",sum);


printf("Character is %c\n\n",letter);

printf("Float variable is %.3f\n",set1);


printf("Double variable for floating number is %.9f\n",num1);
printf("Double variable for exponential is %e\n",num2);
return 0;

OUTPUT:
Integer Variable is 10

Character is Z
Float variable is 10.000
Double variable for floating number is 10.000000000
Double variable for exponential is 1.000000e+011

PROOF:
LAB 2

3. C code in Figure 3 determines the average of 2 numbers input by users and display
the result at the end.

#include<stdio.h>

int main()
{
float number1,number2;
float average=0;

printf("Enter 2 numbers\n");
scanf("%f %f",&number1,&number2);

average=(number1+number2)/2;

printf("Average of %f and %f is %f\n",number1, number2, average);

return 0;

Figure 3

(i) Write the output of the program.

OUTPUT:
Enter 2 numbers

13

Average of 1.000000 and 3.000000 is 2.000000

PROOF:
(ii) Rewrite the program in Figure 3 to accept input of 3 numbers and display the
average of those 3 numbers.

Output:
Enter 3 numbers

123

Average of 1.000000, 2.000000 and 3.000000 is 2.000000

ALGORITHM:

#include<stdio.h>

int main()
{
float number1, number2, number3;
float average=0;

printf("Enter 3 numbers\n");
scanf("%f %f %f",&number1,&number2,&number3);

average=(number1+number2+number3)/3;

printf("Average of %f, %f and %f is %f\n",number1, number2,number3, average);

return 0;
}

PROOF:
LAB 2

PART II: PROBLEM SOLVING QUESTIONS

1. Write a C program to read input of 2 floating numbers. The program must perform
addition, subtraction, multiplication and division against entered numbers. Display
each result of each mathematical operation. Your program output should be as
follows:

Enter 2 numbers:
45 23

45.00 + 23.00 = 68.00


45.00 – 23.00 = 22.00
45.00 x 23.00 = 1035.00
45.00 / 23.00 = 1.96

Your solution must include an algorithm design using flow chart.

ALGORITHM:

#include<stdio.h>

int main()

{
float number1,number2;
float sum=0;
float difference=0;
float product= 0;
float quotient= 0;

printf("Enter 2 numbers\n");
scanf("%f %f",&number1,&number2);

printf("\n");

sum =(number1+number2);
printf("%.2f and %.2f is %.2f\n",number1, number2, sum);

difference = (number1-number2);
printf("%.2f and %.2f is %.2f\n",number1, number2, difference);

product = (number1*number2);
printf("%.2f and %.2f is %.2f\n",number1, number2, product);

quotient = (number1/number2);
printf("%.2f and %.2f is %.2f\n",number1, number2, quotient);

return 0;

}
FLOW CHART: START

Enter Number 1 and


Enter Number 2

Calculate:
SUM = Number 1 + Number 2
DIFFERENCE =Number 1 – Number 2
PRODUCT = Number 1 * Number 2
QUOTIENT = Number 1 / Number 2

Print/Display
Num1 + Num 2 = Sum
Num1 – Num 2 = Difference
Num1 * Num 2 = Product
Num1 / Num 2 = Quotient

END

PROOF:
LAB 2

2. Design an algorithm using flowchart for a program that measure area of triangle.
Then, write a complete C program to implement the algorithm. The program must
read the value of base and height of the triangle entered by users, measure the area of
the triangle and display the result at the end. The formula for triangle area given as
follows:
1
area = × base × height

FLOW CHART:

START

Enter the base and


height of the triangle

Calculate the area of the Triangle


AREA = ½ * base * height

Display the AREA

END

ALGORITHM:

#include<stdio.h>

int main()
{
printf("Area of the Triangle\n");
printf("\n");
float base,height;
float area=0;

printf("Enter the Base of the Triangle: ");


scanf("%f",&base);

printf("\n");

printf("Enter the Height of the Triangle: ");


scanf("%f",&height);

printf("\n");

area =(base*height/2);
printf("The Area of a Triangle : 1/2 x %.2f B x %.2f H = %.2f\n",base, height,area);

return 0;

}
PROOF:
LAB 2

3. Write a program to convert the temperature from degrees in Celsius to degrees


in Fahrenheit. The conversion formula given as follows:
F = (9.0/5.0 x C) + 32

where C is the temperature in Celsius and F the temperature in Fahrenheit.

(i) Design the algorithm, i.e write the pseudo code and draw the flowchart of
the solution.
(ii) Implement the algorithm designed in (i) in your program. Test and verify
the completed program and write down your completed program.

ALGORITHM:

#include <stdio.h>

int main()
{
float celsius, fahrenheit;

printf("Enter temperature in Celsius: ");


scanf("%f", &celsius);
printf("\n");

fahrenheit = (celsius * 9 / 5) + 32;


printf("The Conversion is: \n\n%.2f Celsius = %.2f Fahrenheit\n", celsius, fahrenheit);

return 0;
}

PSUEDOCODE
1. BEGIN.
2. Input Celsius, which represents C.
3. Convert Celsius to Fahrenheit: CALCULATE F = (9.0/5.0 x C) + 32.
4. Display the result of the Conversion.
5. END.
FLOWCHART:
BEGIN

Input Celsius,
VALUE IS C

Convert Celsius to Fahrenheit: CALCULATE


F = (9.0/5.0 x C) + 32

DISPLAY the Conversion

END

PROOF:

You might also like