0% found this document useful (0 votes)
13 views11 pages

Chapter 2 Outputs and Errors

The document contains a series of C programming exercises focusing on decision control structures, including output predictions and error identification in various code snippets. It illustrates the correct use of conditional statements, logical operators, and the syntax required for proper execution. Additionally, it provides examples of rewriting programs using conditional operators.

Uploaded by

azankhan4744
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

Chapter 2 Outputs and Errors

The document contains a series of C programming exercises focusing on decision control structures, including output predictions and error identification in various code snippets. It illustrates the correct use of conditional statements, logical operators, and the syntax required for proper execution. Additionally, it provides examples of rewriting programs using conditional operators.

Uploaded by

azankhan4744
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Chapter 2

The Decision Control


Structure
[A] What would be the output of the following programs:

(a) main( )
{
int a = 300, b, c ;
if ( a >= 400 )
b = 300 ;
c = 200 ;
printf ( "\n%d %d", b, c ) ;
}

Output:0 200

(b) main( )
{
int a = 500, b, c ;
if ( a >= 400 )
b = 300 ;
c = 200 ;
printf ( "\n%d %d", b, c ) ;
}

Output:300 200

(c) main( )
{
int x = 10, y = 20 ;
if ( x == y ) ;
printf ( "\n%d %d", x, y ) ;
}
Output:10 20

(d) main( )
{
int x = 3, y = 5 ;
if ( x == 3 )
printf ( "\n%d", x ) ;
else ;
printf ( "\n%d", y ) ;
}
Output:3
5

(e) main( )
{
int x = 3 ;
float y = 3.0 ;
if ( x == y )
printf ( "\nx and y are equal" ) ;
else
printf ( "\nx and y are not equal" ) ;
}
Output:x and y are equal

(f) main( )
{
int x = 3, y, z ;
y = x = 10 ;
z = x < 10 ;
printf ( "\nx = %d y = %d z = %d", x, y, z ) ;
}
Output:x =10 y =10 z =0

g) main( )
{
int k = 35 ;
printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ;
}
Output:0 50 0

(h) main( )
{
int i = 65 ;
char j = 'A' ;
if ( i == j )
printf ( "C is WOW" ) ;
else
printf( "C is a headache" ) ;
}
Output:C is WOW
(i) main( )
{
int a = 5, b, c ;
b = a = 15 ;
c = a < 15 ;
printf ( "\na = %d b = %d c = %d", a, b, c ) ;
}
Output:a = 15 b = 15 c =0

(j) main( )
{
int x = 15 ;
printf ( "\n%d %d %d", x != 15, x = 20, x < 30 ) ;
}
Output:0 20 1

[B] Point out the errors, if any, in the following programs:

(a) main( )
{
float a = 12.25, b = 12.52 ;
if ( a = b )
printf ( "\na and b are equal" ) ;
}
Error:We need to use ==(equality operator) instead of using = inside of if() statement

(b) main( )
{
int j = 10, k = 12 ;
if ( k >= j )
{
{
k=j;
j=k;
}
}
}
Error:There are extra {} inside if() statement due to which it will not give any output
(c) main( )
{
if ( 'X' < 'x' )
printf ( "\ascii value of X is smaller than that of x" ) ;
}
Error:No Error
(d) main( )
{
int x = 10 ;
if ( x >= 2 ) then
printf ( "\n%d", x ) ;
}
Error:value of x will not be printed because then is written after if() statement
(e) main( )
{
int x = 10 ;
if x >= 2
printf ( "\n%d", x ) ;
}
Error:There are no () after if statement
(f) main( )
{
int x = 10, y = 15 ;
if ( x % 2 = y % 3 )
printf ( "\nCarpathians" ) ;
}
Error:We need to use ==(equality operator) instead of using = inside of if() statement
(g) main( )
{
int x = 30 , y = 40 ;
if ( x == y )
printf( "x is equal to y" ) ;
elseif ( x > y )
printf( "x is greater than y" ) ;
elseif ( x < y )
printf( "x is less than y" ) ;
}
Error:No Error

(h) main( )
{
int x = 10 ;
if ( x >= 2 ) then
printf ( "\n%d", x ) ;
}
Error:value of x will not be printed because then is written after if() statement

(i) main( )
{
int a, b ;
scanf ( "%d %d",a, b ) ;
if ( a > b ) ;
printf ( "This is a game" ) ;
else
printf ( "You have to play it" ) ;
}
Error:After if(a>b) there is “;” making it unable to execute any function after it as attended
[D] What would be the output of the following programs:

(a) main( )
{
int i = 4, z = 12 ;
if ( i = 5 || z > 50 )
printf ( "\nDean of students affairs" ) ;
else
printf ( "\nDosa" ) ;
}
Output:Dean of students affairs

(b) main( )
{
int i = 4, z = 12 ;
if ( i = 5 && z > 5 )
printf ( "\nLet us C" ) ;
else
printf ( "\nWish C was free !" ) ;
}
Output:Let us C

(c) main( )
{
int i = 4, j = -1, k = 0, w, x, y, z ;
w = i || j || k ;
x = i && j && k ;
y = i || j && k ;
z = i && j || k ;
printf ( "\nw = %d x = %d y = %d z = %d", w, x, y, z ) ;
}
Output:w = 1 x = 0 y = 1 z =1

