In the two chapter, the 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] Point out errors, if any, in the following C statements

(a) x = ( y + 3 )

**Answer: **It has no errors

(b) cir = 2 * 3.141593 * r;

**Answer: **It has no errors

(c) char = ‘3’ ;

**Answer: **Keywords are not allowed as variable names

(d) 4 / 3 * 3.14 * r * r * r = vol_of_sphere;

**Answer:** Expressions can’t be placed in the left side, its a place for variables only.

(e) volume = a^{3} ;

**Answer:** We can’t use exponents directly, It should be specified as a * a * a;

(f) area = 1 / 2 * base * height ;

**Answer:** It has no errors.

(g) si = p * r * n / 100 ;

**Answer:** It has no errors

(h) area of circle = 3.14 * r * r ;

**Answer:** area of circle is not a valid variable name, your variable should not contain a space.

(i) peri_of_tri = a + b + c ;

**Answer:** It has no errors.

(j) slope = ( y2 – y1 ) ÷ ( x2 – x1 ) ;

**Answer:** “÷” is not a valid operator

(k) 3 = b = 4 = a ;

**Answer:** Variable name should be on the left side.

(l) count = count + 1 ;

**Answer:** It has no errors

(m) char ch = ’25 Apr 12′ ;

**Answer:** char can only hold a single character, use array for entering multiple character.

#### [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 a int)

**Answer:**

```
ans = 5 * 2 * 2 * 5 - 3 * 3 * 4 * 4 - 8 * 2 * 2 * 5 + 10 * 3 * 4 operation: *
ans = 10 * 2 * 5 - 3 * 3 * 4 * 4 - 8 * 2 * 2 * 5 + 10 * 3 * 4 operation: *
ans = 20 * 5 - 3 * 3 * 4 * 4 - 8 * 2 * 2 * 5 + 10 * 3 * 4 operation: *
ans = 100 - 3 * 3 * 4 * 4 - 8 * 2 * 2 * 5 + 10 * 3 * 4 operation: *
ans = 100 - 9 * 4 * 4 - 8 * 2 * 2 * 5 + 10 * 3 * 4 operation: *
ans = 100 - 36 * 4 - 8 * 2 * 2 * 5 + 10 * 3 * 4 operation: *
ans = 100 - 144 - 8 * 2 * 2 * 5 + 10 * 3 * 4 operation: *
ans = 100 - 144 - 16 * 2 * 5 + 10 * 3 * 4 operation: *
ans = 100 - 144 - 32 * 5 + 10 * 3 * 4 operation: *
ans = 100 - 144 - 160 + 10 * 3 * 4 operation: *
ans = 100 - 144 - 160 + 30 * 4 operation: *
ans = 100 - 144 - 160 + 120 operation: -
ans = -44 - 160 + 120 operation: -
ans = -204 + 120 operation: +
ans = -84
```

(b) res = 4 * a * y / c – a * y / c ;

(a = 4, y = 1, c = 3, assume res to be an int)

**Answer:**

```
res = 4 * 4 * 1 / 3 - 4 * 1 / 3 operation: *
res = 16 * 1 / 3 - 4 * 1 / 3 operation: *
res = 16 / 3 - 4 * 1 / 3 operation: /
res = 5 - 4 * 1 / 3 operation: *
res = 5 - 4 / 3 operation: /
res = 5 - 1 operation: -
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)

**Answer:**

```
s = 4.1 + 2.2 * 3.0 * 3.0 / 0.0 operation: *
s = 4.1 + 6.6 * 3.0 / 0.0 operation: *
s = 4.1 + 19.8 / 0.0 operation: /
Here we 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)

**Answer:**

```
R = 3.5 * 3.5 + 2 * 3.5 + 1 / 2 * 3.5 * 3.5 + 3.5 + 1 operation: *
R = 12.25 + 2 * 3.5 + 1 / 2 * 3.5 * 3.5 + 3.5 + 1 operation: *
R = 12.25 + 7.0 + 1 / 2 * 3.5 * 3.5 + 3.5 + 1 operation: *
R = 12.25 + 7.0 + 1 / 7.0 * 3.5 + 3.5 + 1 operation: *
R = 12.25 + 7.0 + 1 / 24.5 + 3.5 + 1 operation: /
R = 12.25 + 7.0 + 0.04081 + 3.5 + 1 operation: +
R = 19.25 + 0.04081 + 3.5 + 1 operation: +
R = 19.29081 + 3.5 + 1 operation: +
R = 22.79081 + 1 operation: +
S = 23.79081
```

#### [C] Indicate the order in which the following expressions would be evaluated.

(a) g = 10 / 5 / 2 / 1 ;

Evaluation order would be:

```
g = 10 / 5 / 2 / 1 ; operation: /
g = 2 / 2 / 1 ; operation: /
g = 1 / 1 ; operation: /
g = 1 ; operation: =
```

(b) b = 3 / 2 + 5 * 4 / 3 ;

Evaluation order would be:

