Lab 2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINNERING

EEE3123
COMPUTER PROGRAMMING
GROUP 1
LAB 2: USER INPUT

NAME : MD AZIZUL HOQUE


MATRIC NO : 200191
LECTURER : ASSOC. PROF. DR. WAN ZUHA BIN WAN HASAN
SUBMSSION DATE : 01.12 .2020
Objective: To create a program in C language that allows numerical user input.
To create a program in C language that calculates a value from user input.

Equipment: Personal Computer with C Program Pre-Installed

Introduction:

Arithmetic operators, in C, are operators used to perform arithmetic operations that


include multiplication, division, addition and subtraction. With the exception of the
subtraction operator, where "-" is used to indicate a negative number, arithmetic
operators are binary operators that take two operands. The operands are of numeric
data type, and perform in a similar manner as in other languages such as C and C++.

Arithmetic operators "+" and "-" are used to manipulate pointers by adding or
subtracting the numeric value to or from the pointers without generating any exception
during overflow of the of the pointer's domain. Arithmetic operators can be overloaded
when used with user-defined types to extend the nature of normal operators, thus
providing additional functionalities.

Methodology:

1. Write a program that will prompt the user to input the height, length and width of
a box in centimeters. From the user input the program must calculate and
display box’s volume.

2. Write a program that simulates an odometer. The user inputs the speed (in
kmph) and time the vehicle has traveled (in seconds!!). From the user input,
the program must calculate and display the distance traveled. The program
must also display the speed and time entered by the user.

3. Given by a user are the x- and y- coordinates of two points in a plain: (x 1, y1) and
(x2, y2). Write a C program that will compute and print the distance between
these two points using given below.

2 2
Distance = √ ( x −x ) +( y − y )
1 2 1 2
QUESTION 1

PSEUDOCODE:

1.START

2. Enter the value of height, width and length

3. Calculate the volume

4. End

CODING:

#include<stdio.h>

#include<stdlib.h>

int main ()

float Height,Length, Width, TotalVolume;

printf("calculate a box volume");

printf("\n make sure the input value is in centimeters");

printf("\ninput the box height");

scanf("%f",&Height);

printf("input the box length");

scanf("%f",&Length);

printf("input the box width");

scanf("%f",&Width);
TotalVolume=Height*Width*Length;

printf("the total volume of the box is %f", TotalVolume);

return 0;

Listing 1.1: Program code for the arithmetic operation.

Listing 1.2: Total volume of the box.


QUESTION 2:

PSEDUCODE:

1. START
2. ENTER THE SPEED, TIME
3. CALCULATE THE DISTANCE
4. END

CODING

#include <stdio.h>
#include <stdlib.h>
int main ()
{
float speed, time,T,distance;

printf("Input speed in km/h\n");


scanf("%f",&speed);
printf("Input time in seconds\n");
scanf("%f",&time);
T=time/3600;

distance=speed/T;

printf("your distance in km is %f",distance);


return 0;
}

Listing 2.1: Program code for the arithmetic operation.

Listing 2.2: calculating the distance using the arithmetic operation.


QUESTION 3:

PSEUDOCODE

START

ENTER THE VALUE OF x1, y1, x2 AND y2

CALCULATE THE DISTANCE

a = pow((x1-x2),2);

b = pow((y1-y2),2);

dis = pow((a+b),0.5);

PRINT DISTANCE

END

CODING:

#include <stdio.h>

#include <math.h>

int main()

int x1,y1,x2,y2;

float a,b,dis;

printf("Input X1: ");

scanf("%d",&x1);

printf("Input Y1: ");


scanf("%d",&y1);

printf("Input X2: ");

scanf("%d",&x2);

printf("Input Y2: ");

scanf("%d",&y2);

a = pow((x1-x2),2);

b = pow((y1-y2),2);

dis = pow((a+b),0.5);

printf("Distance: %f",dis);

return 0;

}
Listing 3.1: Program code to calculate distance between the x and y coordinates using
the arithmetic operation in C.

Listing 3.2: The distance between x and coordinates.

DISCUSSION:

In the question 1, I input the value of the height, length and width in the program using
the function of Scanf and used the function of the Printf to print the result. According to
the instruction to calculate the volume which is height*width*length were declared in the
program later, I used the function of Printf to print or display the output. In the listing 1.1,
full coding of the program to calculate volume is given and once it executed, desired
output achieved in the listing 1.2. In the question 2, to calculate the distance, the float
value of time and speed entered in the program using the scanf function. As, the
instruction asked the speed were given kmph and time were given in the program in
seconds. After establishing the full program, I was able to calculate distance which is
stated in the Listing 2.1 and Listing 2.2. In the question 3, to calculate the distance
between two coordinate x and y at first declared the function respectively a = pow((x1-
x2),2), b = pow((y1-y2),2) and dis = pow((a+b),0.5 to make it easy to solve the
arithmetic function of c program. Scanf function allowed to input value of x1, x2, y1 and
y2. Finally, I used the printf function to print out the distance between two coordinates
which is given in the listing 3.1 and 3.2. In this lab, I was able to perform the arithmetic
operation using the scanf and printf function.

CONCLUSION:

In conclusion, arithmetic operations enable us to operate numerical user input


and make calculations using the input given by the user, rendering many fruitful future
possibilities with various benefits such as efficient yet convenient calculation process.
REFERENCE:

1.Lab manual 2, EEE3123- Computer Programming.

You might also like