Let us C book Chapter 2 Exercise SOlutions
[A] Point out the errors, if any, in the following C statements:
(a) x = ( y + 3 ) ;
Proper
(b) cir = 2 * 3.141593 * r ;
r is undefined
char = '3';
WHere is the name dude , no variable name given
(d) 4 / 3 * 3.14 * r * r * r = vol_of_sphere ;
Right to left associativitiy applies and left side of = should contain only one variable
(e) volume = a 3 ;
Error no exponentiation form avaiable
(f) area = 1 / 2 * base * height ;
Correct
(g) si = p * r * n / 100 ;
perfect
(h) area of circle = 3.14 * r * r ;
space not allowed in variable name
peri_of_tri = a + b + c ;
ok
slope = ( y2 – y1 ) ÷ ( x2 – x1 ) ;
ok
(k) 3 = b = 4 = a ;
NOt ok - > left side of = cannot have constant
count = count + 1 ;
perfect
(m) char ch = '25 Apr 12' ;
not ok only one character allowed in single quote
[B] Evaluate the following expressions and show their hierarchy.
(a) ans = 5 * b * b * x - 3 * a * y * y - 8 * b * b * x + 10 * a * y ;
(a = 3, b = 2, x = 5, y = 4 assume ans to be an int)
100 - 144 -160 + 120 = - 84
(b) res = 4 * a * y / c - a * y / c ;
(a = 4, y = 1, c = 3, assume res to be an int)
res = 4 * 4 * y / ...
res = 16 * 1 / c - ...
res = 16 / 3 - a * y / c
res = 5 - 4 * 1 / 3
res = 5 - 4 / 3
res = 5 - 1
res = 4
(c) s = c + a * y * y / b ;
(a = 2.2, b = 0.0, c = 4.1, y = 3.0, assume s to be an float)
s = 4.1 + 2.2 * 3.0 * y / b
s = 4.1 + 6.6 * 3.0 / 0.0
s = 4.1 + 19.8 / 0.0
UNDEFINEd cannot divide by 0
(d) R = x * x + 2 * x + 1 / 2 * x * x + x + 1 ;
(x = 3.5, assume R to be an float)
R = 3.5 * 3.5 + ....
R = 12.25 + 2 * 3.5 + ...
R = 12.25 + 7 + 1/2 * ...
R = 12.25 + 7 + 0.5 * 3.5 * 3.5 + ..'
R = 12.25 + 7 + 6.125 + 1
R = 26.375
[C] Indicate the order in which the following expressions would be
evaluated:
(a) g = 10 / 5 /2 / 1 ;
g = 10 / 5 / ...
= 2 / 2 / ...
= 1 / 1 ;
= 1
(b) b = 3 / 2 + 5 * 4 / 3 ;
b = 3 / 2 + ....
= 1 + 5 * 4 / ...
= 1 + 20 / 3 ;
= 1 + 6;
= 7
(c) a = b = c = 3 + 4 ;
a = b = c = 7
c = 7
a = b = 7
b = 7
a = 7
(d) x = 2 – 3 + 5 * 2 / 8 % 3 ;
x = ... + 10 / 8 % ...
x = .... + 1 % 3;
x = 2 -3 + 1
x = 0
(e) z = 5 % 3 / 8 * 3 + 434
z = 5 % 3 / ...
= 2 / 8 * ....
= 0 * 3 + ....
= 0 + 434
= 434
(f) y = z = -3 % -8 / 2 + 7 ;
..... = -3 % -8 / ...
... = -3 / 2 + ...
... = -1 + 7
y = z = 6
z = 6
y = 6
D] Convert the following algebraic expressions into equivalent C
statements:
z = ( x + 3 ) * x * x * x / ( y - 4 ) * ( y + 5 );
ex -> x = 4, y = 3
z = ( 7 ) * ... / ( -1 ) * ( 8 ) ;
= ( 7 ) * 4 * 4 * 4 / ( -1 ) * ( 8 )
= 448 / -1 * 8
= -448 * 8
= -3584
ex -> c = 2, g = 4, v = 10, d = 5
= 2 * 10 + ...
= 20 + 6.22 * ( 2 + 5 ) / ...
= ... + 6.22 * ( 7 ) / ..
=... + 43.54 / 4 + ..
= ... + 10.885 + 10
= 40.885
A = 7.7 * b * ( x * y + a ) / c - 0.8 + 2 * b / ( x + a) * ( 1 / y )
ex -> x = 2, y= 1, a = 4, b = 5, c = 6;
= ... * ( x * y + a ) / .... / ( x + a ) * ( 1 / y )
= ... * ( 2 + 4 ) / .. / ( 6 ) * ( 1 )
= .... * ( 6 ) / ... / 6 * 1
= 7.7 * 5 * 6 / 6 - 0.8 + 2 * 5 / 6 * 1
= 231 / 6 - ....
= 38 - .... * 0 * 1
= 38 - .... * 0
= 38 - 0.8 + 2 * 0
= 38 - 0.8 + 0
= 37.2
X = 12 * x * x * x / 4 * x + 8 * x * x / 4 * x + x / 8 * x + 8 / 8 * x
ex => x = 5;
X = 12 * 5 * 5 * 5 / 4 * 5 + ....
= 1875 + 8 * 5 * ...
= ... + 8 * 5 * 5 / 4 * 5 + ...
= ... + 250 + 5 / 8 * 5 + ...
= .... + ..... + 0 + 8 / 8 * 5
= .... + ... + 5
= 1875 + 250 + 5
= 2130
[E] What will be the output of the following programs:
(a) # include <stdio.h>
int main( )
{
int i = 2, j = 3, k, l ;
float a, b ;
k = i / j * j ;
l = j / i * i ;
a = i / j * j ;
b = j / i * i ;
printf ( "%d %d %f %f\n", k, l, a, b ) ;
return 0 ;
}
OUTPUT
0 2 0.00 2.00
(b) # include <stdio.h>
int main( )
{
int a, b, c, d ;
a = 2 % 5 ;
b = -2 % 5 ;
c = 2 % -5 ;
d = -2 % -5 ;
printf ( "a = %d b = %d c = %d d = %d\n", a, b, c, d ) ;
return 0 ;
}
OUTPUT
a = 2
b = -2
c = 2
d = -2
(c) # include <stdio.h>
int main( )
{
float a = 5, b = 2 ;
int c, d ;
c = a % b ;
d = a / 2 ;
printf ( "%d\n", d ) ;
return 0 ;
}
OUTPUT
MOdulus operator does not work with floating point numbers
c = 1.0
d = 2
(d) # include <stdio.h>
int main( )
{
printf ( "nn \n\n nn\n" ) ;
printf ( "nn /n/n nn/n" ) ;
return 0 ;
}
OUTPUT
nn
nn
nn /n/n nn/n
(e) # include <stdio.h>
int main( )
{
int a, b ;
printf ( "Enter values of a and b" ) ;
scanf ( "%d %d", &a, &b ) ;
printf ( "a = %d b = %d", a, b ) ;
return 0 ;
}
OUTPUT
Enter values of a and b 10 20 a = 10 b = 20
[F] State whether the following statements are True or False:
(a) * or /, + or – represents the correct hierarchy of arithmetic
operators in C. True
(b) [ ] and { } can be used in Arithmetic instructions. False , they are reserved character set in C that has special meaning
(c) Hierarchy decides which operator is used first. True
(d) In C, Arithmetic instruction cannot contain constants on left side of
=. True
(e) In C ** operator is used for exponentiation operation. False its pointer to pointer reference operator
(f) % operator cannot be used with floats. True
[G] Fill in the blanks:
(a) In y = 10 * x / 2 + z ; __Multiplication then division then addition____ operation will be performed first.
(b) If a is an integer variable, a = 11 / 2 ; will store int 5
in a.
(c) The expression, a = 22 / 7 * 5 / 3 ; would evaluate to a = 5
(d) The expression x = -7 % 2 - 8 would evaluate to -9
(e) If d is a float the operation d = 2 / 7.0 would store 0.0 in d.
[H] Attempt the following:
(a) If a five-digit number is input through the keyboard, write a
program to calculate the sum of its digits. (Hint: Use the modulus
operator ‘%’)
#include <stdio.h>
int main()
{
int a = 89934, sum = 0, digit=0;
while( a != 0){
digit = a % 10;
sum = sum + digit;
a = a / 10;
}
printf("The sum of digits are : %d", sum);
return 0;
}
(b) If a five-digit number is input through the keyboard, write a
program to reverse the number.
#include <stdio.h>
int main()
{
int a = 89934, sum = 0, digit=0, reverse=0, iterator=100000;
while( a != 0){
digit = a % 10;
iterator = iterator / 10;
reverse = digit * iterator + reverse;
sum = sum + digit;
a = a / 10;
}
printf("The sum of digits are : %d", sum);
printf("\n The reverse of number is : %d", reverse);
return 0;
}
(c) If lengths of three sides of a triangle are input through the
keyboard, write a program to find the area of the triangle.
(d) Write a program to receive Cartesian co-ordinates (x, y) of a point
and convert them into polar co-ordinates (r, ).
Hint: r = sqrt ( x 2 + y 2 ) and
tan -1 ( y / x )
(e) Write a program to receive values of latitude (L1, L2) and longitude
(G1, G2), in degrees, of two places on the earth and output the
distance (D) between them in nautical miles. The formula for
distance in nautical miles is:
D = 3963 cos -1 ( sin L1 sin L2 + cos L1 cos L2 * cos ( G2 – G1 ) )
(f) Wind chill factor is the felt air temperature on exposed skin due to
wind. The wind chill temperature is always lower than the air
temperature, and is calculated as per the following formula:
wcf = 35.74 + 0.6215t + ( 0.4275t - 35.75 ) * v 0.16
where t is the temperature and v is the wind velocity. Write a
program to receive values of t and v and calculate wind chill factor
(wcf).
(g) If value of an angle is input through the keyboard, write a program
to print all its Trigonometric ratios.Chapter 2: C Instructions
37
(h) Two numbers are input through the keyboard into two locations C
and D. Write a program to interchange the contents of C and D.
(i)
Consider a currency system in which there are notes of seven
denominations, namely, Re. 1, Rs. 2, Rs. 5, Rs. 10, Rs. 50, Rs. 100. If
a sum of Rs. N is entered through the keyboard, write a program to
com
These above questions left due to simple maths and logic , common sense.
Comments
Post a Comment