Chapter 4: More Complex Decision Making – Let Us C Solutions

In the fourth 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 first chapter, Getting Started from Let Us C.

Table of Contents:

  • If a = 10, b = 12, c = 0, find the values of the expressions in the following table
  • What will be the output of the following programs
  • Point out the errors, if any, in the following programs
  • Attempt the following

[A] If a = 10, b = 12, c = 0, find the values of the expressions in the following table

ExpressionValue
a != 6 && b > 5
a == 9 || b < 3
! ( a < 10 )
! ( a > 5 && c )
5 && c != 8 || !c
1
0
1
1
1

[B] What will be the output of the following programs

(a)

# include <stdio.h>
int main( )
{
int i = 4, z = 12 ;
if ( i = 5 || z > 50 )
printf ( "Dean of students affairs\n" ) ;
else
printf ( "Dosa\n" ) ;
return 0 ;
}

Answer: Dean of students affairs

(b)

# include <stdio.h>
int 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 ( "w = %d x = %d y = %d z = %d\n", w, x, y, z ) ;
return 0 ;
}

Answer: w = 1 x = 0 y = 1 z = 1

(c)

# include <stdio.h>
int main( )
{
int x = 20, y = 40, z = 45 ;
if ( x > y && x > z )
printf ( "biggest = %d\n", x ) ;
else if ( y > x && y > z )
printf ( "biggest = %d\n", y ) ;
else if ( z > x && z > y )
printf ( "biggest = %d\n", z ) ;
return 0 ;
}

Answer: biggest = 45

(d)

# include <stdio.h>
int main( )
{
int i = -1, j = 1, k,l ;
k = !i && j ;
l = !i || j ;
printf ( "%d %d\n", i, j ) ;
return 0 ;
}

Answer: -1 1

(e)

# include <stdio.h>
int main( )
{
int i = -4, j, num ;
j = ( num < 0 ? 0 : num * num ) ;
printf ( "%d\n", j ) ;
retrun 0 ;
}

Answer: Unpredictable. num not initialised

(f)

# include <stdio.h>
int main( )
{
int k, num = 30 ;
k = ( num > 5 ? ( num <= 10 ? 100 : 200 ) : 500 ) ;
printf ( "%d\n", num ) ;
return 0 ;
}

Answer: 30

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

(a)

# include <stdio.h>
int main( )
{
int code, flag ;
if ( code == 1 & flag == 0 )
printf ( "The eagle has landed\n" ) ;
return 0 ;
}

Answer: To combine two conditions use && and not &.

(b)

# include <stdio.h>
int main( )
{
char spy = 'a', password = 'z' ;
if ( spy == 'a' or password == 'z' )
printf ( "All the birds are safe in the nest\n" ) ;
return 0 ;
}

Answer: ‘or’ cannot be used to combine conditions . Use ||

(c)

# include <stdio.h>
int main( )
{
int i = 10, j = 20 ;
if ( i = 5 ) && if ( j = 10 )
printf ( "Have a nice day\n" ) ;
return 0 ;
}

Answer: Condition should be if ( i == 5 && j == 10 ).

(d)

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

Answer: Replace and with && to combine the conditions.

(e)

# include <stdio.h>
int main( )
{
int x = 2 ;
if ( x == 2 && x != 0 ) ;
printf ( "Hello\n" ) ;
else
printf ( "Bye\n" ) ;
return 0 ;
}

Answer: The if statement should not be terminated by a
semicolon.

(f)

# include <stdio.h>
int main( )
{
int i = 10, j = 10 ;
if ( i && j == 10 )
printf ( "Have a nice day\n" ) ;
return 0 ;
}

Answer: It has no error.

(g)

# include <stdio.h>
int main( )
{
int j = 65 ;
printf ( "j >= 65 ? %d : %c\n", j ) ;
return 0 ;
}

Answer: It has no error.

(h)

# include <stdio.h>
int main( )
{
int i = 10, j ;
i >= 5 ? j = 10 : j = 15 ;
printf ( "%d %d\n", i, j ) ;
return 0 ;
}

Answer: LValue required. Parenthesize j = 10 and j = 15 to
eliminate the error.

(i)

# include <stdio.h>
int main( )
{
int a = 5, b = 6 ;
( a == b ? printf ( "%d\n", a ) ) ;
return 0 ;
} 

Answer: The ? operator must have a matching : operator.

(j)

# include <stdio.h>
int main( )
{
int n = 9 ;
( n == 9 ? printf ( "Correct\n" ) ; : printf ( "Wrong\n" ) ; ) ;
return 0 ;
}

Answer: Remove the ; after each printf( ).

[D] Attempt the following:

(a) Any year is entered through the keyboard, write a program to
determine whether the year is leap or not. Use the logical
operators && and ||.

Program:

# include <stdio.h>
int main( )
{
int year ;
printf ( "\nEnter year: " ) ;
scanf ( "%d", &year ) ;
if ( year % 400 == 0 || year % 100 != 0 && year % 4 == 0 )
printf ( "Leap year\n" ) ;
else
printf ( "Not a leap year\n" ) ;
return 0 ;
}

(b) Any character is entered through the keyboard, write a
program to determine whether the character entered is a
capital letter, a small case letter, a digit or a special symbol.
The following table shows the range of ASCII values for
various characters.

CharactersASCII Values
A – Z
a – z
0 – 9
special symbols
65 – 90
97 – 122
48 – 57
0 – 47, 58 – 64, 91 – 96, 123 – 127

Program:

# include <stdio.h>
int main( )
{
char ch ;
printf ( "\nEnter a character from the keyboard: " ) ;
scanf ( "%c", &ch ) ;
if ( ch >= 65 && ch <= 90 )
printf ( "The character is an uppercase letter\n" ) ;
if ( ch >= 97 && ch <= 122 )
printf ( "The character is a lowercase letter\n" ) ;
if ( ch >= 48 && ch <= 57 )
printf ( "The character is a digit\n" ) ;
if ( ( ch >= 0 && ch < 48 ) || ( ch > 57 && ch < 65 )
 || ( ch > 90 && ch < 97 ) || ch > 122 )
printf ( "The character is a special symbol\n" ) ;
return 0 ;
}

(c) A certain grade of steel is graded according to the following
conditions:
(i) Hardness must be greater than 50
(ii) Carbon content must be less than 0.7
(iii) Tensile strength must be greater than 5600
The grades are as follows:
Grade is 10 if all three conditions are met
Grade is 9 if conditions (i) and (ii) are met
Grade is 8 if conditions (ii) and (iii) are met
Grade is 7 if conditions (i) and (iii) are met
Grade is 6 if only one condition is met
Grade is 5 if none of the conditions are met
Write a program, which will require the user to give values of
hardness, carbon content and tensile strength of the steel
under consideration and output the grade of the steel.

Program:

# include <stdio.h>
int main( )
{
float hard, carbon, tensile ;
printf ( "\nEnter Hardness of steel: " ) ;
scanf ( "%f", &hard ) ;
printf ( "\nEnter Carbon content: " ) ;
scanf ( "%f", &carbon ) ;
printf ( "\nEnter Tensile strength:" ) ;
scanf ( "%f", &tensile ) ;
if ( hard > 50 && carbon < 0.7 && tensile > 5600 )
{
printf ( "Grade 10\n" ) ;
exit ( 0 ) ; /* Terminates the execution */
}
if ( hard > 50 && carbon < 0.7 && tensile <= 5600 )
{
printf ( "Grade 9\n" ) ;
exit ( 0 ) ;
}
if ( hard <= 50 && carbon < 0.7 && tensile > 5600 )
{
printf ( "Grade 8\n" ) ;
exit ( 0 ) ;
}
if ( hard > 50 && carbon >= 0.7 && tensile > 5600 )
{
printf ( "Grade 7\n" ) ;
exit ( 0 ) ;
}
if ( hard > 50 || carbon < 0.7 || tensile > 5600 )
{
printf ( "Grade 6\n" ) ;
exit ( 0 ) ;
}
printf ( "Grade 5\n" ) ;
return 0 ;
}

(d) If the three sides of a triangle are entered through the
keyboard, write a program to check whether the triangle is
valid or not. The triangle is valid if the sum of two sides is
greater than the largest of the three sides.

Program:

# include <stdio.h>
int main( )
{
int side1, side2, side3, largeside, sum ;
printf ( "\nEnter three sides of the triangle: " ) ;
scanf ( "%d %d %d", &side1, &side2, &side3 ) ;
if ( side1 > side2 )
{
if ( side1 > side3 )
{
sum = side2 + side3 ;
largeside = side1 ;
}
else
{
sum = side1 + side2 ;
largeside = side3 ;
}
}
else
{
if ( side2 > side3 )
{
sum = side1 + side3 ;
largeside = side2 ;
}
else
{
sum = side1 + side2 ;
largeside = side3 ;
}
}
if ( sum > largeside )
printf ( "The triangle is a valid triangle\n" ) ;
else
printf ( "The triangle is an invalid triangle\n" ) ;
return 0 ;
}

(e) If the three sides of a triangle are entered through the
keyboard, write a program to check whether the triangle is
isosceles, equilateral, scalene or right-angled triangle.

Program:

# include <stdio.h>
 int main( )
{
int s1, s2, s3, a, b, c ;
printf ( "\nEnter three sides of a triangle: " ) ;
scanf ( "%d %d %d", &s1, &s2, &s3 ) ;
if ( s1 != s2 && s2 != s3 && s3 != s1 )
printf ( "Scalene triangle\n" ) ;
if ( ( s1 == s2 ) && ( s2 != s3 ) )
printf ( "Isosceles triangle\n" ) ;
if ( ( s2 == s3 ) && ( s3 != s1 ) )
printf ( "Isosceles triangle\n" ) ;
if ( ( s1 == s3 ) && ( s3 != s2 ) )
printf ( "Isosceles triangle\n" ) ;
if ( s1 == s2 && s2 == s3 )
printf ( "Equilateral triangle\n" ) ;
a = ( s1 * s1 ) == ( s2 * s2 ) + ( s3 * s3 ) ;
b = ( s2 * s2 ) == ( s1 * s1 ) + ( s3 * s3 ) ;
c = ( s3 * s3 ) == ( s1 * s1 ) + ( s2 * s2 ) ;
if ( a || b || c )
printf ( "Right-angled triangle\n" ) ;
return 0 ;
}

(f) In boxing the weight class of a boxer is decided as per the
following table. Write a program that receives weight as input
and prints out the boxer’s weight class.

Boxer ClassWeight in Pounds
Flyweight
Bantamweight
Featherweight
Middleweight
Heavyweight
< 115
115 – 121
122 – 153
154 – 189
>= 190

Program:

# include <stdio.h>
int main( )
{
int wt ;
printf ( "Enter weight in pounds: " ) ;
scanf ( "%d", &wt ) ;
if ( wt < 115 )
printf ( "Flyweight category\n" ) ;
if ( wt >= 115 && wt <= 121 )
printf ( "Bantamweight category\n" ) ;
if ( wt >= 122 && wt <= 153 )
printf ( "Featherweight category\n" ) ;
if ( wt >= 154 && wt <= 189 )
printf ( "Middleweight category\n" ) ;
if ( wt >= 190 )
printf ( "Heavyweight category\n" ) ;
return 0 ;
}

(g) In digital world colors are specified in Red-Green-Blue
(RGB) format, with values of R, G, B varying on an integer
scale from 0 to 255. In print publishing the colors are
mentioned in Cyan-Magenta-Yellow-Black (CMYK) format,
with values of C, M, Y, and K varying on a real scale from
0.0 to 1.0. Write a program that converts RGB color to
CMYK color as per the following formulae:

Program:

# include <stdio.h>
int main( )
{
float red, green, blue ;
float white, cyan, magenta, yellow, black ;
float max ;
printf ( "\nEnter Red, Green, Blue values (0 to 255): " ) ;
scanf ( "%f %f %f", &red, &green, &blue ) ;
if ( red == 0 && green == 0 && blue == 0 )
{
cyan = magenta = yellow = 0 ;
black = 1 ;
}
else
{
red = red / 255 ;
green = green / 255 ;
blue = blue / 255 ;
max = red ;
if ( green > max )
max = green ;
if ( blue > max )
max = blue ;
white = max ;
cyan = ( white - red ) / white ;
magenta = ( white - green ) / white ;
yellow = ( white - blue ) / white ;
black = 1 - white ;
}
printf("CMYK = %f %f %f %f\n", cyan, magenta, yellow, black ) ;
return 0 ;
}

(h) Write a program that receives month and date of birth as input
and prints the corresponding Zodiac sign based on the
following table:

Zodiac SignFrom – To
Capricorn
Aquarius
Pisces
Aries
Taurus
Gemini
Cancer
Leo
Virgo
Libra
Scorpio
Sagittarius
December 22 – January 19
January 20 – February 17
February 18 – March 19
March 20 – April 19
April 20 – May 20
May 21 – June 20
June 21 – July 22
July 23 – August 22
August 23 – September 22
September 23 – October 22
October 23 – November 21
November 22 – December 21
# include <stdio.h>
int main( )
{
int d, m ;
printf ( "Enter day and month of birth: " ) ;
scanf ( "%d %d", &d, &m ) ;
if ( d >= 22 && m == 12 || d <= 19 && m == 1 )
printf ( "Capricon\n" ) ;
if ( d >= 20 && m == 1 || d <= 17 && m == 2 )
printf ( "Aquarius\n" ) ;
if ( d >= 18 && m == 2 || d <= 19 && m == 3 )
printf ( "Pisces\n" ) ;
if ( d >= 20 && m == 3 || d <= 19 && m == 4 )
printf ( "Aries\n" ) ;
if ( d >= 20 && m == 4 || d <= 20 && m == 5 )
printf ( "Taurus\n" ) ;
if ( d >= 21 && m == 5 || d <= 20 && m == 6 )
printf ( "Gemini\n" ) ;
if ( d >= 21 && m == 6 || d <= 22 && m == 7 )
printf ( "Cancer\n" ) ;
if ( d >= 23 && m == 7 || d <= 22 && m == 8 )
printf ( "Leo\n" ) ;
if ( d >= 23 && m == 8 || d <= 22 && m == 9 )
printf ( "Virgo\n" ) ;
if ( d >= 23 && m == 9 || d <= 22 && m == 10 )
printf ( "Libra\n" ) ;
if ( d >= 23 && m == 10 || d <= 21 && m == 11 )
printf ( "Scorpio\n" ) ;
if ( d >= 22 && m == 11 || d <= 21 && m == 12 )
printf ( "Sagittarius\n" ) ;
return 0 ;
}

(i) The Body Mass Index (BMI) is defined as ratio of the weight
of a person (in kilograms) to the square of the height (in
meters). Write a program that receives weight and height,
calculates the BMI, and reports the BMI category as per the
following table:

BMI CategoryBMI
Starvation
Anorexic
Underweight
Ideal
Overweight
Obese
Morbidly Obese
< 15
15.1 to 17.5
17.6 to 18.5
18.6 to 24.9
25 to 25.9
30 to 30.9
>= 40

Answer:

# include <stdio.h>
# include <math.h>
int main( )
{
float wt, ht, bmi ;
printf ( "Enter weight in kg and height in meters: " ) ;
scanf ( "%f %f", &wt, &ht ) ;
bmi = wt / ( ht * ht ) ;
printf ( "Body Mass Index = %f\n", bmi ) ;
if ( bmi < 15 )
printf ( "BMI Category: Starvation\n" ) ;
else if ( bmi < 17.5 )
printf ( "BMI Category: Anorexic\n" ) ;
else if ( bmi < 18.5 )
printf ( "BMI Category: Underweight\n" ) ;
else if ( bmi < 25 )
printf ( "BMI Category: Ideal\n" ) ;
else if ( bmi < 30 )
printf ( "BMI Category: Overweight\n" ) ;
else if ( bmi < 40 )
printf ( "BMI Category: Obese\n" ) ;
else
printf ( "BMI Category: Morbidly Obese\n" ) ;
return 0 ;
}