```
Evaluation order would be:
b = 3 / 2 + 5 * 4 / 3 operation: /
b = 1 + 5 * 4 / 3 operation: *
b = 1 + 20 / 3 operation: /
b = 1 + 6 operation: +
b = 7 operation: =
```

(c) a = b = c = 3 + 4 ;

Evaluation order would be:

```
b = 3 / 2 + 5 * 4 / 3 operation: /
b = 1 + 5 * 4 / 3 operation: *
b = 1 + 20 / 3 operation: /
b = 1 + 6 operation: +
b = 7 operation: =
```

(d) x = 2 – 3 + 5 * 2 / 8 % 3 ;

Evaluation order would be:

```
x = 2 - 3 + 5 * 2 / 8 % 3 operation: *
x = 2 - 3 + 10 / 8 % 3 operation: /
x = 2 - 3 + 1 % 3 operation: %
x = 2 - 3 + 1 operation: -
x = -1 + 1 operation: +
x = 0 operation: =
```

(e) z = 5 % 3 / 8 * 3 + 4 ;

Evaluation order would be:

```
z = 5 % 3 / 8 * 3 + 4 operation: %
z = 2 / 8 * 3 + 4 operation: /
z = 0 * 3 + 4 operation: *
z = 0 + 4 operation: +
z = 4 operation: =
```

(f) y = z = -3 % -8 / 2 + 7 ;

Evaluation order would be:

```
y = z = -3 % -8 / 2 + 7 operation: -
y = z = -3 % -8 / 2 + 7 operation: -
y = z = -3 % -8 / 2 + 7 operation: %
y = z = -3 / 2 + 7 operation: /
y = z = -1 + 7 operation: +
y = z = 6 operation: =
y = 6 operation: =
```

#### [D] Convert the following algebraic expressions into equivalent C statements.

**Answer:**

`Z = ( ( x + 3 ) * x * x * x ) / ( ( y - 4 ) * ( y + 5 ) )`

`R = ( 2 * v + 6.22 * ( c + d ) ) / ( g + v )`

`A = ( ( 7.7 * b ) * ( x * y + a ) / c – 0.8 + 2 * b ) / ( ( x + a ) * ( 1 / y ) )`

`X = ( 12 * x * x * x / 4 * x ) + ( 8 * x * x / 4 * x ) + ( x / 8 * x ) + ( 8 / 8 * x )`

#### [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.000000 2.000000`

