Problem Set for Decision Control Structure

What will be the output of the following?

(a) main( ) {

int a = 300, b, c ;

if ( a >= 400 )

b = 300 ;

c = 200 ;

printf ( "\n%d %d", b, c ) ;

}

(b) main( )

{

int a = 500, b, c ;

if ( a >= 400 )

c = 200 ;

cout< "\n" b "\n " c ;

}

(c) main( )

{

int x = 10, y = 20 ;

if ( x == y ) ;

cout<x<”\t”<y ;

}

(d) main( )

{

int x = 3, y = 5 ;

if ( x == 3 )

cout<x ;

else ;

cout< y ;

}

(e) main( )

{

int x = 3 ;

float y = 3.0 ;

if ( x == y )

cout<"\nx and y are equal" ;

else

cout< "\nx and y are not equal" ;

}

(f) main( )

{

int x = 3, y, z ;

y = x = 10 ;

z = x < 10 ;

cout< "\nx =”<x< ” y = “<y<”z =" z ;

}

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

(a) main( ) {

float a = 12.25, b = 12.52 ;

if ( a = b )

cout<"\na and b are equal" ;

}

(b) main( )

{

int j = 10, k = 12 ;

if ( k >= j )

{

{

k = j ;

j = k ;

}

}

}

(c) main( )

{

if ( 'X' < 'x' )

cout< "\nascii value of X is smaller than that of x" ;

}

(d) main( )

{

int x = 10 ;

if ( x >= 2 ) then

cout< x ;

}

(e) main( )

{

int x = 10 ;

if x >= 2

cout< x ;

}

(f) main( )

{

int x = 10, y = 15 ;

if ( x % 2 = y % 3 )

cout< "\nCarpathians" ;

}

(g) main( )

{

int x = 30 , y = 40 ;

if ( x == y )

cout<"x is equal to y";

elseif ( x > y )

cout< "x is greater than y" ;

elseif ( x < y )

cout<"x is less than y" ;

}

(h) main( )

{

int x = 10 ;

if ( x >= 2 ) then

cout<x ;

}

(i) main( )

{

int a, b ;

cin>a> b ;

if ( a > b ) ;

cout<"This is a game" ;

else

cout< "You have to play it" ;

}

Write programs for the following statements:

a)If cost price and selling price of an item is input through the keyboard, write a program to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred.

b)Any integer is input through the keyboard. Write a program to find out whether it is an odd number or even number.

c)Any year is input through the keyboard. Write a program to determine whether the year is a leap year or not.

d)According to the Gregorian calendar, it was Monday on the date 01/01/1900. If any year is input through the keyboard write a program to find out what is the day on 1st January of this year.

e)A five-digit number is entered through the keyboard. Write a program to obtain the reversed number and to determine whether the original and reversed numbers are equal or not.

f)If the ages of Ali, Omer and Asghar are input through the keyboard, write a program to determine the youngest of the three.

g)Write a program to check whether a triangle is valid or not, when the three angles of the triangle are entered through the keyboard. A triangle is valid if the sum of all the three angles is equal to 180 degrees.

h)Find the absolute value of a number entered through the keyboard.

i)Given the length and breadth of a rectangle, write a program to find whether the area of the rectangle is greater than its perimeter. For example, the area of the rectangle with length = 5 and breadth = 4 is greater than its perimeter.

j)Given three points (x1, y1), (x2, y2) and (x3, y3), write a program to check if all the three points fall on one straight line.