Tutorial 2
Question 1: Program the problem of reading two fractions entered by users, then add two
fractions, and print the result to the screen.
Question 2: What output do the followings calls of printf produce?
• printf(“%6d, %4d”, 86, 1040);
• printf(“%12.5e”, 30.253);
• printf(“%.4f”. 83.162);
• printf(“%-6.2g”, 0.000009979);
Question 3: Suppose that we call scanf as follows:
scanf(“%d%f%d”, &i, &x, &j);
If the user enters
10.3 5 6
What will be the values of i, x, and j after the call? (Assume that i and j are int variables and x is
a float variable).
Question 4: Write a program that accepts a date from the user in the form mm/dd/yyyy and
then displays it in the form yyyyddmm:
Enter a date (mm/dd/yyyy): 2/17/2011
You entered the date 20110217
Question 5: Show the output produced by each of the following program fragments. Assume
that i, j, and k are int variables.
a) i=5; j=3;
printf(“%d %d”, i/j, i%j);
b) i=7; j=8;k=9;
printf("%d”, (i+10) % k/j);
Question 6: If i and j are positive integer, does (-i)/j always have the same value as (-i/j)? Justify
your answer.
Question 7: Is the following if statement legal?
if(n>=1 <=10)
printf (“n is between 1 and 10\n”);
If so, what does it do when n is equal to 0?
Question 8: What output does the following program fragment produce? (Assume the i is an
integer variable.)
i=1;
switch (i %3)
{
case 0: printf(“Zero”);
case 1: printf(“one”);
case 2: printf(“two”);
}
Question 9: Write a program to convert the Vietnamese grades to German grades.
Question 10: Write a program that sums a series of integers entered by users.
Question 11: Write a program that approximates e by computing the value of
1+1/1!+1/2!+….+1/n!,
where n is an integer entered by the user.