(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:**

`Error. Mod ( % ) operator cannot be used on floats`

(d)

```
# include <stdio.h>
int main( )
{
printf ( "nn \n\n nn\n" ) ;
printf ( "nn /n/n nn/n" ) ;
return 0 ;
}
```

**Answer:**

```
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:**

```
Since spaces are given after and before double quotes in
scanf() we must supply a space, then two numbers and again
a space followed by enter. The printf( ) would then output the
two number that you enter.
```

#### [F] State whether the following statements are True or False:

(a) * or /, + or – represents the correct hierarchy of arithmetic

operators in C.

**Answer:** True

(b) [ ] and { } can be used in Arithmetic instructions.

**Answer:** False

(c) Hierarchy decides which operator is used first.

**Answer:** True

(d) In C, Arithmetic instruction cannot contain constants on left

side of =

**Answer:** True

(e) In C ** operator is used for exponentiation operation.

**Answer**: False

(f) % operator cannot be used on floats.

**Answer:** True

#### [G] Fill in the blanks:

(a) In y = 10 * x / 2 + z ; 10 * x operation will be performed first.

(b) If a is an integer variable, a = 11 / 2 ; will store 5 in a.

(c) The expression, a = 22 / 7 * 5 / 3 ; would evaluate to 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.285714

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 ‘%’)

**Program:**

```
# include <stdio.h>
int main( )
{
int num, a, n ;
int sum = 0 ; /* initialise to zero, otherwise it will contain a
garbage value*/
printf ( "\nEnter a 5 digit number(less than 32767): " ) ;
scanf ( "%d", &num ) ;
a = num % 10 ; /* last digit extracted as remainder */
n = num /10 ; /* remaining digits */
sum = sum + a ; /* sum updated with addition of extracted digit */
a = n % 10 ; /* 4 th digit */
n = n /10 ;
sum = sum + a ;
a = n % 10 ; /* 3 rd digit */
n = n /10 ;
sum = sum + a ;
a = n % 10 ; /* 2 nd digit */
n = n /10 ;
sum = sum + a ;
a = n % 10 ; /* 1 st digit */
sum = sum + a ;
printf ( "The sum of the 5 digits of %d is %d\n", num, sum ) ;
return 0 ;
}
```

(b) If a five-digit number is input through the keyboard, write a

program to reverse the number

**Program:**

```
# include <stdio.h>
int main( )
{
int n, a, b ;
long int revnum = 0 ;
printf ( "\nEnter a five digit number (less than 32767): " ) ;
scanf ( "%d", &n ) ;
a = n % 10 ; /* last digit */
n = n / 10 ; /* remaining digits */
revnum = revnum + a * 10000L ; /* revnum updated with
value of extracted digit */
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 ;
/* specifier %ld is used for printing a long integer */
printf ( "The reversed number is %ld\n", revnum ) ;
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.

**Program:**

```
#include<stdio.h>
#include<math.h>
int main( )
{
float a, b, c, sp, area ;
printf ( "\nEnter sides of a triangle: " ) ;
scanf ( "%f %f %f", &a, &b, &c ) ;
sp = ( a + b + c ) / 2 ;
area = sqrt ( sp * ( sp - a ) * ( sp - b ) * ( sp - c ) ) ;
printf ( "Area of triangle = %f\n", area ) ;
return 0 ;
}
```

(d) Write a program to receive Cartesian co-ordinates (x, y) of a

point and convert them into polar co-ordinates

**Program:**

```
# include <stdio.h>
# include <math.h>
int main( )
{
float x, y, r, theta ;
printf ( "\nEnter x and y co-ordinates: " ) ;
scanf ( "%f %f", &x, &y ) ;
r = sqrt ( x * x + y * y ) ;
theta = atan2 ( y, x ) ;
theta = theta * 180 / 3.14 ; /* convert to degrees */
printf ( "r = %f theta = %f\n", r, theta ) ;
return 0 ;
}
```

(e) Write a program to receive values of latitude (L1, L2) and

longitude (G1, G2), in degrees, of two places on the earth and

outputs the distance between them in nautical miles. The

formula for distance in nautical miles is:

D = 3963 acos ( sin L1 sin L2 + cos L1cos L2 * cos ( G2 – G1 )

**Program:**

```
# include <stdio.h>
# include <math.h>
int main( )
{
float lat1, lat2, lon1, lon2, d ;
printf ( "\nEnter Latitude and Longitude of Place 1: " ) ;
scanf ( "%f %f", &lat1, &lon1 ) ;
printf ( "Enter Latitude and Longitude of Place 2: " ) ;
scanf ( "%f %f", &lat2, &lon2 ) ;
lat1 = lat1 * 3.14 / 180 ;
lat2 = lat2 * 3.14 / 180 ;
lon1 = lon1 * 3.14 / 180 ;
lon2 = lon2 * 3.14 / 180 ;
d = 3963 * acos ( sin ( lat1 ) * sin ( lat2 ) + cos ( lat1 ) * cos ( lat2 )
* cos ( lon2 - lon1 ) ) ;
printf ( "Distance between Place1 and Place 2: %f\n", d ) ;
return 0 ;
}
```

(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 ) * v0.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.

**Program:**

```
# include <stdio.h>
# include <math.h>
int main( )
{
float temp, vel, wcf ;
printf ( "\nEnter values of temp and velocity: " ) ;
scanf ( "%f %f", &temp, &vel ) ;
wcf = 35.74 + 0.6215 * temp + ( 0.4275 * t - 35.75 )
* pow ( vel, 0.16f ) ;
printf ( "Wind Chill Factor = %f\n", wcf ) ;
return 0 ;
}
```

(g) If value of an angle is input through the keyboard, write a

program to print all its Trigonometric ratios.

**Program:**

```
# include <stdio.h>
# include <math.h>
int main( )
{
float angle, s, c, t ;
printf ( "\nEnter an angle: " ) ;
scanf ( "%f", &angle ) ;
/* convert angle to radians */
angle = angle * 3.14 / 180 ;
s = sin ( angle ) ;
c = cos ( angle ) ;
t = tan ( angle ) ;
printf ( "sin = %f cos = %f tan = %f\n", s, c, t ) ;
return 0 ;
}
```

(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.

**Program:**

```
# include <stdio.h>
int main( )
{
int c, d, e ;
printf ( "\nEnter the number at location C: " ) ;
scanf ( "%d", &c ) ;
printf ( "\nEnter the number at location D: " ) ;
scanf ( "%d", &d ) ;
/* Interchange contents of two variables using a third variable as
temporary store */
e = c ;
c = d ;
d = e ;
printf ( "New Number at location C = %d\n", c ) ;
printf ( "New Number at location D = %d\n", d ) ;
return 0 ;
}
```

(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 compute the smallest number of notes that will combine to give Rs. N.

**Program:**

```
int main( )
{
int amount, nohun, nofifty, noten, nofive, notwo, noone, totalnotes ;
printf ( "Enter the amount: " ) ;
scanf ( "%d", &amount ) ;
nohun = amount / 100 ;
amount = amount % 100 ;
nofifty = amount / 50 ;
amount = amount % 50 ;
noten = amount / 10 ;
amount = amount % 10 ;
nofive = amount / 5 ;
amount = amount % 5 ;
notwo = amount / 2 ;
amount = amount % 2 ;
noone = amount / 1 ;
amount = amount % 1 ;
totalnotes = nohun + nofifty + noten + nofive + notwo + noone ;
printf ( "Smallest number of notes = %d\n", totalnotes ) ;
return 0 ;
}
```

These were the solutions of Chapter 2: C Instructions of Let Us C Book by Yashvant Kanetkar.

Previous | Next |
---|---|

Chapter 1: Getting Started | Chapter 3: Decision Control Instruction |