Chapter 7: Case-Control Instruction – Let Us C Solutions

In the seventh chapter, Let Us C covered all the basic things we need to get started in the journey of learning C Programming. Now, let’s have a look at the solutions of the exercise of the seventh chapter, Case-Control Instruction from Let Us C.

[A] What will be the output of the following programs:
(a)

# include <stdio.h>
int main( )
{
char suite = 3 ;
switch ( suite )
{
case 1 :
printf ( "Diamond\n" ) ;
case 2 :
printf ( "Spade\n" ) ;
default :
printf ( "Heart\n" ) ;
}
printf ( "I thought one wears a suite\n" ) ;
return 0 ;
}

Output:
Heart
I thought one wears a suite

(b)

# include <stdio.h>
int main( )
{
int c = 3 ;
switch ( c )
{
case '3' :
printf ( "You never win the silver prize\n" ) ;
break ;
case 3 :
printf ( "You always lose the gold prize\n" ) ;
break ;
default :
printf ( "Of course provided you win a prize\n" ) ;
}
return 0 ;
}

Output:
You always lose the gold prize

(c)

# include <stdio.h>
int main( )
{
int i = 3 ;
switch ( i )
{
case 0 :
printf ( "Customers are dicey\n" ) ;
case 1 + 0 :
printf ( "Markets are pricey\n" ) ;
case 4 / 2 :
printf ( "Investors are moody\n" ) ;
case 8 % 5 :
printf ( "At least employees are good\n" ) ;
}
return 0 ;
}

Output:
At least employees are good
(d)

# include <stdio.h>
int main( )
{
int k ;
float j = 2.0 ;
switch ( k = j + 1 )
{
case 3 :
printf ( "Trapped\n" ) ;
break ;
default :
printf ( "Caught!\n" ) ;
}
return 0 ;
}

Output:
Trapped
(e)

