Print Friendly and PDF
(Correct the Code Errors) Identify and correct the error(s) in each of the following:
(Correct the Code Errors) Identify and correct the error(s) in each of the following:
a)

if ( age >= 65 ) ;
cout << "Age is greater than or equal to 65" << endl;
else
cout << "Age is less than 65 << endl";
b)
if ( age >= 65 )
cout << "Age is greater than or equal to 65" << endl;
else;
cout << "Age is less than 65 << endl";
c)
int x = 1, total;
while ( x <= 10 )
{
total += x;
++x;
}d
)
While ( x <= 100 )
total += x;
++x;
e)
while ( y > 0 )
{
cout << y << endl;
++y;
}


Solution:

a)
if ( age >= 65 ) // remove semicolon
cout << "Age is greater than or equal to 65" << endl;
else
cout << "Age is less than 65" << endl; // fix quotation mark error
b)
if ( age >= 65 )
cout << "Age is greater than or equal to 65" << endl;
else // remove semicolon
cout << "Age is less than 65" << endl; // fix quotation mark error
c)
int x = 1, total = 0; //initialize variable total to avoid garbage value in it
while ( x <= 10 )
{
total += x;
++x;
}


d)
while ( x <= 100 ) // fix uppercase error
{
total += x;
++x; // include x increment to while body with brackets
}e
)
while ( y > 0 )
{
cout << y << endl;
++y; // it is infinite loop, one of possible solutions is decrement ' y'
}
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: