In: Computer Science
C PROGRAMMING
Identify and correct the errors in each of the following. [Note: There may be more than one
error in each piece of code.]
a)
if ( sales => 5000 )
puts( "Sales are greater than or equal to $5000" )
else
puts( "Sales are less than $5000 )
b)
int x = 1, product = 0;
while ( x <= 10 ); {
product *= x;
++x;
}
c)
While ( x <= 100 )
total =+ x;
++x;
d)
while ( y < 10 ) {
printf( "%d\n", y );
}
// Note: Below are the corrected statement with incorrection marked after // for eg ; is missing
Ans a)
if ( sales >= 5000 ) // correct way is
>=
puts( "Sales are greater than or
equal to $5000" ); // semicolon missing
else
puts( "Sales are less than $5000
"); // " and ; missing
---------
Ans b)
Note: In this code there is one syntax error in while loop there is also one logic error product should be equal to 1 inorder to do product 1 to 10
int x = 1, product = 1;
while ( x <= 10 ) { // no need of semicolon
product *= x;
++x;
}
---------------
Ans c)
while ( x <= 100 ){ // curly braces and W should be
small
total =+ x;
++x;
}
-------------
Ans d)
while ( y < 10 ) {
printf( "%d\n", y );
y++; // y should be
incremented
}