# include <stdio.h>
int main( )
{
int ch = 'a' + 'b' ;
switch ( ch )
{
case 'a' :
case 'b' :
printf ( "You entered b\n" ) ;
case 'A' :
printf ( "a as in ashar\n" ) ;
case 'b' + 'a' :
printf ( "You entered a and b\n" ) ;
return 0 ;
}

Output:
You entered a and b

(f)

# include <stdio.h>
int main( )
{
int i = 1 ;
switch ( i - 2 )
{
case -1 :
printf ( "Feeding fish\n" ) ;
case 0 :
printf ( "Weeding grass\n" ) ;
case 1 :
printf ( "Mending roof\n" ) ;
default :
printf ( "Just to survive\n" ) ;
}
return 0 ;
}

Output:
Feeding fish
Weeding grass
mending roof
Just to survive

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

# include <stdio.h>
int main( )
{
int suite = 1 ;
switch ( suite ) ;
{
case 0 ;
printf ( "Club\n" ) ;
case 1 ;
printf ( "Diamond\n" ) ;
}
return 0 ;
}

Error. Semi-colon after switch statement and after case 0 and
case 1.

(b)

# include <stdio.h>
int main( )
{
int temp ;
scanf ( "%d", &temp ) ;
switch ( temp )
{
case ( temp <= 20 ) :
printf ( "Ooooooohhhh! Damn cool! \n" ) ;
case ( temp > 20 && temp <= 30 ) :
printf ( "Rain rain here again! \n" ) ;
case ( temp > 30 && temp <= 40 ) :
printf ( "Wish I am on Everest\n" ) ;
default :
printf ( "Good old nagpur weather\n" ) ;
}
return 0 ;
}

Error. Relational operators cannot be used in cases.

(c)

# include <stdio.h>
int main( )
{
float a = 3.5 ;
switch ( a )
{
case 0.5 :
printf ( "The art of C\ " ) ;
break ;
case 1.5 :
printf ( "The spirit of C\n" ) ;
break ;
case 2.5 :
printf ( "See through C\n" ) ;
break ;
case 3.5 :
printf ( "Simply c\n" ) ;
}
return 0 ;
}

Error. Floats cannot be used in cases.

(d)

# include <stdio.h>
int main( )
{
int a = 3, b = 4, c ;
c = b – a ;
switch ( c )
{
case 1 || 2 :
printf ( "God give me a chance to change things\n" ) ;
break ;
case a || b :
printf ( "God give me chance to run my show\n" ) ;
break ;
}
return 0 ;
}

Error. A case needs a constant expression. Logical operators
cannot be used in cases.
[C] Write a menu driven program which has following options:

  1. Factorial of a number
  2. Prime or not
  3. Odd or even
  4. Exit
    Once a menu item is selected the appropriate action should be
    taken and once this action is finished, the menu should
    reappear. Unless the user selects the ‘Exit’ option the program
    should continue to work.
    Hint: Make use of an infinite while and a switch statement.
    Program:
# include <stdio.h>
int main( )
{
int choice, num, i ;
unsigned long int fact ;
while ( 1 )
{
printf ( "\n\n1. Factorial\n" ) ;
printf ( "2. Prime\n" ) ;
printf ( "3. Odd / Even\n" ) ;
printf ( "4. Exit\n" ) ;
printf ( "\nYour choice ? " ) ;
scanf ( "%d", &choice ) ;
switch ( choice )
{
case 1 :
printf ( "\nEnter number: " ) ;
scanf ( "%d", &num ) ;
fact = 1 ;
for ( i = 1 ; i <= num ; i++ )
fact = fact * i ;
printf ( "Factorial value = %lu\n", fact ) ;
break ;
case 2 :
printf ( "\nEnter number: " ) ;
scanf ( "%d", &num ) ;
for ( i = 2 ; i < num ; i++ )
{
if ( num % i == 0 )
{
printf ( "Not a prime number\n" ) ;
break ;
}
}
if ( i == num )
printf ( "\nPrime number" ) ;
break ;
case 3 :
printf ( "\nEnter number: " ) ;
scanf ( "%d", &num ) ;
if ( num % 2 == 0 )
printf ( "Even number\n" ) ;
else
printf ( "Odd number\n" ) ;
break ;
case 4 :
exit ( 0 ) ; /* Terminates program execution */
}
}
return 0 ;
}

[D] Write a program which to find the grace marks for a student
using switch. The user should enter the class obtained by the
student and the number of subjects he has failed in. Use the
following logic:

  1. If the student gets first class and the number of subjects he failed in is greater than 3, then he does not get any grace. Otherwise the grace is of 5 marks per subject.
  2. If the student gets second class and the number of subjects he failed in is greater than 2, then he does not get any grace. Otherwise the grace is of 4 marks per subject.
  3. If the student gets third class and the number of subjects he failed in is greater than 1, then he does not get any grace. Otherwise the grace is of 5 marks.

Program:

# include <stdio.h>
int main( )
{
int c, sub ;
printf ( "\nEnter the class and number of subjects failed: " ) ;
scanf ( "%d %d", &c, &sub ) ;
switch ( c )
{
case 1 :
if ( sub <= 3 )
printf ( "Student gets total of %d grace marks\n",
5 * sub ) ;
else
printf ( "No grace marks\n" ) ;
break ;
case 2 :
if ( sub <= 2 )
printf ( "Student gets total of %d grace marks\n",
4 * sub ) ;
else
printf ( "No grace marks\n" ) ;
break ;
case 3 :
if ( sub == 1 )
printf ( "Student gets 5 grace marks\n" ) ;
else
printf ( "No grace marks\n" ) ;
break ;
default :
printf ( "Wrong class entered\n" ) ;
}
return 0 ;
}

These were the solutions of Chapter 7: Case-Control Instruction of Let Us C Book by Yashvant Kanetkar

Leave a Reply

Your email address will not be published.