In the third chapter, Let Us C covered all the basic things we need to get started in the journey of learning C Programming. Now, let’s have a look at the solutions of the exercise of the first chapter, Getting Started from Let Us C.
[A] What will be the output of the following programs:
(a)
# include <stdio.h>
int main( )
{
int a = 300, b, c ;
if ( a >= 400 )
b = 300 ;
c = 200 ;
printf ( "%d %d\n", b, c ) ;
return 0 ;
}
Answer: b will contain some garbage value and c will be equal to 200
(b)
# include <stdio.h>
int main( )
{
int a = 500, b, c ;
if ( a >= 400 )
b = 300 ;
c = 200 ;
printf ( "%d %d\n", b, c ) ;
return 0 ;
}
Answer: 300 200
(c)
# include <stdio.h>
int main( )
{
int x = 10, y = 20 ;
if ( x == y ) ;
printf ( "%d %d\n", x, y ) ;
return 0 ;
}
Answer: 10 20
(d)
# include <stdio.h>
int main( )
{
int x = 3 ;
float y = 3.0 ;
if ( x == y )
printf ( "x and y are equal\n" ) ;
else
printf ( "x and y are not equal\n" ) ;
return 0 ;
}
Answer: x and y are equal
(e)
# include <stdio.h>
int main( )
{
int x = 3, y, z ;
y = x = 10 ;
z = x < 10 ;
printf ( "x = %d y = %d z = %d\n", x, y, z ) ;
return 0 ;
}
Answer:x = 10 y = 10 z = 0
(f)
# include <stdio.h>
int main( )
{
int i = 65 ;
char j = 'A' ;
if ( i == j )
printf ( "C is WOW\n" ) ;
else
printf( "C is a headache\n" ) ;
return 0 ;
}
Output: C is WOW
[B] Point out the errors, if any, in the following programs:
(a)
# include <stdio.h>
int main( )
{
float a = 12.25, b = 12.52 ;
if ( a = b )
printf ( "a and b are equal\n" ) ;
return 0 ;
}
Answer: It has no errors
(b)
# include <stdio.h>
int main( )
{
int j = 10, k = 12 ;
if ( k >= j )
{
{
k = j ;
j = k ;
}
}
return 0 ;
}
Answer: It has no errors.
(c)
# include <stdio.h>
int main( )
{
if ( 'X' < 'x' )
printf ( "ascii value of X is smaller than that of x\n" ) ;
}
It has no error.
(d)
# include <stdio.h>
int main( )
{
int x = 10 ;
if ( x >= 2 ) then
printf ( "%d\n", x ) ;
return 0 ;
}
Answer: then is not used in C.
(e)
# include <stdio.h>
int main( )
{
int x = 10, y = 15 ;
if ( x % 2 = y % 3 )
printf ( "Carpathians\n" ) ;
return 0 ;
}
Answer: = is not used to compare. == is used to compare in C.
(f)
# include <stdio.h>
int main( )
{
int x = 30, y = 40 ;
if ( x == y )
printf ( "x is equal to y\n" ) ;
elseif ( x > y )
printf ( "x is greater than y\n" ) ;
elseif ( x < y )
printf ( "x is less than y\n" ) ;
return 0 ;
}
Answer: elseif is not a keyword to be used in C.
(g)
# include <stdio.h>
int main( )
{
int a, b ;
scanf ( "%d %d", a, b ) ;
if ( a > b ) ;
printf ( "This is a game\n" ) ;
else
printf ( "You have to play it\n" ) ;
return 0 ;
}
Answer: There should be ‘&’ before variable in scanf().
[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.
Program:
# include <stdio.h>
int main( )
{
float cp, sp, p, l ;
printf ( "\nEnter cost price and selling price: " ) ;
scanf ( "%f %f", &cp, &sp ) ;
p = sp - cp ;
l = cp - sp ;
if ( p > 0 )
printf ( "The seller has made a profit of Rs.%f\n", p ) ;
if ( l > 0 )
printf ( "The seller is in loss by Rs.%f\n", l ) ;
if ( p == 0 )
printf ( "There is no loss, no profit\n" ) ;
return 0 ;
}
(b) Any integer is input through the keyboard. Write a program to
find out whether it is an odd number or even number.
Program:
# include <stdio.h>
int main( )
{
int n ;
printf ( "\nEnter any number" ) ;
scanf ( "%d", &n ) ;
if ( n % 2 == 0 ) /* remainder after division by 2 */
printf ( "\nThe number is even\n" ) ;
else
printf ( "\nThe number is odd\n" ) ;
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)
Program:
# include <stdio.h>
int main( )
{
int yr ;
printf ( "\nEnter a year:" ) ;
scanf ( "%d", &yr ) ;
if ( yr % 100 == 0 )
{
if ( yr % 400 == 0 )
printf ( "Leap year\n" ) ;
else
printf ( "Not a Leap year\n" ) ;
}
else
{
if ( yr % 4 == 0 )
printf ( "Leap year\n" ) ;
else
printf ( "Not a leap year\n" ) ;
}
return 0 ;
}
(d) According to the Gregorian calendar, it was Monday on the
date 01/01/01. If any year is input through the keyboard write
a program to find out what is the day on 1st January of this
year.
Program:
# include <stdio.h>
int main( )
{
int leapdays, firstday, yr ;
long int normaldays, totaldays ;
printf ( "Enter year:" ) ;
scanf ( "%d", &yr );
normaldays = ( yr - 1 ) * 365L ;
leapdays = ( yr - 1 ) / 4 - ( yr - 1 ) / 100 + ( yr - 1 ) / 400 ;
totaldays = normaldays + leapdays ;
firstday = totaldays % 7 ;
if ( firstday == 0 )
printf ( "Monday\n" ) ;
if ( firstday == 1 )
printf ( "Tuesday\n" ) ;
if ( firstday == 2 )
printf ( "Wednesday\n" ) ;
if ( firstday == 3 )
printf ( "Thursday\n" ) ;
if ( firstday == 4 )
printf ( "Friday\n" ) ;
if ( firstday == 5 )
printf ( "Saturday\n" ) ;
if ( firstday == 6 )
printf ( "Sunday\n" ) ;
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.
Program:
# include <stdio.h>
int main( )
{
int n, a, b, num ;
long int revnum = 0 ;
printf ( "\nEnter a five digit number (less than or equal to 32767): " ) ;
scanf ( "%d", &n ) ;
num = n ;
a = n % 10 ; /* last digit */
n = n / 10 ; /* remaining digits */
revnum = revnum + a * 10000L;
a = n % 10 ; /* 4 th digit */
n = n / 10 ; /* remaining digits */
revnum = revnum + a * 1000;
a = n % 10 ; /* 3 rd digit */
n = n / 10 ; /* remaining digits */
revnum = revnum + a * 100;
a = n % 10 ; /* 2 nd digit */
n = n / 10 ; /* remaining digits */
revnum = revnum + a * 10 ;
a = n % 10 ; /* 1 st digit */
revnum = revnum + a ;
if ( revnum == num )
printf ( "Given number & its reversed number are equal\n" ) ;
else
printf ( "Given number & its reversed number are not equal\n" ) ;
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.
Program:
# include <stdio.h>
int main( )
{
int r, s, a, young ;
printf ( "\nEnter age of Ram, Shyam and Ajay: " ) ;
scanf ( "%d %d %d", &r, &s, &a ) ;
if ( r < s )
{
if ( r < a )
young = r ;
else
young = a ;
}
else
{
if ( s < a )
young = s ;
else
young = a ;
}
printf ( "The youngest of Ram(%d), Shyam(%d) and Ajay(%d) is %d\n", r, s, a, young ) ;
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.
Program:
# include <stdio.h>
int main( )
{
float angle1, angle2, angle3 ;
printf ( "\nEnter three angles of the triangle: " ) ;
scanf ( "%f %f %f", &angle1, &angle2, &angle3 ) ;
if ( ( angle1 + angle2 + angle3 ) == 180 )
printf ( "The triangle is a valid triangle\n" ) ;
else
printf ( "The triangle is an invalid triangle\n" ) ;
return 0 ;
}
(h) Find the absolute value of a number entered through the
keyboard.
Program:
# include <stdio.h>
int main( )
{
int no ;
printf ( "\nEnter any number: " ) ;
scanf ( "%d", &no ) ;
if ( no < 0 )
no = no * -1 ;
printf ( "The absolute value of given number is %d\n", no ) ;
return 0 ;
}
(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.
Program:
# include <stdio.h>
int main( )
{
int l, b, area, peri ;
printf ( "\nEnter length and breadth of the rectangle: " ) ;
scanf ( "%d %d", &l, &b ) ;
area = l * b ;
peri = 2 * ( l + b ) ;
if ( area > peri )
printf ( "area is greater than perimeter\n" ) ;
else
printf ( "area is lesser than perimeter\n" ) ;
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.
Program:
# include <stdio.h>
# include <math.h>
int main( )
{
int x1, y1, x2, y2, x3, y3 ;
int s1, s2, s3 ;
printf ( "\nEnter the values of x1 and y1 coordinates of first point: " ) ;
scanf ( "%d%d", &x1, &y1 ) ;
printf ( "\nEnter the values of x2 and y2 coordinates of first point: " ) ;
scanf ( "%d%d", &x2, &y2 ) ;
printf ( "\nEnter the values of x3 and y3 coordinates of first point: " ) ;
scanf ( "%d%d", &x3, &y3 ) ;
/* Calculate Slope of line between each pair of points */
s1 = abs ( x2- x1 ) / abs ( y2 - y1 ) ;
s2 = abs ( x3 - x1 ) / abs ( y3 - y1 ) ;
s3 = abs ( x3 - x2 ) / abs ( y3 - y2 ) ;
if ( ( s1 == s2 ) && ( s1 == s3 ) )
printf ( "Points are Co-linear\n" ) ;
else
printf ( "Points are NOT Co-linear\n" ) ;
return 0 ;
}
(k) Given the coordinates (x, y) of center of a circle and its
radius, write a program that will determine whether a point lies inside the circle, on the circle or outside the circle. (Hint: Use sqrt( ) and pow( ) functions)
Program:
# include <stdio.h>
int main( )
{
int x, y, r ;
int dis, d ;
printf ( "\nEnter radius of circle and coordinates of point ( x, y ): " ) ;
scanf ( "%d %d %d", &r, &x, &y ) ;
dis = x * x + y * y ; /* or use pow( ) function*/
d = r * r ;
if ( dis == d )
printf ( "Point is on the circle\n" ) ;
else
{
if ( dis > d )
printf ( "Point is outside the circle\n" ) ;
else
printf ( "Point is inside the circle\n" ) ;
}
return 0 ;
}
(l) Given a point (x, y), write a program to find out if it lies on
the x-axis, y-axis or on the origin.
Program:
# include <stdio.h>
int main( )
{
int x, y ;
printf ( "\nEnter the x and y coordinates of a point: " ) ;
scanf ( "%d%d", &x, &y ) ;
if ( x == 0 && y == 0 )
printf ( "Point lies on origin\n" ) ;
else
if ( x == 0 && y != 0 )
printf ( "Point lies on Y axis\n" ) ;
else
if ( x != 0 && y == 0 )
printf ( "Point lies on X axis\n" ) ;
else
printf ( "Point neither lies on any axis, nor origin\n" ) ;
return 0 ;
}
These were the solutions of Chapter 2: C Instructions of Let Us C Book by Yashvant Kanetkar.
your solution is not working
let us c chapter 3
(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
int main( )
{
int r, s, a, young ;
printf ( “\nEnter age of Ram, Shyam and Ajay: ” ) ;
scanf ( “%d %d %d”, &r, &s, &a ) ;
if ( r < s )
{
if ( r < a )
young = r ;
else
young = a ;
}
else
{
if ( s < a )
young = s ;
else
young = a ;
}
printf ( "The youngest of Ram(%d), Shyam(%d) and Ajay(%d) is
%d\n", r, s, a, young ) ;
return 0 ;
}
please check it republish the answer …………..
I’ve updated the code. Kindly check. Apologies.