2
Most read
9
Most read
10
Most read
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
[A] 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 ) ;
}
Ans) garbage_value 200
(b) main( )
{
int a = 500, b, c ;
if ( a >= 400 )
b = 300 ;
c = 200 ;
printf ( "n%d %d", b, c ) ;
Ans) 300 200
(c) main( )
{
int x = 10, y = 20 ;
if ( x == y ) ;
printf ( "n%d %d", x, y ) ;
}
Ans) nothing is going to print
(d) main( )
{
int x = 3, y = 5 ;
if ( x == 3 )
printf ( "n%d", x ) ;
else ;
printf ( "n%d", y ) ;
}
Ans) 3
(e) 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" ) ;
Ans) x and y are equal
(f) main( )
{
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
int x = 3, y, z ;
y = x = 10 ;
z = x < 10 ;
printf ( "nx = %d y = %d z = %d", x, y, z ) ;
}
Ans) x=10 y=10 z=0
(g) main( )
{
int k = 35 ;
printf ( "n%d %d %d", k == 35, k = 50, k > 40 ) ;
}
Ans) 0 50 0
(h) main( )
{
int i = 65 ;
char j = ‘A’ ;
if ( i == j )
printf ( “C is WOW” ) ;
else
printf( "C is a headache" ) ;
}
Ans) C is WOW
(i) main( )
{
int a = 5, b, c ;
b = a = 15 ;
c = a < 15 ;
printf ( "na = %d b = %d c = %d", a, b, c ) ;
}
Ans) a = 15 b = 15 c = 0
(j) main( )
{
int x = 15 ;
printf ( "n%d %d %d", x != 15, x = 20, x < 30 ) ;
}
Ans) 1 20 1
[B] Point out the errors, if any, in the following programs:
(a) main( )
{
float a = 12.25, b = 12.52 ;
if ( a = b )
printf ( "na and b are equal" ) ;
}
Ans) We need to use == instead of = in the if statement
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
(b) main( )
{
int j = 10, k = 12 ;
if ( k >= j )
{
{
k = j ;
j = k ;
}
}
}
Ans) There should be only one pair of curved brackets.
(c) main( )
{
if ( 'X' < 'x' )
printf ( "nascii value of X is smaller than that of x" ) ;
}
Ans) There is no error.
(d) main( )
{
int x = 10 ;
if ( x >= 2 ) then
printf ( "n%d", x ) ;
}
Ans) No then is required.
(e) main( )
{
int x = 10 ;
if x >= 2
printf ( "n%d", x ) ;
}
Ans) The statement after the if should be in braces
(f) main( )
{
int x = 10, y = 15 ;
if ( x % 2 = y % 3 )
printf ( "nCarpathians" ) ;
}
Ans) We need to use == instead of =
(g) main( )
{
int x = 30 , y = 40 ;
if ( x == y )
printf( "x is equal to y" ) ;
Else if ( x > y )
printf( "x is greater than y" ) ;
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
Else if ( x < y )
printf( "x is less than y" ) ;
}
Ans) There is no error.
(h) main( )
{
int x = 10 ;
if ( x >= 2 ) then
printf ( "n%d", x ) ;
}
Ans) No then is required
(i) main( )
{
int a, b ;
scanf ( "%d %d",a, b ) ;
if ( a > b ) ;
printf ( "This is a game" ) ;
else
printf ( "You have to play it" ) ;
}
Ans) There should be no semicolon after the if statement.
[C] Attempt the following:
(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.
#include <stdio.h>
int main()
{
int sp,cp;
printf("Enter the cost price: ");
scanf("%d",&cp);
printf("Enter the selling price: ");
scanf("%d",&sp);
int gain = 0;
int loss = 0;
if (cp>sp)
{
loss = cp-sp;
printf("You have incurred loss. The loss is: %d",loss);
}
else if(cp<sp)
{
gain = sp-cp;
printf("You have incurred gain, The gain is: %d",gain);
return 0;
}
}
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
(b) Any integer is input through the keyboard. Write a program to find out whether it is an odd
number or even number
#include <stdio.h>
int main()
{
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0)
{
printf("The number is an even number");
}
else
{
printf("The number is an odd number");
return 0;
}
(c) Any year is input through the keyboard. Write a program to determine whether the year is a leap
year or not.
(Hint: Use the % (modulus) operator)
#include <stdio.h>
int main()
{
int number=0;
printf("Enter a year:");
scanf("%d",&number);
if(number%4==0)
{
printf("The year is a leap year");
}
else
{
printf("The year is not a leap year");
}
return 0;
}
(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.
#include <stdio.h>
int main()
{
int month=1,year,a,day=1;
printf("Enter the year: ");
scanf("%d",&year);
a=(14-month)/12;
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
year=year-a;
month=month+12*a-2;
day=(day+year+(year/4)-(year/100)+(year/400)+((31*month)/12)) % 7;
if (day == 0)
printf("The day is sunday.");
else if (day == 1)
printf("The day is monday.");
else if (day == 2)
printf("The day is tuesday.");
else if (day == 3)
printf("The day is wednesday.");
else if (day == 4)
printf("The day is thursday.");
else if (day == 5)
printf("The day is friday.");
else if (day == 6)
printf("The day is saturday.");
return 0;
}
(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.
#include <stdio.h>
int main()
{
int num,reversed_num,a,b,c,d,e,d1,d2,d3,d4,d5;
printf("Enter any five digit number: ");
scanf("%d",&num);
a = num/10;
d5 = num%10;
b = a/10;
d4 = a%10;
c = b/10;
d3 = b%10;
d = c/10;
d2 = c%10;
e = d/10;
d1 = d%10;
reversed_num = (d5*10000) + (d4*1000) + (d3*100) + (d2*10) + (d1*1);
printf("The sum of the reversed digits of the five-digit number you had entered is:
%d",reversed_num);
return 0;
}
(f) If the ages of Ram, Shyam and Ajay are input through the keyboard, write a program to determine
the youngest of the three.
#include <stdio.h>
int main()
{
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
int shyam_age,ram_age,ajay_age,highestage;
printf("Enter the age of shyam: ");
scanf("%d",&shyam_age);
printf("Enter the age of Ram: ");
scanf("%d",&ram_age);
printf("Enter the age of ajay: ");
scanf("%d",&ajay_age);
if (shyam_age<ajay_age && shyam_age<ram_age)
printf("Shyam is the youngest");
else if(ajay_age<shyam_age && ajay_age<ram_age)
printf("Ajay is the youngest.");
else
printf("Ram is the youngest");
return 0;
}
(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.
#include <stdio.h>
int main()
{
int ang1,ang2,ang3,ang_sum=180;
printf("Enter the value of angle 1: ");
scanf("%d",&ang1);
printf("Enyter the value of angle 2: ");
scanf("%d",&ang2);
printf("Enyter the value of angle 3: ");
scanf("%d",&ang3);
if (ang_sum == ang1 + ang2 + ang3)
printf("The triangle is valid.");
else
printf("The triangle is not valid.");
return 0;
}
(h) Find the absolute value of a number entered through the keyboard.
#include <stdio.h>
int main()
{
int num,absolute_num;
printf("Enter a number: ");
scanf("%d",&num);
absolute_num = -(num);
if (num >= 0)
printf("The absolute value of the number you had entered is: %d",num);
else
printf("The absolute value of the number you had entered is: %d",absolute_num);
return 0;
}
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
(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.
#include <stdio.h>
int main()
{
int length,breadth,perimeter,area;
printf("Enter the length of the rectangle: ");
scanf("%d",&length);
printf("Enter the breadth of the rectangle: ");
scanf("%d",&breadth);
perimeter = 2*(length + breadth);
area = length*breadth;
if(perimeter>area)
printf("The perimeter of the rectangle with dimensions %d and %d is greater than the area of this
rectangle.",length,breadth);
else
printf("The area of the rectangle with dimensions %d and %d is greater than the perimeter of this
rectangle.",length,breadth);
return 0;
}
(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.
#include <stdio.h>
int main()
{
int x1,y1,x2,y2,x3,y3,slope1,slope2;
printf("Enter the coordinates of first point (x1 and y1) : ");
scanf("%d,%d",&x1,&y1);
printf("Enter the coordinates of second point (x2 and y2) :");
scanf("%d,%d",&x2,&y2);
printf("nEnter the coordinates of third point (x3 and y3) : ");
scanf("%d,%d",&x3,&y3);
slope1 = (y2-y1)/(x2-x1);
slope2 = (y3-y2)/(x3-x2);
if (slope1 == slope2)
printf("The given points fall on a straight line.");
else
printf("The given points do not fall on a straight line.");
return 0;
}
(k) Given the coordinates (x, y) of a center of a circle and it’s radius, write a program which will
determine whether a point lies inside the circle, on the circle or outside the circle. (Hint: Use sqrt( )
and pow( ) functions)
#include <stdio.h>
#include <math.h>
int main()
{
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
float x1,y1,x,y,r,a,a1;
printf("Enter the coordinates of the center of the circle(x and y) : ");
scanf("%f,%f",&x,&y);
printf("Enter the radius of the circle: ");
scanf("%f",&r);
printf("Enter the coordinates of a point in the plane of the circle(x and y) : ");
scanf("%f,%f",&x1,&y1);
a = (((x-x1)*(x-x1))+((y-y1)*(y-y1)));
a1 = sqrt(a);
if (a1 < r)
printf("The point lies inside the circle.");
else if (a1 == r)
printf("The point lies on the circle.");
else if (a1 > r)
printf("The point lies outside the circle.");
return 0;
}
(l) Given a point (x, y), write a program to find out if it lies on the x-axis, y-axis or at the origin, viz. (0,
0).
#include <stdio.h>
int main()
{
int x,y;
printf("Enter the coordinates of a point (x,y) : ");
scanf("%d,%d",&x,&y);
if (x == 0 && y != 0 )
printf("The point lies on x axis.");
else if (x != 0 && y == 0)
printf("The point lies on y axis.");
else if (x == 0 && y == 0)
printf("The point lies on origin.");
return 0;
}
Logical Operators
If a = 10, b = 12, c = 0, find the values of the expressions in the following table:
Expression Value
a != 6 && b > 5 1
a == 9 || b < 3 0
! ( a < 10 ) 1
! ( a > 5 && c ) 0
5 && c != 8 || !c 1
[D] What would be the output of the following programs:
(a) main( )
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
{
int i = 4, z = 12 ;
if ( i = 5 || z > 50 )
printf ( "nDean of students affairs" ) ;
else
printf ( "nDosa" ) ;
}
Ans) Dean of student affairs
(b) main( )
{
int i = 4, z = 12 ;
if ( i = 5 && z > 5 )
printf ( "nLet us C" ) ;
else
printf ( "nWish C was free !" ) ;
}
Ans) Let us C
(c) 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 ( "nw = %d x = %d y = %d z = %d", w, x, y, z ) ;
}
Ans) w = 1 x = 0 y = 1 z = 1
(d) main( )
{
int i = 4, j = -1, k = 0, y, z ;
y = i + 5 && j + 1 || k + 2 ;
z = i + 5 || j + 1 && k + 2 ;
printf ( "ny = %d z = %d", y, z ) ;
}
Ans) y = 1 z = 1
(e) main( )
{
int i = -3, j = 3 ;
if ( !i + !j * 1 )
printf ( "nMassaro" ) ;
else
printf ( "nBennarivo" ) ;
}
Ans) Bennarivo
(f) main( )
{
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
int a = 40 ;
if ( a > 40 && a < 45 )
printf ( "a is greater than 40 and less than 45" ) ;
else
printf ( "%d", a ) ;
}
Ans) 40
(g) main( )
{
int p = 8, q = 20 ;
if ( p == 5 && q > 5 )
printf ( "nWhy not C" ) ;
else
printf ( "nDefinitely C !" ) ;
}
Ans) Definitely C
(h) main( )
{
int i = -1, j = 1, k ,l ;
k = i && j ;
l = i || j ;
printf ( "%d %d", I, j ) ;
}
Ans) -1 1
(i) main( )
{
int x = 20 , y = 40 , z = 45 ;
if ( x > y && x > z )
printf( "x is big" ) ;
else if ( y > x && y > z )
printf( "y is big" ) ;
else if ( z > x && z > y )
printf( "z is big" ) ;
}
Ans) z is big
(j) main ()
{
int i = -1, j = 1, k ,l ;
k = !i && j ;
l = !i || j ;
printf ( "%d %d", i, j ) ;
}
Ans) -1 1
(k) main( )
{
int j = 4, k ;
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
k = !5 && j ;
printf ( "nk = %d", k ) ;
}
Ans) k = 0
[E] Point out the errors, if any, in the following programs:
(a) /* This program
/* is an example of
/* using Logical operators */
main( )
{
int i = 2, j = 5 ;
if ( i == 2 && j == 5 )
printf ( "nSatisfied at last" ) ;
}
Ans) Incorrect comment: it should be:
/* This program
Is an example of
Using logical operators/*
(b) main( )
{
int code, flag ;
if ( code == 1 & flag == 0 )
printf ( "nThe eagle has landed" ) ;
}
Ans) There is no such logical operator as '&' it should be changed to '&&'
(c) main( )
{
char spy = 'a', password = 'z' ;
if ( spy == 'a' or password == 'z' )
printf ( "nAll the birds are safe in the nest" ) ;
}
Ans) There is no such logical operator as 'or' it should be changed to '||'
(d) main( )
{
int i = 10, j = 20 ;
if ( i = 5 ) && if ( j = 10 )
printf ( "nHave a nice day" ) ;
}
Ans) Incorrect statement : 4th statement
Correct statement : if( i == 5 && j == 10)
(e) main( )
{
int x = 10 , y = 20;
if ( x >= 2 and y <=50 )
printf ( "n%d", x ) ;
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
}
Ans) There is no such logical operator as 'and' it should be changed to '&&'
(f) main( )
{
int a, b ;
if ( a == 1 & b == 0 )
printf ( "nGod is Great" ) ;
}
Ans) There is no such logical operator as '&' it should be changed to '&&'
(g) main( )
{
int x = 2;
if ( x == 2 && x != 0 ) ;
{
printf ( "nHi" ) ;
printf( "nHello" ) ;
}
else
printf( "Bye" ) ;
}
Ans) There is No error
(h) main( )
{
int i = 10, j = 10 ;
if ( i && j == 10)
printf ( "nHave a nice day" ) ;
}
Ans) There is No error
[F] 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 ||.
#include <stdio.h>
int main()
{
int number=0;
printf("Enter a year:");
scanf("%d",&number);
if(number%4==0)
{
printf("The year is a leap year");
}
else
{
printf("The year is not a leap year");
}
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
return 0;
}
I don't think it is valuable to try to make this program complex. It should be left as simple as we can.
(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.
Characters ASCII Values
a-z 65 – 90
A-Z 97 – 122
0-9 48 – 57
Special Symbols 0 - 47, 58 - 64, 91 - 96, 123 - 127
#include <stdio.h>
int main()
{
char c;
printf("Enter any character: ");
scanf("%c",&c);
if ((c >= 65) && c <= 90 )
printf("Your character you had enterd is a capital letter.");
else if ((c >= 97) && c <= 122)
printf("The character you had entered is a small case letter.");
else if ((c >= 48) && c <= 57)
printf("The character you ahd entered is a number.");
else if ((((c >= 0 && c<= 47) || c >= 58 && c <= 64) || c >= 91 && c <= 96) || c >= 123 && c <= 127)
printf("The character you had entered is a special character.");
return 0;
}
(c) An Insurance company follows following rules to calculate premium.
(1) If a person’s health is excellent and the person is between 25 and 35 years of age and lives in a
city and is a male then the premium is Rs. 4 per thousand and his policy amount cannot exceed Rs. 2
lakhs.
(2) If a person satisfies all the above conditions except that the sex is female then the premium is Rs.
3 per thousand and her policy amount cannot exceed Rs. 1 lakh.
(3) If a person’s health is poor and the person is between 25 and 35 years of age and lives in a village
and is a male
then the premium is Rs. 6 per thousand and his policy cannot exceed Rs. 10,000.
(4) In all other cases the person is not insured.
Write a program to output whether the person should be insured or not, his/her premium rate and
maximum amount for which he/she can be insured.
#include <stdio.h>
int main()
{
char place,health,gender;
int age,premium_rate,exceed;
printf("Enter your health. If it is E(Excellent or P(Poor): ");
scanf(" %c",&health);
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
fflush(stdin);
printf("Enter the place where you live. If you live in V(village) or C(city): ");
scanf(" %c",&place);
fflush(stdin);
printf("Enter your gender. If it is M(male) or F(female): ");
scanf(" %c",&gender);
fflush(stdin);
printf("Enter your age: ");
scanf(" %d",&age);
if ((((health == 69) && age >= 25 && age <= 35) && place == 67) && gender == 77 )
{
printf("You are ensured.");
printf("nThe premium is rupees 4 per thousand.");
printf("nThe policy amount cannot exceed rupees 2 lakhs");
}
else if ((((health == 69) && age >= 25 && age <= 35) && place == 67) && gender == 70 )
{
printf("You are ensured.");
printf("nThe premium is rupees 3 per thousand.");
printf("nThe policy amount cannot exceed rupees 1 lakhs.");
}
else if ((((health == 80) && age >= 25 && age <= 35) && place == 86) && gender == 77 )
{
printf("You are ensured.");
printf("nThe premium is rupees 6 per thousand.");
printf("nThe policy amount cannot exceed rupees 10,000");
}
else
printf("You are not ensured.");
return 0;
}
(d) 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.
#include <stdio.h>
int main()
{
float carbon_content;
int tensile_strength,hardness;
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
printf("Enter the hardness of steel: ");
scanf(" %d",&hardness);
printf("Enter the carbon content of steel: ");
scanf(" %f",&carbon_content);
printf("Enter the tensile strength of steel: ");
scanf(" %d",&tensile_strength);
if (((hardness > 50) && carbon_content > 0.7) && tensile_strength > 5600)
printf("The grade of steel is grade 10.");
else if (((hardness > 50) && carbon_content > 0.7) && tensile_strength < 5600)
printf("The grade of steel is grade 9.");
else if (((hardness < 50) && carbon_content < 0.7) && tensile_strength > 5600)
printf("The grade of steel is grade 8.");
else if (((hardness > 50) && carbon_content > 0.7) && tensile_strength > 5600)
printf("The grade of steel is grade 7.");
else if (((hardness > 50) || carbon_content < 0.7) || tensile_strength > 5600)
printf("The grade of steel is grade 6.");
else
printf("The grade of steel is grade 5.");
return 0;
}
(e) A library charges a fine for every book returned late. For first 5 days the fine is 50 paise, for 6-10
days fine is one rupee and above 10 days fine is 5 rupees. If you return the book after 30 days your
membership will be cancelled. Write a program to accept the number of days the member is late to
return the book and display the fine or the appropriate message.
#include <stdio.h>
int main()
{
int day;
printf("Enter the number of days youa re late to return the book: ");
scanf(" %d",&day);
if ((day >= 0) && day <= 5)
printf("Your fine is 50 paise.");
else if ((day >= 6) && day <= 10)
printf("Your fine is 1 rupee.");
else if ((day > 10) && day <= 30)
printf("The fine is 5 rupees.");
else if (day > 30)
printf("Your membership is cancelled.");
return 0;
}
(f) 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.
#include <stdio.h>
int main()
{
int side1,side2,side3;
printf("Enter the length of greatest side: ");
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
scanf(" %d",&side1);
printf("Enter the length of second greatest side: ");
scanf(" %d",&side2);
printf("Enter the length of smallest side: ");
scanf(" %d",&side3);
if ((side3 + side2 > side1) && side1 - side2 < side3)
printf("The triangle is valid.");
else
printf("The triangle is not valid.");
return 0;
}
(g) 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.
#include <stdio.h>
int main()
{
int side1,side2,side3;
printf("Enter the length of first side: ");
scanf(" %d",&side1);
printf("Enter the length of second side: ");
scanf(" %d",&side2);
printf("Enter the length of third side: ");
scanf(" %d",&side3);
if (side1 == side2 == side3)
printf("The triangle is equilateral.");
else if (((side1 == side2 ) || side2 == side3 ) || side1 == side3 )
printf("The triangle is isosceles.");
else
printf("The triangle is scalene.");
return 0;
}
(h) In a company, worker efficiency is determined on the basis of the time required for a worker to
complete a particular job. If the time taken by the worker is between 2 – 3 hours, then the worker is
said to be highly efficient. If the time required by the worker is between 3 – 4 hours, then the worker
is ordered to improve speed. If the time taken is between 4 – 5 hours, the worker is given training to
improve his speed, and if the time taken by the worker is more than 5 hours, then the worker has to
leave the company. If the time taken by the worker is input through the keyboard, find the efficiency
of the worker.
#include <stdio.h>
int main()
{
int hour;
printf("Enter the number of hours for you to complete a job: ");
scanf(" %d",&hour);
if ((hour >= 1) && hour <= 3)
printf("Your work is highly efficient.");
else if ((hour > 3) && hour <= 4)
printf("You are ordered to improve your speed.");
else if ((hour > 4) && hour <= 5)
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
printf("You are given training to improve your speed.");
else if (hour > 5)
printf("You have to leave the company.");
return 0;
}
(I) A university has the following rules for a student to qualify for a degree with A as the main
subject and B as the subsidiary subject:
(a) He should get 55 percent or more in A and 45 percent or more in B.
(b) If he gets than 55 percent in A he should get 55 percent or more in B. However, he should get at
least 45 percent in A.
(c) If he gets less than 45 percent in B and 65 percent or more in A he is allowed to reappear in an
examination in B to qualify.
(d) In all other cases he is declared to have failed.
Write a program to receive marks in A and B and Output whether the student has passed, failed or is
allowed to reappear in B.
#include <stdio.h>
int main()
{
int mark1,mark2;
printf("Enter the percentage you got in subject A: ");
scanf(" %d",&mark1);
printf("Enter the percentage you got in subject B: ");
scanf(" %d",&mark2);
if ((mark1 > 55) && mark2 > 45)
printf("You have passed.");
else if ((mark1 > 45) && mark2 > 55)
printf("You have passed.");
else if ((mark1 > 65) && mark2 < 45)
printf("You are allowed to reappear for examination of subject B.");
else
printf("You have failed");
return 0;
}
Conditional operators
[G] What would be the output of the following programs:
(a) main( )
{
int i = -4, j, num ;
j = ( num < 0 ? 0 : num * num ) ;
printf ( "n%d", j ) ;
}
Ans) 0
(b) main( )
{
int k, num = 30 ;
k = ( num > 5 ? ( num <= 10 ? 100 : 200 ) : 500 ) ;
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
printf ( "n%d", num ) ;
}
Ans) 30
(c) main( )
{
int j = 4 ;
( !j != 1 ? printf ( "nWelcome") : printf ( "nGood Bye") ) ;
}
Ans) Welcome
[H] Point out the errors, if any, in the following programs:
(a) main( )
{
int tag = 0, code = 1 ;
if ( tag == 0 )
( code > 1 ? printf ( "nHello" ) ? printf ( "nHi" ) ) ;
else
printf ( "nHello Hi !!" ) ;
}
Ans) Incorrect statement : ( code > 1 ? printf ( "nHello" ) ? printf ( "nHi" ) ) ;
Correct statement : (code > 1 ? Printf("nHello") : printf ("nHi")
(b) main( )
{
int ji = 65 ;
printf ( "nji >= 65 ? %d : %c", ji ) ;
}
Ans) 'ji' is a integer not character and there is no other character variable mentioned.
(c) main( )
{
int i = 10, j ;
i >= 5 ? ( j = 10 ) : ( j = 15 ) ;
printf ( "n%d %d", i, j ) ;
}
Ans) No error
(d) main( )
{
int a = 5 , b = 6 ;
( a == b ? printf( "%d",a) ) ;
}
Ans) There is no ':' in the statement which states it wrong.
(e) main( )
{
int n = 9 ;
( n == 9 ? printf( "You are correct" ) ; : printf( "You are wrong" ) ;) ;
}
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
Ans) There is no requirement of ';' in this statement.
(f) main( )
{
int kk = 65 ,ll ;
ll = ( kk == 65 : printf ( "n kk is equal to 65" ) : printf ( "n kk is not equal to 65" ) ) ;
printf( "%d", ll ) ;
}
Ans) '||' cannot be a variable name. A variable name does not contain any special characters apart
from '_'
(g) main( )
{
int x = 10, y = 20 ;
x == 20 && y != 10 ? printf( "True" ) : printf( "False" ) ;
}
Ans) No error
[I] Rewrite the following programs using conditional operators.
(a) main( )
{
int x, min, max ;
scanf ( "n%d %d", &max, &x ) ;
if ( x > max )
max = x ;
else
min = x ;
}
int main( )
{
int x, min, max ;
scanf ( "n%d %d", &max, &x ) ;
(x > max) ? (max = x) : (min = x)
}
(b) main( )
{
int code ;
scanf ( "%d", &code ) ;
if ( code > 1 )
printf ( "nJerusalem" ) ;
else
if ( code < 1 )
printf ( "nEddie" ) ;
else
printf ( "nC Brain" ) ;
}
int main()
{
int code;
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
scanf(" %d",&code);
code > 1 ? printf("nJerusleum") : (code < 1) ? printf("Eddie") : printf("Brain")
}
(c) main( )
{
float sal ;
printf ("Enter the salary" ) ;
scanf ( "%f", &sal ) ;
if ( sal < 40000 && sal > 25000 )
printf ( "Manager" ) ;
else
if ( sal < 25000 && sal > 15000 )
printf ( "Accountant" ) ;
else
printf ( "Clerk" ) ;
}
int main( )
{
float sal ;
printf ("Enter the salary" ) ;
scanf ( "%f", &sal ) ;
sal < 40000 && sal > 25000 ? printf("Manager") : ( sal < 25000 && sal > 15000 ) ?
printf("Accountant") : printf("Clerk")
}
[J] Attempt the following:
(a) Using conditional operators determine:
(1) Whether the character entered through the keyboard is a lower case alphabet or not.
(2) Whether a character entered through the keyboard is a special symbol or not.
1. #include <stdio.h>
int main()
{
char c;
printf("Enter any character: ");
scanf("%c",&c);
printf("The character you had entered is a small case letter.");
((c >= 48) && c <= 57) ? printf("The character you had entered is a number.") : printf("The
number you entered was not a small case letter.)
Return 0;
2.
#include <stdio.h>
int main()
{
char c;
printf("Enter any character: ");
scanf("%c",&c);
LET US C (5th
EDITION) CHAPTER 3 ANSWERS
((((c >= 0 && c<= 47) || c >= 58 && c <= 64) || c >= 91 && c <= 96) || c >= 123 && c <= 127) ?
printf("The character you had entered is a special character.") : printf("The number you had
enetered is not a special symbol.")
return 0;
}
(b) Write a program using conditional operators to determine whether a year entered through the
keyboard is a leap year or not.
#include <stdio.h>
int main()
{
int number=0;
printf("Enter a year:");
scanf("%d",&number);
(number%4==0) ? printf("The year is a leap year") : printf("The year is not a leap year");
return 0;
}
I don't think it is valuable to try to make this program complex. It should be left as simple as we can.
(c) Write a program to find the greatest of the three numbers entered through the keyboard using
conditional operators.
#include <stdio.h>
int main()
{
int num1,num2,num3;
printf("Enter the first number: ");
scanf("%d",&num1);
printf("Enter the second number: ");
scanf("%d",&num2);
printf("Enter the third number: ");
scanf("%d",&num3);
(num1>num2 && num1>num3) ? printf("The first number is the greatest") : (num2>num1 &&
num2>num3) ? printf("The second number is the greatest.") : printf("The third number is the
greatest.");
return 0;
}

More Related Content

PDF
Let us c chapter 4 solution
DOCX
Let us C (by yashvant Kanetkar) chapter 3 Solution
DOCX
Chapter 8 c solution
PDF
Let us c(by yashwant kanetkar) chapter 2 solution
PDF
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
DOC
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
PDF
LET US C (5th EDITION) CHAPTER 4 ANSWERS
PDF
LET US C (5th EDITION) CHAPTER 1 ANSWERS
Let us c chapter 4 solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
Chapter 8 c solution
Let us c(by yashwant kanetkar) chapter 2 solution
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
LET US C (5th EDITION) CHAPTER 4 ANSWERS
LET US C (5th EDITION) CHAPTER 1 ANSWERS

What's hot (20)

DOCX
Data structures and algorithms unit i
DOCX
Let us c (by yashvant kanetkar) chapter 1 solution
RTF
PDF
Chapter 1 : Balagurusamy_ Programming ANsI in C
DOCX
Printing different pyramid patterns of numbers,alphabets and stars using C.
PDF
Chapter 6 Balagurusamy Programming ANSI in c
PDF
Chapter 4 : Balagurusamy Programming ANSI in C
PDF
User_Defined_Functions_ppt_slideshare.
PDF
Chapter 3 : Balagurusamy Programming ANSI in C
PDF
Chapter 2 : Balagurusamy_ Programming ANsI in C
PPTX
String In C Language
DOCX
Programming fundamentals
ODP
Function
PDF
Function in C
PPTX
Functions in c
PDF
The solution manual of programming in ansi by Robin
PPTX
Technical aptitude Test 1 CSE
PDF
Function lecture
PPTX
Types of function call
PDF
best notes in c language
Data structures and algorithms unit i
Let us c (by yashvant kanetkar) chapter 1 solution
Chapter 1 : Balagurusamy_ Programming ANsI in C
Printing different pyramid patterns of numbers,alphabets and stars using C.
Chapter 6 Balagurusamy Programming ANSI in c
Chapter 4 : Balagurusamy Programming ANSI in C
User_Defined_Functions_ppt_slideshare.
Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 2 : Balagurusamy_ Programming ANsI in C
String In C Language
Programming fundamentals
Function
Function in C
Functions in c
The solution manual of programming in ansi by Robin
Technical aptitude Test 1 CSE
Function lecture
Types of function call
best notes in c language
Ad

Similar to LET US C (5th EDITION) CHAPTER 2 ANSWERS (20)

DOCX
PDF
Simple C programs
DOCX
ADA FILE
PDF
C Programming Example
DOC
'C' language notes (a.p)
PPT
All important c programby makhan kumbhkar
DOC
Basic c programs updated on 31.8.2020
PPTX
Najmul
DOC
C basics
DOCX
C lab manaual
PDF
Data Structure using C
PDF
C programms
DOCX
Best C Programming Solution
PDF
PCA-2 Programming and Solving 2nd Sem.pdf
DOCX
PCA-2 Programming and Solving 2nd Sem.docx
DOCX
DOCX
SaraPIC
PPTX
C programming
PPTX
c programming lab for first year student
DOC
C-programs
Simple C programs
ADA FILE
C Programming Example
'C' language notes (a.p)
All important c programby makhan kumbhkar
Basic c programs updated on 31.8.2020
Najmul
C basics
C lab manaual
Data Structure using C
C programms
Best C Programming Solution
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.docx
SaraPIC
C programming
c programming lab for first year student
C-programs
Ad

Recently uploaded (20)

PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PPTX
20th Century Theater, Methods, History.pptx
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Share_Module_2_Power_conflict_and_negotiation.pptx
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Cambridge-Practice-Tests-for-IELTS-12.docx
Unit 4 Computer Architecture Multicore Processor.pptx
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Paper A Mock Exam 9_ Attempt review.pdf.
Chinmaya Tiranga quiz Grand Finale.pdf
B.Sc. DS Unit 2 Software Engineering.pptx
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
20th Century Theater, Methods, History.pptx
AI-driven educational solutions for real-life interventions in the Philippine...
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
Introduction to pro and eukaryotes and differences.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS

LET US C (5th EDITION) CHAPTER 2 ANSWERS

  • 1. LET US C (5th EDITION) CHAPTER 3 ANSWERS [A] 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 ) ; } Ans) garbage_value 200 (b) main( ) { int a = 500, b, c ; if ( a >= 400 ) b = 300 ; c = 200 ; printf ( "n%d %d", b, c ) ; Ans) 300 200 (c) main( ) { int x = 10, y = 20 ; if ( x == y ) ; printf ( "n%d %d", x, y ) ; } Ans) nothing is going to print (d) main( ) { int x = 3, y = 5 ; if ( x == 3 ) printf ( "n%d", x ) ; else ; printf ( "n%d", y ) ; } Ans) 3 (e) 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" ) ; Ans) x and y are equal (f) main( ) {
  • 2. LET US C (5th EDITION) CHAPTER 3 ANSWERS int x = 3, y, z ; y = x = 10 ; z = x < 10 ; printf ( "nx = %d y = %d z = %d", x, y, z ) ; } Ans) x=10 y=10 z=0 (g) main( ) { int k = 35 ; printf ( "n%d %d %d", k == 35, k = 50, k > 40 ) ; } Ans) 0 50 0 (h) main( ) { int i = 65 ; char j = ‘A’ ; if ( i == j ) printf ( “C is WOW” ) ; else printf( "C is a headache" ) ; } Ans) C is WOW (i) main( ) { int a = 5, b, c ; b = a = 15 ; c = a < 15 ; printf ( "na = %d b = %d c = %d", a, b, c ) ; } Ans) a = 15 b = 15 c = 0 (j) main( ) { int x = 15 ; printf ( "n%d %d %d", x != 15, x = 20, x < 30 ) ; } Ans) 1 20 1 [B] Point out the errors, if any, in the following programs: (a) main( ) { float a = 12.25, b = 12.52 ; if ( a = b ) printf ( "na and b are equal" ) ; } Ans) We need to use == instead of = in the if statement
  • 3. LET US C (5th EDITION) CHAPTER 3 ANSWERS (b) main( ) { int j = 10, k = 12 ; if ( k >= j ) { { k = j ; j = k ; } } } Ans) There should be only one pair of curved brackets. (c) main( ) { if ( 'X' < 'x' ) printf ( "nascii value of X is smaller than that of x" ) ; } Ans) There is no error. (d) main( ) { int x = 10 ; if ( x >= 2 ) then printf ( "n%d", x ) ; } Ans) No then is required. (e) main( ) { int x = 10 ; if x >= 2 printf ( "n%d", x ) ; } Ans) The statement after the if should be in braces (f) main( ) { int x = 10, y = 15 ; if ( x % 2 = y % 3 ) printf ( "nCarpathians" ) ; } Ans) We need to use == instead of = (g) main( ) { int x = 30 , y = 40 ; if ( x == y ) printf( "x is equal to y" ) ; Else if ( x > y ) printf( "x is greater than y" ) ;
  • 4. LET US C (5th EDITION) CHAPTER 3 ANSWERS Else if ( x < y ) printf( "x is less than y" ) ; } Ans) There is no error. (h) main( ) { int x = 10 ; if ( x >= 2 ) then printf ( "n%d", x ) ; } Ans) No then is required (i) main( ) { int a, b ; scanf ( "%d %d",a, b ) ; if ( a > b ) ; printf ( "This is a game" ) ; else printf ( "You have to play it" ) ; } Ans) There should be no semicolon after the if statement. [C] Attempt the following: (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. #include <stdio.h> int main() { int sp,cp; printf("Enter the cost price: "); scanf("%d",&cp); printf("Enter the selling price: "); scanf("%d",&sp); int gain = 0; int loss = 0; if (cp>sp) { loss = cp-sp; printf("You have incurred loss. The loss is: %d",loss); } else if(cp<sp) { gain = sp-cp; printf("You have incurred gain, The gain is: %d",gain); return 0; } }
  • 5. LET US C (5th EDITION) CHAPTER 3 ANSWERS (b) Any integer is input through the keyboard. Write a program to find out whether it is an odd number or even number #include <stdio.h> int main() { int number=0; printf("Enter a number:"); scanf("%d",&number); if(number%2==0) { printf("The number is an even number"); } else { printf("The number is an odd number"); return 0; } (c) Any year is input through the keyboard. Write a program to determine whether the year is a leap year or not. (Hint: Use the % (modulus) operator) #include <stdio.h> int main() { int number=0; printf("Enter a year:"); scanf("%d",&number); if(number%4==0) { printf("The year is a leap year"); } else { printf("The year is not a leap year"); } return 0; } (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. #include <stdio.h> int main() { int month=1,year,a,day=1; printf("Enter the year: "); scanf("%d",&year); a=(14-month)/12;
  • 6. LET US C (5th EDITION) CHAPTER 3 ANSWERS year=year-a; month=month+12*a-2; day=(day+year+(year/4)-(year/100)+(year/400)+((31*month)/12)) % 7; if (day == 0) printf("The day is sunday."); else if (day == 1) printf("The day is monday."); else if (day == 2) printf("The day is tuesday."); else if (day == 3) printf("The day is wednesday."); else if (day == 4) printf("The day is thursday."); else if (day == 5) printf("The day is friday."); else if (day == 6) printf("The day is saturday."); return 0; } (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. #include <stdio.h> int main() { int num,reversed_num,a,b,c,d,e,d1,d2,d3,d4,d5; printf("Enter any five digit number: "); scanf("%d",&num); a = num/10; d5 = num%10; b = a/10; d4 = a%10; c = b/10; d3 = b%10; d = c/10; d2 = c%10; e = d/10; d1 = d%10; reversed_num = (d5*10000) + (d4*1000) + (d3*100) + (d2*10) + (d1*1); printf("The sum of the reversed digits of the five-digit number you had entered is: %d",reversed_num); return 0; } (f) If the ages of Ram, Shyam and Ajay are input through the keyboard, write a program to determine the youngest of the three. #include <stdio.h> int main() {
  • 7. LET US C (5th EDITION) CHAPTER 3 ANSWERS int shyam_age,ram_age,ajay_age,highestage; printf("Enter the age of shyam: "); scanf("%d",&shyam_age); printf("Enter the age of Ram: "); scanf("%d",&ram_age); printf("Enter the age of ajay: "); scanf("%d",&ajay_age); if (shyam_age<ajay_age && shyam_age<ram_age) printf("Shyam is the youngest"); else if(ajay_age<shyam_age && ajay_age<ram_age) printf("Ajay is the youngest."); else printf("Ram is the youngest"); return 0; } (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. #include <stdio.h> int main() { int ang1,ang2,ang3,ang_sum=180; printf("Enter the value of angle 1: "); scanf("%d",&ang1); printf("Enyter the value of angle 2: "); scanf("%d",&ang2); printf("Enyter the value of angle 3: "); scanf("%d",&ang3); if (ang_sum == ang1 + ang2 + ang3) printf("The triangle is valid."); else printf("The triangle is not valid."); return 0; } (h) Find the absolute value of a number entered through the keyboard. #include <stdio.h> int main() { int num,absolute_num; printf("Enter a number: "); scanf("%d",&num); absolute_num = -(num); if (num >= 0) printf("The absolute value of the number you had entered is: %d",num); else printf("The absolute value of the number you had entered is: %d",absolute_num); return 0; }
  • 8. LET US C (5th EDITION) CHAPTER 3 ANSWERS (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. #include <stdio.h> int main() { int length,breadth,perimeter,area; printf("Enter the length of the rectangle: "); scanf("%d",&length); printf("Enter the breadth of the rectangle: "); scanf("%d",&breadth); perimeter = 2*(length + breadth); area = length*breadth; if(perimeter>area) printf("The perimeter of the rectangle with dimensions %d and %d is greater than the area of this rectangle.",length,breadth); else printf("The area of the rectangle with dimensions %d and %d is greater than the perimeter of this rectangle.",length,breadth); return 0; } (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. #include <stdio.h> int main() { int x1,y1,x2,y2,x3,y3,slope1,slope2; printf("Enter the coordinates of first point (x1 and y1) : "); scanf("%d,%d",&x1,&y1); printf("Enter the coordinates of second point (x2 and y2) :"); scanf("%d,%d",&x2,&y2); printf("nEnter the coordinates of third point (x3 and y3) : "); scanf("%d,%d",&x3,&y3); slope1 = (y2-y1)/(x2-x1); slope2 = (y3-y2)/(x3-x2); if (slope1 == slope2) printf("The given points fall on a straight line."); else printf("The given points do not fall on a straight line."); return 0; } (k) Given the coordinates (x, y) of a center of a circle and it’s radius, write a program which will determine whether a point lies inside the circle, on the circle or outside the circle. (Hint: Use sqrt( ) and pow( ) functions) #include <stdio.h> #include <math.h> int main() {
  • 9. LET US C (5th EDITION) CHAPTER 3 ANSWERS float x1,y1,x,y,r,a,a1; printf("Enter the coordinates of the center of the circle(x and y) : "); scanf("%f,%f",&x,&y); printf("Enter the radius of the circle: "); scanf("%f",&r); printf("Enter the coordinates of a point in the plane of the circle(x and y) : "); scanf("%f,%f",&x1,&y1); a = (((x-x1)*(x-x1))+((y-y1)*(y-y1))); a1 = sqrt(a); if (a1 < r) printf("The point lies inside the circle."); else if (a1 == r) printf("The point lies on the circle."); else if (a1 > r) printf("The point lies outside the circle."); return 0; } (l) Given a point (x, y), write a program to find out if it lies on the x-axis, y-axis or at the origin, viz. (0, 0). #include <stdio.h> int main() { int x,y; printf("Enter the coordinates of a point (x,y) : "); scanf("%d,%d",&x,&y); if (x == 0 && y != 0 ) printf("The point lies on x axis."); else if (x != 0 && y == 0) printf("The point lies on y axis."); else if (x == 0 && y == 0) printf("The point lies on origin."); return 0; } Logical Operators If a = 10, b = 12, c = 0, find the values of the expressions in the following table: Expression Value a != 6 && b > 5 1 a == 9 || b < 3 0 ! ( a < 10 ) 1 ! ( a > 5 && c ) 0 5 && c != 8 || !c 1 [D] What would be the output of the following programs: (a) main( )
  • 10. LET US C (5th EDITION) CHAPTER 3 ANSWERS { int i = 4, z = 12 ; if ( i = 5 || z > 50 ) printf ( "nDean of students affairs" ) ; else printf ( "nDosa" ) ; } Ans) Dean of student affairs (b) main( ) { int i = 4, z = 12 ; if ( i = 5 && z > 5 ) printf ( "nLet us C" ) ; else printf ( "nWish C was free !" ) ; } Ans) Let us C (c) 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 ( "nw = %d x = %d y = %d z = %d", w, x, y, z ) ; } Ans) w = 1 x = 0 y = 1 z = 1 (d) main( ) { int i = 4, j = -1, k = 0, y, z ; y = i + 5 && j + 1 || k + 2 ; z = i + 5 || j + 1 && k + 2 ; printf ( "ny = %d z = %d", y, z ) ; } Ans) y = 1 z = 1 (e) main( ) { int i = -3, j = 3 ; if ( !i + !j * 1 ) printf ( "nMassaro" ) ; else printf ( "nBennarivo" ) ; } Ans) Bennarivo (f) main( ) {
  • 11. LET US C (5th EDITION) CHAPTER 3 ANSWERS int a = 40 ; if ( a > 40 && a < 45 ) printf ( "a is greater than 40 and less than 45" ) ; else printf ( "%d", a ) ; } Ans) 40 (g) main( ) { int p = 8, q = 20 ; if ( p == 5 && q > 5 ) printf ( "nWhy not C" ) ; else printf ( "nDefinitely C !" ) ; } Ans) Definitely C (h) main( ) { int i = -1, j = 1, k ,l ; k = i && j ; l = i || j ; printf ( "%d %d", I, j ) ; } Ans) -1 1 (i) main( ) { int x = 20 , y = 40 , z = 45 ; if ( x > y && x > z ) printf( "x is big" ) ; else if ( y > x && y > z ) printf( "y is big" ) ; else if ( z > x && z > y ) printf( "z is big" ) ; } Ans) z is big (j) main () { int i = -1, j = 1, k ,l ; k = !i && j ; l = !i || j ; printf ( "%d %d", i, j ) ; } Ans) -1 1 (k) main( ) { int j = 4, k ;
  • 12. LET US C (5th EDITION) CHAPTER 3 ANSWERS k = !5 && j ; printf ( "nk = %d", k ) ; } Ans) k = 0 [E] Point out the errors, if any, in the following programs: (a) /* This program /* is an example of /* using Logical operators */ main( ) { int i = 2, j = 5 ; if ( i == 2 && j == 5 ) printf ( "nSatisfied at last" ) ; } Ans) Incorrect comment: it should be: /* This program Is an example of Using logical operators/* (b) main( ) { int code, flag ; if ( code == 1 & flag == 0 ) printf ( "nThe eagle has landed" ) ; } Ans) There is no such logical operator as '&' it should be changed to '&&' (c) main( ) { char spy = 'a', password = 'z' ; if ( spy == 'a' or password == 'z' ) printf ( "nAll the birds are safe in the nest" ) ; } Ans) There is no such logical operator as 'or' it should be changed to '||' (d) main( ) { int i = 10, j = 20 ; if ( i = 5 ) && if ( j = 10 ) printf ( "nHave a nice day" ) ; } Ans) Incorrect statement : 4th statement Correct statement : if( i == 5 && j == 10) (e) main( ) { int x = 10 , y = 20; if ( x >= 2 and y <=50 ) printf ( "n%d", x ) ;
  • 13. LET US C (5th EDITION) CHAPTER 3 ANSWERS } Ans) There is no such logical operator as 'and' it should be changed to '&&' (f) main( ) { int a, b ; if ( a == 1 & b == 0 ) printf ( "nGod is Great" ) ; } Ans) There is no such logical operator as '&' it should be changed to '&&' (g) main( ) { int x = 2; if ( x == 2 && x != 0 ) ; { printf ( "nHi" ) ; printf( "nHello" ) ; } else printf( "Bye" ) ; } Ans) There is No error (h) main( ) { int i = 10, j = 10 ; if ( i && j == 10) printf ( "nHave a nice day" ) ; } Ans) There is No error [F] 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 ||. #include <stdio.h> int main() { int number=0; printf("Enter a year:"); scanf("%d",&number); if(number%4==0) { printf("The year is a leap year"); } else { printf("The year is not a leap year"); }
  • 14. LET US C (5th EDITION) CHAPTER 3 ANSWERS return 0; } I don't think it is valuable to try to make this program complex. It should be left as simple as we can. (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. Characters ASCII Values a-z 65 – 90 A-Z 97 – 122 0-9 48 – 57 Special Symbols 0 - 47, 58 - 64, 91 - 96, 123 - 127 #include <stdio.h> int main() { char c; printf("Enter any character: "); scanf("%c",&c); if ((c >= 65) && c <= 90 ) printf("Your character you had enterd is a capital letter."); else if ((c >= 97) && c <= 122) printf("The character you had entered is a small case letter."); else if ((c >= 48) && c <= 57) printf("The character you ahd entered is a number."); else if ((((c >= 0 && c<= 47) || c >= 58 && c <= 64) || c >= 91 && c <= 96) || c >= 123 && c <= 127) printf("The character you had entered is a special character."); return 0; } (c) An Insurance company follows following rules to calculate premium. (1) If a person’s health is excellent and the person is between 25 and 35 years of age and lives in a city and is a male then the premium is Rs. 4 per thousand and his policy amount cannot exceed Rs. 2 lakhs. (2) If a person satisfies all the above conditions except that the sex is female then the premium is Rs. 3 per thousand and her policy amount cannot exceed Rs. 1 lakh. (3) If a person’s health is poor and the person is between 25 and 35 years of age and lives in a village and is a male then the premium is Rs. 6 per thousand and his policy cannot exceed Rs. 10,000. (4) In all other cases the person is not insured. Write a program to output whether the person should be insured or not, his/her premium rate and maximum amount for which he/she can be insured. #include <stdio.h> int main() { char place,health,gender; int age,premium_rate,exceed; printf("Enter your health. If it is E(Excellent or P(Poor): "); scanf(" %c",&health);
  • 15. LET US C (5th EDITION) CHAPTER 3 ANSWERS fflush(stdin); printf("Enter the place where you live. If you live in V(village) or C(city): "); scanf(" %c",&place); fflush(stdin); printf("Enter your gender. If it is M(male) or F(female): "); scanf(" %c",&gender); fflush(stdin); printf("Enter your age: "); scanf(" %d",&age); if ((((health == 69) && age >= 25 && age <= 35) && place == 67) && gender == 77 ) { printf("You are ensured."); printf("nThe premium is rupees 4 per thousand."); printf("nThe policy amount cannot exceed rupees 2 lakhs"); } else if ((((health == 69) && age >= 25 && age <= 35) && place == 67) && gender == 70 ) { printf("You are ensured."); printf("nThe premium is rupees 3 per thousand."); printf("nThe policy amount cannot exceed rupees 1 lakhs."); } else if ((((health == 80) && age >= 25 && age <= 35) && place == 86) && gender == 77 ) { printf("You are ensured."); printf("nThe premium is rupees 6 per thousand."); printf("nThe policy amount cannot exceed rupees 10,000"); } else printf("You are not ensured."); return 0; } (d) 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. #include <stdio.h> int main() { float carbon_content; int tensile_strength,hardness;
  • 16. LET US C (5th EDITION) CHAPTER 3 ANSWERS printf("Enter the hardness of steel: "); scanf(" %d",&hardness); printf("Enter the carbon content of steel: "); scanf(" %f",&carbon_content); printf("Enter the tensile strength of steel: "); scanf(" %d",&tensile_strength); if (((hardness > 50) && carbon_content > 0.7) && tensile_strength > 5600) printf("The grade of steel is grade 10."); else if (((hardness > 50) && carbon_content > 0.7) && tensile_strength < 5600) printf("The grade of steel is grade 9."); else if (((hardness < 50) && carbon_content < 0.7) && tensile_strength > 5600) printf("The grade of steel is grade 8."); else if (((hardness > 50) && carbon_content > 0.7) && tensile_strength > 5600) printf("The grade of steel is grade 7."); else if (((hardness > 50) || carbon_content < 0.7) || tensile_strength > 5600) printf("The grade of steel is grade 6."); else printf("The grade of steel is grade 5."); return 0; } (e) A library charges a fine for every book returned late. For first 5 days the fine is 50 paise, for 6-10 days fine is one rupee and above 10 days fine is 5 rupees. If you return the book after 30 days your membership will be cancelled. Write a program to accept the number of days the member is late to return the book and display the fine or the appropriate message. #include <stdio.h> int main() { int day; printf("Enter the number of days youa re late to return the book: "); scanf(" %d",&day); if ((day >= 0) && day <= 5) printf("Your fine is 50 paise."); else if ((day >= 6) && day <= 10) printf("Your fine is 1 rupee."); else if ((day > 10) && day <= 30) printf("The fine is 5 rupees."); else if (day > 30) printf("Your membership is cancelled."); return 0; } (f) 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. #include <stdio.h> int main() { int side1,side2,side3; printf("Enter the length of greatest side: ");
  • 17. LET US C (5th EDITION) CHAPTER 3 ANSWERS scanf(" %d",&side1); printf("Enter the length of second greatest side: "); scanf(" %d",&side2); printf("Enter the length of smallest side: "); scanf(" %d",&side3); if ((side3 + side2 > side1) && side1 - side2 < side3) printf("The triangle is valid."); else printf("The triangle is not valid."); return 0; } (g) 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. #include <stdio.h> int main() { int side1,side2,side3; printf("Enter the length of first side: "); scanf(" %d",&side1); printf("Enter the length of second side: "); scanf(" %d",&side2); printf("Enter the length of third side: "); scanf(" %d",&side3); if (side1 == side2 == side3) printf("The triangle is equilateral."); else if (((side1 == side2 ) || side2 == side3 ) || side1 == side3 ) printf("The triangle is isosceles."); else printf("The triangle is scalene."); return 0; } (h) In a company, worker efficiency is determined on the basis of the time required for a worker to complete a particular job. If the time taken by the worker is between 2 – 3 hours, then the worker is said to be highly efficient. If the time required by the worker is between 3 – 4 hours, then the worker is ordered to improve speed. If the time taken is between 4 – 5 hours, the worker is given training to improve his speed, and if the time taken by the worker is more than 5 hours, then the worker has to leave the company. If the time taken by the worker is input through the keyboard, find the efficiency of the worker. #include <stdio.h> int main() { int hour; printf("Enter the number of hours for you to complete a job: "); scanf(" %d",&hour); if ((hour >= 1) && hour <= 3) printf("Your work is highly efficient."); else if ((hour > 3) && hour <= 4) printf("You are ordered to improve your speed."); else if ((hour > 4) && hour <= 5)
  • 18. LET US C (5th EDITION) CHAPTER 3 ANSWERS printf("You are given training to improve your speed."); else if (hour > 5) printf("You have to leave the company."); return 0; } (I) A university has the following rules for a student to qualify for a degree with A as the main subject and B as the subsidiary subject: (a) He should get 55 percent or more in A and 45 percent or more in B. (b) If he gets than 55 percent in A he should get 55 percent or more in B. However, he should get at least 45 percent in A. (c) If he gets less than 45 percent in B and 65 percent or more in A he is allowed to reappear in an examination in B to qualify. (d) In all other cases he is declared to have failed. Write a program to receive marks in A and B and Output whether the student has passed, failed or is allowed to reappear in B. #include <stdio.h> int main() { int mark1,mark2; printf("Enter the percentage you got in subject A: "); scanf(" %d",&mark1); printf("Enter the percentage you got in subject B: "); scanf(" %d",&mark2); if ((mark1 > 55) && mark2 > 45) printf("You have passed."); else if ((mark1 > 45) && mark2 > 55) printf("You have passed."); else if ((mark1 > 65) && mark2 < 45) printf("You are allowed to reappear for examination of subject B."); else printf("You have failed"); return 0; } Conditional operators [G] What would be the output of the following programs: (a) main( ) { int i = -4, j, num ; j = ( num < 0 ? 0 : num * num ) ; printf ( "n%d", j ) ; } Ans) 0 (b) main( ) { int k, num = 30 ; k = ( num > 5 ? ( num <= 10 ? 100 : 200 ) : 500 ) ;
  • 19. LET US C (5th EDITION) CHAPTER 3 ANSWERS printf ( "n%d", num ) ; } Ans) 30 (c) main( ) { int j = 4 ; ( !j != 1 ? printf ( "nWelcome") : printf ( "nGood Bye") ) ; } Ans) Welcome [H] Point out the errors, if any, in the following programs: (a) main( ) { int tag = 0, code = 1 ; if ( tag == 0 ) ( code > 1 ? printf ( "nHello" ) ? printf ( "nHi" ) ) ; else printf ( "nHello Hi !!" ) ; } Ans) Incorrect statement : ( code > 1 ? printf ( "nHello" ) ? printf ( "nHi" ) ) ; Correct statement : (code > 1 ? Printf("nHello") : printf ("nHi") (b) main( ) { int ji = 65 ; printf ( "nji >= 65 ? %d : %c", ji ) ; } Ans) 'ji' is a integer not character and there is no other character variable mentioned. (c) main( ) { int i = 10, j ; i >= 5 ? ( j = 10 ) : ( j = 15 ) ; printf ( "n%d %d", i, j ) ; } Ans) No error (d) main( ) { int a = 5 , b = 6 ; ( a == b ? printf( "%d",a) ) ; } Ans) There is no ':' in the statement which states it wrong. (e) main( ) { int n = 9 ; ( n == 9 ? printf( "You are correct" ) ; : printf( "You are wrong" ) ;) ; }
  • 20. LET US C (5th EDITION) CHAPTER 3 ANSWERS Ans) There is no requirement of ';' in this statement. (f) main( ) { int kk = 65 ,ll ; ll = ( kk == 65 : printf ( "n kk is equal to 65" ) : printf ( "n kk is not equal to 65" ) ) ; printf( "%d", ll ) ; } Ans) '||' cannot be a variable name. A variable name does not contain any special characters apart from '_' (g) main( ) { int x = 10, y = 20 ; x == 20 && y != 10 ? printf( "True" ) : printf( "False" ) ; } Ans) No error [I] Rewrite the following programs using conditional operators. (a) main( ) { int x, min, max ; scanf ( "n%d %d", &max, &x ) ; if ( x > max ) max = x ; else min = x ; } int main( ) { int x, min, max ; scanf ( "n%d %d", &max, &x ) ; (x > max) ? (max = x) : (min = x) } (b) main( ) { int code ; scanf ( "%d", &code ) ; if ( code > 1 ) printf ( "nJerusalem" ) ; else if ( code < 1 ) printf ( "nEddie" ) ; else printf ( "nC Brain" ) ; } int main() { int code;
  • 21. LET US C (5th EDITION) CHAPTER 3 ANSWERS scanf(" %d",&code); code > 1 ? printf("nJerusleum") : (code < 1) ? printf("Eddie") : printf("Brain") } (c) main( ) { float sal ; printf ("Enter the salary" ) ; scanf ( "%f", &sal ) ; if ( sal < 40000 && sal > 25000 ) printf ( "Manager" ) ; else if ( sal < 25000 && sal > 15000 ) printf ( "Accountant" ) ; else printf ( "Clerk" ) ; } int main( ) { float sal ; printf ("Enter the salary" ) ; scanf ( "%f", &sal ) ; sal < 40000 && sal > 25000 ? printf("Manager") : ( sal < 25000 && sal > 15000 ) ? printf("Accountant") : printf("Clerk") } [J] Attempt the following: (a) Using conditional operators determine: (1) Whether the character entered through the keyboard is a lower case alphabet or not. (2) Whether a character entered through the keyboard is a special symbol or not. 1. #include <stdio.h> int main() { char c; printf("Enter any character: "); scanf("%c",&c); printf("The character you had entered is a small case letter."); ((c >= 48) && c <= 57) ? printf("The character you had entered is a number.") : printf("The number you entered was not a small case letter.) Return 0; 2. #include <stdio.h> int main() { char c; printf("Enter any character: "); scanf("%c",&c);
  • 22. LET US C (5th EDITION) CHAPTER 3 ANSWERS ((((c >= 0 && c<= 47) || c >= 58 && c <= 64) || c >= 91 && c <= 96) || c >= 123 && c <= 127) ? printf("The character you had entered is a special character.") : printf("The number you had enetered is not a special symbol.") return 0; } (b) Write a program using conditional operators to determine whether a year entered through the keyboard is a leap year or not. #include <stdio.h> int main() { int number=0; printf("Enter a year:"); scanf("%d",&number); (number%4==0) ? printf("The year is a leap year") : printf("The year is not a leap year"); return 0; } I don't think it is valuable to try to make this program complex. It should be left as simple as we can. (c) Write a program to find the greatest of the three numbers entered through the keyboard using conditional operators. #include <stdio.h> int main() { int num1,num2,num3; printf("Enter the first number: "); scanf("%d",&num1); printf("Enter the second number: "); scanf("%d",&num2); printf("Enter the third number: "); scanf("%d",&num3); (num1>num2 && num1>num3) ? printf("The first number is the greatest") : (num2>num1 && num2>num3) ? printf("The second number is the greatest.") : printf("The third number is the greatest."); return 0; }