Chapter - 3: The Decision Control

Point out the errors, if any, in the following programs:


B
Sections
1
Exercises

A

B

C

D

E

F

G

H

I

J

K

1

(a)

#include<stdio.h>
int main( )
{
	float a = 12.25, b = 12.52 ;
	if ( a = b )
	printf ( "\na and b are equal" ) ; 
	return 0;
}

No Error


(b)

#include<stdio.h>
int main( )
{
	int j = 10, k = 12 ;
	if ( k >= j )
	{
		{
			k = j ;
			j = k ;
		}
	} 
	return 0;
}

No Error


(c)

#include<stdio.h>
int main( )
{
	if ( 'X' < 'x' )
		printf ( "\nascii value of X is smaller than that of x" ) ; 
	return 0;
}

No Error


(d)

#include<stdio.h>
int main( )
{
	int x = 10 ;
	if ( x >= 2 ) then
		printf ( "\n%d", x ) ; 
	return 0;
}

Error: then is not any defined variable or keyword.


(e)

#include<stdio.h>
int main( )
{
	int x = 10, y = 15 ;
	if ( x % 2 = y % 3 ) 
		printf ( "\nCarpathians" ) ;
	return 0;
}

Error: lvalue required. In the if condition, there is constant value before =


(f)

#include<stdio.h>
int 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" ) ;
	return 0;
}

Error: There should be "else if" at the place of "elseif"


(g)

#include<stdio.h>
int main( )
{
	int a, b ;
	scanf ( "%d %d",a, b ) ;
	if ( a > b ) ;
		printf ( "This is a game" ) ;
	else
		printf ( "You have to play it" ) ;
	return 0;
}

Error: &a and &b should be used instead of a and b in scanf();


© 2020 Garbage Valuegarbage value logo