SlideShare uma empresa Scribd logo
1 de 22
Baixar para ler offline
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;
}

Mais conteúdo relacionado

Mais procurados

Chapter 1 : Balagurusamy_ Programming ANsI in C
Chapter 1  :  Balagurusamy_ Programming ANsI in C Chapter 1  :  Balagurusamy_ Programming ANsI in C
Chapter 1 : Balagurusamy_ Programming ANsI in C BUBT
 
Let us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solutionLet us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solutionHazrat Bilal
 
Chapter 6 Balagurusamy Programming ANSI in c
Chapter 6  Balagurusamy Programming ANSI  in cChapter 6  Balagurusamy Programming ANSI  in c
Chapter 6 Balagurusamy Programming ANSI in cBUBT
 
Chapter 2 : Balagurusamy_ Programming ANsI in C
Chapter 2 :  Balagurusamy_ Programming ANsI in CChapter 2 :  Balagurusamy_ Programming ANsI in C
Chapter 2 : Balagurusamy_ Programming ANsI in CBUBT
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in cBUBT
 
Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.Hazrat Bilal
 
The solution manual of programming in ansi by Robin
The solution manual of programming in ansi by RobinThe solution manual of programming in ansi by Robin
The solution manual of programming in ansi by RobinShariful Haque Robin
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in cBUBT
 
Exercices pascal tous les chapitres
Exercices pascal tous les chapitresExercices pascal tous les chapitres
Exercices pascal tous les chapitresborhen boukthir
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robinAbdullah Al Naser
 
Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C BUBT
 
Exercices en turbo pascal sur les nombres
Exercices en turbo pascal sur les nombresExercices en turbo pascal sur les nombres
Exercices en turbo pascal sur les nombressalah fenni
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignmentSaket Pathak
 
Strings in C language
Strings in C languageStrings in C language
Strings in C languageP M Patil
 
DAC CCAT GUESS PAPER Jun-Jul 2013
DAC CCAT GUESS PAPER Jun-Jul 2013 DAC CCAT GUESS PAPER Jun-Jul 2013
DAC CCAT GUESS PAPER Jun-Jul 2013 prabhatjon
 
C programs
C programsC programs
C programsMinu S
 
Simple c program
Simple c programSimple c program
Simple c programRavi Singh
 

Mais procurados (20)

Chapter 1 : Balagurusamy_ Programming ANsI in C
Chapter 1  :  Balagurusamy_ Programming ANsI in C Chapter 1  :  Balagurusamy_ Programming ANsI in C
Chapter 1 : Balagurusamy_ Programming ANsI in C
 
Let us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solutionLet us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solution
 
Chapter 6 Balagurusamy Programming ANSI in c
Chapter 6  Balagurusamy Programming ANSI  in cChapter 6  Balagurusamy Programming ANSI  in c
Chapter 6 Balagurusamy Programming ANSI in c
 
Chapter 2 : Balagurusamy_ Programming ANsI in C
Chapter 2 :  Balagurusamy_ Programming ANsI in CChapter 2 :  Balagurusamy_ Programming ANsI in C
Chapter 2 : Balagurusamy_ Programming ANsI in C
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in c
 
Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.
 
The solution manual of programming in ansi by Robin
The solution manual of programming in ansi by RobinThe solution manual of programming in ansi by Robin
The solution manual of programming in ansi by Robin
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
 
Exercices pascal tous les chapitres
Exercices pascal tous les chapitresExercices pascal tous les chapitres
Exercices pascal tous les chapitres
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
 
Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C
 
Exercices en turbo pascal sur les nombres
Exercices en turbo pascal sur les nombresExercices en turbo pascal sur les nombres
Exercices en turbo pascal sur les nombres
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
DAC CCAT GUESS PAPER Jun-Jul 2013
DAC CCAT GUESS PAPER Jun-Jul 2013 DAC CCAT GUESS PAPER Jun-Jul 2013
DAC CCAT GUESS PAPER Jun-Jul 2013
 
C programs
C programsC programs
C programs
 
Simple c program
Simple c programSimple c program
Simple c program
 

Semelhante a LET US C (5th EDITION) CHAPTER 2 ANSWERS

Semelhante a LET US C (5th EDITION) CHAPTER 2 ANSWERS (20)

C file
C fileC file
C file
 
Simple C programs
Simple C programsSimple C programs
Simple C programs
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
Najmul
Najmul  Najmul
Najmul
 
C basics
C basicsC basics
C basics
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
C programms
C programmsC programms
C programms
 
Best C Programming Solution
Best C Programming SolutionBest C Programming Solution
Best C Programming Solution
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
SaraPIC
SaraPICSaraPIC
SaraPIC
 
C programming
C programmingC programming
C programming
 
C-programs
C-programsC-programs
C-programs
 

Último

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 

Último (20)

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 

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; }