CSE 103 Lab-2
CSE 103 Lab-2
Lab Manual
Course : CSE 103
Credit Title : Structured Programming
Instructor : Dr. Maheen Islam, Associate Professor, CSE Department
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;
}
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.
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.
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.
Exercise 3: Write a program to calculate the area and the perimeter of a circle using the
formulas area = r2 and perimeter = 2r, where is a constant whose value is 3.1415926
and r is the radius of the circle. Use float type variable for r.
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.
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.
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]
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 – 5x -6sin(x)
Page 4 of 4