(d) main( )
{
int i = 4, j = -1, k = 0, y, z ;
y = i + 5 && j + 1 || k + 2 ;
z = i + 5 || j + 1 && k + 2 ;
printf ( "\ny = %d z = %d", y, z ) ;
}
Output:y =1 z =1

(e) main( )
{
int i = -3, j = 3 ;
if ( !i + !j * 1 )
printf ( "\nMassaro" ) ;
else
printf ( "\nBennarivo" ) ;
}
Output:Bennarivo
(f) main( )
{
int a = 40 ;
if ( a > 40 && a < 45 )
printf ( "a is greater than 40 and less than 45" ) ;
else
printf ( "%d", a ) ;
}
Output:40
(g) main( )
{
int p = 8, q = 20 ;
if ( p == 5 && q > 5 )
printf ( "\nWhy not C" ) ;
else
printf ( "\nDefinitely C !" ) ;
}
Output:Definitely C !
(h) main( )
{
int i = -1, j = 1, k ,l ;
k = i && j ;
l = i || j ;
printf ( "%d %d", I, j ) ;
}
Output:1 1
(i) main( )
{
int x = 20 , y = 40 , z = 45 ;
if ( x > y && x > z )
printf( "x is big" ) ;
else if ( y > x && y > z )
printf( "y is big" ) ;
else if ( z > x && z > y )
printf( "z is big" ) ;
}
Output:z is big
(j) main( )
{
int i = -1, j = 1, k ,l ;
k = !i && j ;
l = !i || j ;
printf ( "%d %d", k, l ) ;
}
Output:0 1
(k) main( )
{
int j = 4, k ;
k = !5 && j ;
printf ( "\nk = %d", k ) ;
}
Output:0

[E] Point out the errors, if any, in the following programs:

(a)/* This program


/* is an example of
/* using Logical operators */
main( )
{
int i = 2, j = 5 ;
if ( i == 2 && j == 5 )
printf ( "\nSatisfied at last" ) ;
}
Error:Incorrect comment

(b) main( )
{
int code, flag ;
if ( code == 1 & flag == 0 )
printf ( "\nThe eagle has landed" ) ;
}
Error:Incorrect because of single &(correct is &&)
(c) main( )
{
char spy = 'a', password = 'z' ;
if ( spy == 'a' or password == 'z' )
printf ( "\nAll the birds are safe in the nest" ) ;
}
Error:Incorrect because of or(correct is ||)
(d) main( )
{
int i = 10, j = 20 ;
if ( i = 5 ) && if ( j = 10 )
printf ( "\nHave a nice day" ) ;
}
Error:We need to use ==(equality operator) instead of using = inside of if() statement

(e) main( )
{
int x = 10 , y = 20;
if ( x >= 2 and y <=50 )
printf ( "\n%d", x ) ;
}
Error:Incorrect because of and(correct is &&)

(f) main( )
{
int a, b ;
if ( a == 1 & b == 0 )
printf ( "\nGod is Great" ) ;
}
Error:Incorrect because of single &(correct is &&)

(g) main( )
{
int x = 2;
if ( x == 2 && x != 0 ) ;
{
printf ( "\nHi" ) ;
printf( "\nHello" ) ;
}
else
printf( "Bye" ) ;
}
Error:No Error
(h) main( )
{
int i = 10, j = 10 ;
if ( i && j == 10)
printf ( "\nHave a nice day" ) ;
}
Error:The variable i has no comparison value in the if statement
[G] What would be the output of the following programs:
(a) main( )
{
int i = -4, j, num ;
j = ( num < 0 ? 0 : num * num ) ;
printf ( "\n%d", j ) ;
}
Output:num not initialized

(b) main( )
{
int k, num = 30 ;
k = ( num > 5 ? ( num <= 10 ? 100 : 200 ) : 500 ) ;
printf ( "\n%d", num ) ;
}
Output:200

(c) main( )
{
int j = 4 ;
( !j != 1 ? printf ( "\nWelcome") : printf ( "\nGood Bye") ) ;
}
Output:Welcome

[I] Rewrite the following programs using conditional operators.


(a) main( )
{
int x, min, max ;
scanf ( "\n%d %d", &max, &x ) ;
if ( x > max )
max = x ;
else
min = x ;
}
Using Conditional Operators:

main()
{
int x, min, max ;
scanf ( "\n%d %d", &max, &x );
x>max?(max=x):(min=x);
}

(b) main( )
{
int code ;
scanf ( "%d", &code ) ;
if ( code > 1 )
printf ( "\nJerusalem" ) ;
else
if ( code < 1 )
printf ( "\nEddie" ) ;
else
printf ( "\nC Brain" ) ;
}
Using Conditional Operators:
main()
{
int code ;
scanf ( "%d", &code ) ;
code>1?printf("\nJerusalem"):(code<1?printf("\nEddie"):printf("\nC Brain"));
}

(c) main( )
{
float sal ;
printf ("Enter the salary" ) ;
scanf ( "%f", &sal ) ;
if ( sal < 40000 && sal > 25000 )
printf ( "Manager" ) ;
else
if ( sal < 25000 && sal > 15000 )
printf ( "Accountant" ) ;
else
printf ( "Clerk" ) ;
}
Using Conditional Operators:
main()
{
float sal ;
printf ("Enter the salary:" ) ;
scanf ( "%f", &sal ) ;
sal<40000&&sal>25000?printf("Manager"):(sal<25000&&sal>15000?
printf("Accountant"):printf("Clerk"));
}

You might also like