EXERCISE: 5 The Decision Control Structure

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: 12803 200;
Rectified: The program has an error. Correct is given bellow:
#include
#include
void main( )
{
int a = 300, b, c ;
clrscr();
if ( a >= 200 )
b = 300 ;
c = 200 ;
printf ( "\n%d %d", b, c ) ;
getch();
}
Output: 300 200

(c) 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



(d) main( )
{
int i = 4, z = 12 ;
if ( i = 5 || z > 50 )
printf ( "\nDean of students affairs" ) ;
else
printf ( "\nDosa" ) ;
}
Rectified: The program has an error. Correct is given bellow:

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

(e) 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 (f) main( ) { int i = -1, j = 1, k ,l ; k = !i && j ; l = !i || j ; printf ( "%d %d", i, j ) ; } Rectified: The program has an error. Correct is given bellow: #include
#include
void main( )
{
int i = -1, j = 1, k= 7, l= 8 ;
clrscr();
k = !i && j ;
l = !i || j ;
printf ( "%d %d", i, j ) ;
getch();
}
Output: -1 , 1

(g) main( )
{
int i = -4, j, num ;
j = ( num < 0 ? 0 : num * num ) ; printf ( "\n%d", j ) ; } Rectified: The program has an error. Correct is given bellow: #include
#include
void main( )
{
int j, num=4 ;
clrscr();
j = ( num < 0 ? 0 : num * num ) ; printf ( "\n%d", j ) ; getch(); } Output: 16 Attempt the following: (a) Any integer is input through the keyboard. Write a program to find out whether it is an odd number or even number. Ans: #include
#include
void main( )
{
int j;
clrscr();
printf("enter no=");
scanf("%d", &j);
if(j%2==0)
{
printf("the no %d is even",j );
}
else
{
printf("the no %d is odd",j);
}
getch();
}
Input: 9
Output: the no 9 is odd
Input: 10
Output: the no 10 is even
(b) Write a program to find the greatest of the three numbers entered through the keyboard using conditional operators.
Ans: #include
#include
void main()
{
int a,b,c;
clrscr();
printf("enter three unequal no=\n") ;
scanf("%d %d %d", &a,&b,&c);
if((a>b)&&(b>c))
{
printf("a=%d is greatest\n",a);
}
else if (b>c)
{
printf("b=%d is greatest\n",b);
}
else
{
printf("c=%d is greatest\n",c);
}
getch();
}
Input: enter three unequal no=
-44
0
-87
Output: b=0 is greatest

Followers