[E] Attempt the following:

(a) Using conditional operators determine:
(1) Whether the character entered through the keyboard is a
lower case alphabet or not.

Program:

# include <stdio.h>
int main( )
{
char ch ;
printf ( "Enter character" ) ;
scanf ( "%c", &ch ) ;
ch >= 97 && ch <= 122 ? printf ( "Character entered is lower
 case\n" ) : printf ( "Character entered is not lower case\n" ) ;
return 0 ;
}

(2) Whether a character entered through the keyboard is a
special symbol or not.

Program:

# include <stdio.h>
int main( )
{
char ch ;
printf ( "Enter character: " ) ;
scanf ( "%c", &ch ) ;
( ( ch >= 0 && ch <= 47 ) || ( ch >= 58 && ch <= 64 ) ||
 ( ch >= 91 && ch <= 96 ) || ( ch >= 123 ) ) ?
 printf ( "Character entered is a special symbol\n" ) :
 printf ( "Character entered is not a special symbol\n" ) ;
return 0 ;
}

(b) Write a program using conditional operators to determine
whether a year entered through the keyboard is a leap year or
not.

Program:

# include <stdio.h>
int main( )
{
int year ;
printf ( "Enter Year: " ) ;
scanf ( "%d", &year ) ;
year % 100 == 0 ? ( year % 400 == 0 ? printf ( " Leap Year\n" )
: printf ( "Not a Leap Year\n" ) ) : ( year % 4 == 0 ?
printf ( "Leap Year\n" ) : printf ( "Not A Leap Year\n" ) ) ;
return 0 ;
}

(c) Write a program to find the greatest of three numbers entered
through the keyboard. Use conditional operators.

Program:

# include <stdio.h>
int main( )
{
int n1, n2, n3, great ;
printf ( "\nEnter three numbers: " ) ;
scanf ( "%d %d %d", &n1, &n2, &n3 ) ;
great = n1 > n2 ? ( n1 > n3 ? n1 : n3 ) : ( n2 > n3 ? n2 : n3 ) ;
printf ( "Greatest number is: %d", great ) ;
return 0 ;
}

(d) Write a program to receive value of an angle in degrees and
check whether sum of squares of sine and cosine of this angle
is equal to 1.

Program:

# include <stdio.h>
int main( )
{
int n1, n2, n3, great ;
printf ( "\nEnter angle in degrees: " ) ;
scanf ( "%d", &angle ) ;
angle = angle * 3.14 / 180 ;
sum = pow ( sin ( angle ), 2 ) + pow ( cos ( angle ), 2 ) ) ;
if ( sum == 1 )
printf ( " sum of squares of sine and cosine is equal to 1" ) ;
else
printf ( " sum of squares of sine and cosine is not equal to 1" ) ;
return 0 ;
}

(e) Rewrite the following programs using conditional operators.

# include <stdio.h>
int main( )
{
float sal ;
printf ( "Enter the salary" ) ;
scanf ( "%f", &sal ) ;
if ( sal >= 25000 && sal <= 40000 )
printf ( "Manager\n" ) ;
else
if ( sal >= 15000 && sal < 25000 )
printf ( "Accountant\n" ) ;
else
printf ( "Clerk\n" ) ;
return 0 ;
}

Answer:

# include <stdio.h>
int main( )
{
float sal ;
printf ( "Enter the salary" ) ;
scanf ( "%f", &sal ) ;
( sal >= 25000 && sal <= 40000 ? printf ( "Manager\n" ) :
( sal >= 15000 && sal < 25000 ? printf ( "Accountant\n" ) :
printf ( "Clerk\n" ) ) ) ;
return 0 ;
}

These were the solutions of Chapter 3: Decision Control Instruction of Let Us C Book by Yashvant Kanetkar

PreviousNext
Chapter 3: Decision Control InstructionChapter 5: Loop Control Instruction

Leave a Reply

Your email address will not be published.