chapter1_excercise
chapter1_excercise
Control Instructions in C
As the name suggests the ‘Control Instructions’ enable us to
specify the order in which the various instructions in a program are
to be executed by the computer. In other words the control
instructions determine the ‘flow of control’ in a program. There
are four types of control instructions in C. They are:
Summary
(a) The three primary constants and variable types in C are
integer, float and character.
(b) A variable name can be of maximum 31 characters.
(c) Do not use a keyword as a variable name.
38 Let Us C
Exercise
[A] Which of the following are invalid variable names and why?
(e) k = ( a * b ) ( c + ( 2.5a + b ) ( d + e ) ;
(j) k = ( (a * b ) + c ) ( 2.5 * a + b ) ;
(k) a = b = 3 = 4 ;
(d) s = 1 / 3 * a / 4 - 6 / 2 + 2 / 3 * 6 / g ;
(a = 4, g = 3, assume s to be an int)
[D] Fill the following table for the expressions given below and
then evaluate the result. A sample entry has been filled in the
table for expression (a).
40 Let Us C
/ 10 5 or 5 / 2 Left operand is
/1 unambiguous, Right
is not
.. .. .. ..
(a) g = 10 / 5 /2 / 1 ;
(b) b = 3 / 2 + 5 * 4 / 3 ;
(c) a = b = c = 3 + 4 ;
8.8 ( a + b ) 2 / c - 0.5 + 2 a / ( q + r )
(a) Z=
( a + b ) * (1 / m )
- b + ( b * b ) + 2 4ac
(b) X=
2a
2v + 6.22 ( c + d )
(c) R=
g+v
7.7b ( xy + a ) / c - 0.8 + 2b
(d) A=
( x + a ) (1 / y )
(a) main( )
{
Chapter 1: Getting Started 41
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", k, l, a, b ) ;
}
(b) main( )
{
int a, b ;
a = -3 - - 3 ;
b = -3 - - ( - 3 ) ;
printf ( "a = %d b = %d", a, b ) ;
}
(c) main( )
{
float a = 5, b = 2 ;
int c ;
c=a%b;
printf ( "%d", c ) ;
}
(d) main( )
{
printf ( "nn \n\n nn\n" ) ;
printf ( "nn /n/n nn/n" ) ;
}
(e) main( )
{
int a, b ;
printf ( "Enter values of a and b" ) ;
scanf ( " %d %d ", &a, &b ) ;
printf ( "a = %d b = %d", a, b ) ;
}
42 Let Us C
(f) main( )
{
int p, q ;
printf ( "Enter values of p and q" ) ;
scanf ( " %d %d ", p, q ) ;
printf ( "p = %d q =%d", p, q ) ;
}
(2) Hyphen
(3) Decimal point
(4) All the above
(e) The length & breadth of a rectangle and radius of a circle are
input through the keyboard. Write a program to calculate the
area & perimeter of the rectangle, and the area &
circumference of the circle.
(f) Two numbers are input through the keyboard into two
locations C and D. Write a program to interchange the
contents of C and D.
(l) If the total selling price of 15 items and the total profit earned
on them is input through the keyboard, write a program to
find the cost price of one item.