0% found this document useful (0 votes)
15 views4 pages

CSE 103 Lab-2

Uploaded by

amivalo45rt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

CSE 103 Lab-2

Uploaded by

amivalo45rt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

East West University

Department of Computer Science & Engineering


Plot No-A/2, Main Road, Jahurul Islam City, Aftabnagar, Dhaka-1219

Lab Manual
Course : CSE 103
Credit Title : Structured Programming
Instructor : Dr. Maheen Islam, Associate Professor, CSE Department

Lab-2: Inputs, Output and Arithmetical expressions

Aims: By the end of the workshop you should be able to understand: -

• Detail of C’s scanf and printf functions


• How to take input from keyboard through scanf ?
• How to evaluate an arithmetical expression with C?

In the previous lab we go through the printf in detain. So, we can start with scanf. As
discussed in the class scanf is the C library’s counterpart to printf. Actually, scanf requires a
format string to specify the appearance of the input data. Example of using scanf to read an int
value:

scanf("%d", &i);

The above code will read an integer and stores into variable i. The & symbol is usually (but
not always) required when using scanf. Now write the following code:

#include <stdio.h>
int main()
{
int i;
printf("Enter The Number i\n");
scanf("%d",&i);
printf("Your Entered Integer is = %d",i);
return 0;
}

1. Compile and execute the above code.


2. When you execute the code you will certainly see a screen like this.

Page 1 of 4
3. If not then there is obviously something wrong in your code, in that case knock your
teacher.
4. If there is nothing wrong enter 10 form your keyboard and press enter.
5. Your output should be as follows, otherwise knock your teacher.

6. Now execute the code again and take 10.50 form your keyboard.
7. The output should be the same as in step 5. In output .50 is missing, this is because we
take the variable i as an integer. If we want to print the exact 10.50, we should declare
variable i as float or double.
8. Reading a float value requires a slightly different call of scanf:
scanf("%f", &i);
"%f" tells scanf to look for an input value in float format i.e. the number may contain a
decimal point, but doesn’t always have to.
9. If you declare i as double your scanf should be as follows:
scanf("%lf", &i);
"%lf" tells scanf to look for an input value in double format.
10. For the printf you have to use %d or %f or %lf respectively. Look at table 1 for more
detail. Don’t worry about the char and string we will discuss more detail in the
upcoming labs.

printf scanf
int %d %d
float %f %f
double %f %lf
char %c %c
string %s %s

11. If you want to enter more than one value i.e., serialized the inputs you can do it by the
following code.

float height, weight;


… … … … … … … … … … … … … … … … … …

scanf(“%f%f”, &height, &weight);

Exercise 1: In this initial exercise you are asked to calculate the volume of a room. As
you know volume = width × height × length. In the C this formula will be
volume = height * length * width;
That is operation * in C is same as × in mathematics. For more operators in C look at
following table

Page 2 of 4
Your job is to declare width, height and length as double. Write the code to take these as input
from keyboard. Print the volume.

Sample input Sample Output

Enter height of box: 10 Volume (cubic inches): 1000


Enter length of box: 10
Enter width of box: 10

Enter height of box: 10 Volume (cubic inches): 15478.750000


Enter length of box: 30.50
Enter width of box: 50.75

Exercise 2: The following formula is used to calculate the Celsius equivalent of a Fahrenheit
temperature:
C = (5/9) *(F – 32)
Where F is the temperature in Fahrenheit and C is the Celsius equivalent of that temperature.
Write a program in C to convert the temperature in Fahrenheit to the equivalent Celsius
temperature.

Sample input Sample Output

Enter Fahrenheit Temperature: 20 Temperature in Fahrenheit: 20


Temperature in Celsius: -6

Exercise 3: Write a program to calculate the area and the perimeter of a circle using the
formulas area = r2 and perimeter = 2r, where  is a constant whose value is 3.1415926
and r is the radius of the circle. Use float type variable for r.

Sample input Sample Output

Enter the radius: 4.0 Area = 50.265482


Perimeter = 25.132741

Exercise 4: Compute the straight line distance between two points in a plane. i.e. your job is
to take the points as input from keyboard and print the outputs. The coordinates of points
should be declared as float and for this you should know how to use sqrt() with
#include<math.h> as discussed in the class.

Sample input Sample Output

Enter Point X1: 1.0 The distance is 3.605551


Enter Point X2: 4.0

Page 3 of 4
Enter Point Y1: 5.0
Enter Point Y2: 7.0
Enter Point X1: 11.0 The distance is 4.242640
Enter Point X2: 14.0
Enter Point Y1: 5.0
Enter Point Y2: 8.0

Home Work:
Problem 1: A computer manufacturing company has the following monthly compensation
policy to their sales-persons:
Minimum base salary : 1500.00
Bonus for every computer sold : 200.00
Commission on the total monthly sales : 2 percent
Given the base salary, bonus and commission rate, the inputs necessary to calculate the gross
salary are, the price of each computer and the number sold during the month.
Calculate the gross salary of a sales-person.

Sample input Sample Output

Enter the number of computers: 5 Bonus = 1000.00


Price of each computer: 40000 Commission = 4000.00

Problem 2: Write a program to convert the distance in miles and yards to kilometer. To
convert miles to kilometers, multiply by the conversion factor 1.609. [1 mile = 1760 yards]

Sample input Sample Output

Enter the distance in miles and yards The distance in kilometer is: 42.185970 km
Miles: 26
Yards: 385

Problem 3: Write a program to evaluate the following function for different values of x.
[Hints: use math.h functions, pow(), sin(), sqrt() ]
f(x) = 3x5 – 5x -6sin(x)

Sample input Sample Output

Enter x : 5 f(x) = 9369.5732

Page 4 of 4

You might also like