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

1. Identify and correct the errors in each of the following statements.

There may be more


than one error per statement. Write a short explanation on how you corrected each.

a) The statement scanf("&d", %value); is wrong. The errors are the placement of ‘&’
and ‘%’. The ‘&’ and ‘%’ need to swap positions, and the correct statement is
supposed to be scanf(“%d”, &value); .

b) The statement a + b + c = sum; is wrong. The error here is the incorrect inputting
of the equation. The left-hand side of the operator must be the variable ‘sum’,
while the right-hand side should be ‘a + b + c’. Hence the correct statement is
sum = a + b + c; .

c) The statement print("The product is &d\n," x * y ); is wrong. The keyword should


be ‘printf’ instead of just ‘print’. Moreover, the comma should be inputted after the
apostrophe. Hence the correct statement is printf("The product is %d\n", x * y); .

d) The statement scanf("%d", int); is wrong. Not only is the ‘&’ missing from the
variable, the variable used is ‘int’. The variable ‘int’ cannot be used since it is a
keyword with a special meaning in the C language. A correct version of this
statement should be scanf("%d", &sum); .

e) The statement scanf("%d, %d, %d", &x &y &z ); is wrong. The error here is that
there are no commas in between variables x, y, and z. Therefore, the correct
statement would be scanf("%d, %d, %d", &x, &y, &z ); .

f) The statement printf("%c\n", 'Hello'); is wrong. Since the word ‘Hello’ is a string,
use ‘%s’ instead of ‘%c’ since ‘%c’ is only used to print single characters.
Additionally, remove the single apostrophes from ‘Hello’ because here we
assume that ‘Hello’ is a declared character. The correct statement should be
printf("%s\n", hello); .

g) The correct statement is /* The following statement should print "Bon Voyage": */
printf(""%s"", "Bon Voyage"); . This is because the first sentence is a comment
that should not be read as a statement for the program. Hence the need to
enclose it within /* */ .

h) The statement printf(%f, 123.456); is wrong. The error here is that the ‘%f’ is not
enclosed within double apostrophes. Therefore, the correct statement is
printf(“%f”, 123.456); .

2. Write a printf or scanf statement for each of the following:

a) Read a hexadecimal value into variable hex:


scanf("%x", &hex);

b) Print 1.234 in a 9-digit field with preceding zeros:

printf("%09.3f", 1.234);

c) Read a time of the form hh:mm:ss, storing the parts of the time in the integer
variables hour, minute and second. Skip the colons (:) in the input stream. Use
the assignment suppression character.

scanf("%d%*c%d%*c%d", &hour, &minute, &second);

3. Consider the following program in which the statements are in the incorrect order.
Rearrange the statements so that the program prompts the user to input the height and
the radius of the base of a cylinder and outputs the volume and surface area of the
cylinder.

#include<stdio.h>
#include<math.h>
#define PI 3.14159

int main ()
{
double radius;
double height;
puts("Enter the radius of the base of the cylinder:");
scanf("%lf", &radius);
puts("Enter the height of the cylinder:");
scanf("%lf", &height);
printf("Volume of the cylinder = %.3lf\n", PI*pow(radius, 2.0)*height);
printf("Surface area: %.3lf\n", 2*PI*radius*height + 2*PI*pow(radius, 2.0));
return 0;
}

You might also like