To Use Arithmetic Operators and Understand The Arithmetic Operators Precedence
To Use Arithmetic Operators and Understand The Arithmetic Operators Precedence
To Use Arithmetic Operators and Understand The Arithmetic Operators Precedence
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
3. Type “Hello World” program source code in the code block editor.
INTRODUCTION TO C
LAB EXERCISES
#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:
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;
return 0;
Figure 2
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;
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;
return 0;
Figure 3
OUTPUT:
Enter 2 numbers
13
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
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;
return 0;
}
PROOF:
LAB 2
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
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
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
END
ALGORITHM:
#include<stdio.h>
int main()
{
printf("Area of the Triangle\n");
printf("\n");
float base,height;
float area=0;
printf("\n");
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
(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;
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
END
PROOF: