Let Us C Solutions:Chapter 1

Let Us C Solutions:Chapter 1 

Exercise 

 [A] Which of the following are invalid variable names and why? 

 BASICSALARY 
Ans. It is a valid variable . 
 _basic 
Ans. It is a valid variable . 

basic-hra 
Ans. It is a invalid variable because ‘-’ is used (only alphabet, hyphen and numbers can be used in a variable name). 

#MEAN
Ans. It is invalid variable name since variable name cannot contain (#). 

group. 
Ans. It is invalid variable name since variable name cannot contain (.) 

422 
Ans. It is invalid variable name because variable name cannot Start from number. 

Population in 2006 
Ans. It is invalid variable name because space cannot be used in a variable name.

 over time 
Ans. It is invalid 

 mindovermatter 
 Ans. It is a valid variable name.

 FLOAT 
Ans. It is a valid variable name. 

 hELLO 
Ans. It is a valid variable name. 

queue. 
 Ans. It is a invalid variable name. 

Team’svictory 
 Ans. It is a invalid variable name as (‘) cannot be used in a variable name. 

Plot # 3 
 Ans. It is a invalid variable name.

 2015_DDay 
 Ans. It is a invalid variable name since it cannot be start from number. 

[B] Point out the errors, if any, in the following C statements:


 (a) int = 314.562 * 150 ;
 Ans. int is keyword defined to declare integer value but here float value (314.562) is given.

 (b) name = ‘Ajay’ ; 
 Ans. Here name is a character variable but a character variable must be a single letter or a single digit or a single special symbol. Therefore it is totally wrong declaration. And also both inverter commas are pointed towards left. Example: ’A’. 

(c) varchar = ‘3’ ; 
Ans. Both commas should be pointed towards left.

(d) 3.14 * r * r * h = vol_of_cyl ; 
Ans. Variable should always be on left side.

(e) k = ( a * b ) ( c + ( 2.5a + b ) ( d + e ) ; 
Ans. Correct expression is 
k = ( a * b ) *( c + ( 2.5a + b ) *( d + e ) ; 

 (f) m_inst = rate of interest * amount in rs ; 
Ans. Error variable name (rate of interest ) because space is used in between variable name.

(g) si = principal * rateofinterest * numberofyears / 100 ; 
Ans. No error.

(h) area = 3.14 * r ** 2 ; 
Ans. (**) doesn't have any meaning in C .

(i) Volume = 3.14 * r ^ 2 * h ;
Ans. r^2 is not define in C.

 (j) k = ( (a * b ) + c ) ( 2.5 * a + b ) ; 
Ans. Correct expression is k = ( (a * b ) + c )* ( 2.5 * a + b ) ; 

 (k) a = b = 3 = 4 ; 
Ans. Wrong expression.

 (l) count = count + 1 ; 
Ans. No error.

 (m) date = ‘2 Mar 04’ ; 
Ans. Same error as in (b).

 [C] Evaluate the following expressions and show their hierarchy. 


 (a) G = big / 2 + big * 4 / big – big + abc / 3 ; (abc = 2.5, big = 2, assume g to be a float)
Ans. G = 2/2 +2*4/2 - 2 + 2.5/3    (putting values of big and abc)
G = 1+ 2*4/2 - 2 + 2.5/3          operation: /
G = 1 + 2*2 - 2 + 2.5/3            operation: /
G = 1 + 4 - 2 + 2.5/3                operation: *
G = 1 + 4 - 2 + 0.8                   operation: /
G = 5.8 - 2                                operation: +
G = 3.8                                     operation: -

 (b) On = ink * act / 2 + 3 / 2 * act + 2 + tig ; (ink = 4, act = 1, tig = 3.2, assume on to be an int) 
Ans. On =4*1/2 + 3/2*1 + 2 + 3.2     (putting values of big and abc)
On = 4/2 + 3/2*1 + 2 + 3.2          operation: *
On = 2 + 3/2*1 + 2 + 3.2             operation: /
(here 3/2 =1 only integer value of 1.5 is taken because integer division gives integer value)
On = 2 + 1*1 + 2 + 3.2                operation: /
On = 2 + 1 + 2 + 3.2                    operation: *
On = 8.2                                       operation: +
On = 8  (since On is an int)

(c) s = qui
 * add / 4 – 6 / 2 + 2 / 3 * 6 / god ; (qui = 4, add = 2, god = 2, assume s to be an int) S = 1 / 3 * a / 4 – 6 / 2 + 2 / 3 * 6 / g ; (a = 4, g = 3, assume s to be an int)
Ans. s = 4*2/4 - 6/2 + 2/3*6/2   (putting values of big and abc)
s = 8/4 - 6/2 + 2/3*6/2                 operation: *
s = 2 - 6/2 + 2/3*6/2                    operation: /
s = 2 - 3 + 2/3*6/2                       operation: /
s = 2 - 3 + 0*6/2                          operation: /
s = 2-3                                          operation: *
s = -1                                            operation: -

(d) s = 1 / 3 * a / 4 - 6 / 2 + 2 / 3 * 6 / g ;
(a = 4, g = 3, assume s to be an int)
Ans. S=1/3*4/4-6/2+2/3*6/3         (putting values of big and abc)
S = 0*4/4 - 6/2 + 2/3*6/3              operation: /
S = 0 - 6/2 + 2/3*6/3                     operation: *
S = -3 + 2/3*6/3                             operation: /
S = -3 + 0*6/3                                operation: /
S = -3                                             operation: *

 [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 expressions
operator left right remarks
/ 10 5 or 5 / 2
/ 1
Left operand is
unambiguous, Right
is not
... ... ... ...
 


(a). (a) G = 10 / 5 /2 / 1 ; 

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

(c)a = b = c = 3 + 4 ;
S. no. operator left right remarks evaluation
a / 10 5 or 5/2/1 Left operand is
unambiguous, Right
is not
g=10/5/2/1
=2/2/1
=1/1
=1
b /





*
3 and 4





5
2 and 3





4/3
Both left and right
operands are
unambiguous

Both left and right
operands are
unambiguous
b=3/2+5*4/3
=1+5*4/3
=1+20/3
=1+6
=7
c + 3 4 Both left and right
operands are
unambiguous
a=b=c=3+4
=7

 

[E] Convert the following equations into corresponding C Statements

 (a) Z = (8.8 ( a b ) 2 / c - 0.5 2 a / ( q r ))/((a+b)*(1/m))

Solution. Z =(8.8*(a*b)*2/c-0.5*2*a/(q*r))/((a+b)*(1/m))

(b)  X = (-b + ( b * b ) + 2 4ac)/2a

Solution. Z = (-b + b*b +2*4*a*c)/2*a

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

Solution. (2*v + 6.22**c*d)/(g+v)

(d) A = (7.7b ( xy + a ) / c - 0.8 + 2b)/(x+a)(1/y)

Solution. A = (7.7*b*(x*y + a)/c - 0.8 + 2*b)/(x+a)(1/y)

[F] What would be the output of the following programs:


 (a) 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”, k, l, a, b);
}
Output: 0 0 0 0

(b) main()
{
    int a, b;
    a = -3 - -3;
    b = -3 - -(-3);
    printf( “a = % d b = % d”, a, b);
}
Output. Error as '--' is incorrect operator 

(c) main()
{
    float a = 5, b = 2;
    int c;
    c = a % b;
    printf( “% d”, c);
}
Output. 1

(d)  main()
{
    printf( “nn \n\n nn\n” );
    printf( “nn / n / n nn / n” );
}
Output.
nn

 nn
nn / n / n nn / n
[Program finished]

(e)  main()
{
    int a, b;
    printf( “Enter values of a and b” );
    scanf( “ % d % d “, &a, &b);
    printf( “a = % d b = % d”, a, b);
}
Output.
Enter the values of a and b 25 56
a = 25 b = 56

(f) main()
{
    int p, q;
    printf( “Enter values of p and q” );
    scanf( “ % d % d “, p, q);
    printf( “p = % d q = % d”, p, q);
}
Output.
Segment fault 
correct statement is scanf("%d %d", &p, &q)

 [G] Pick up the correct alternative for each of the following Questions: 


 (a) C language has been developed by
 (1) Ken Thompsom       (2) Dennis Ritchie 
 (3) Peter Norton            (4) Martin Richards 

 (b) C can be used on

(1) Only MS-DOS operating system       
(2) Only Linux operating system 
(3) Only Windows operating system 
(4) All the above

(c) C programs are converted into machine language with the Help of 

 (1) An Editor                     (2) A compiler
 (3) An operating system (4) None of the above 

(d) The real constant in C can be expressed in which of the Following forms 

 (1) Fractional form only    (2) Exponential form only (3) ASCII form only     (4) Both fractional and exponential forms

(e) A character variable can at a time store

 (1) 1 character             (2) 8 characters 
 (3) 254 characters          (5) None of the above 

(f) The statement char ch = ‘Z’ would store in ch
 (1) The character Z                                            (2) ASCII value of Z
 (3) Z along with the single inverted commas (4) Both (1) and (2) 

 (g) Which of the following is NOT a character constant 

 (1) ‘Thank You’.              (2) ‘Enter values of P, N, R’ 
 (3) ’23.56E-03’                 (4) All the above

 (h) The maximum value that an integer constant can have is

 (1) -32767                         (2) 32767
 (3) 1.7014e+38                 (4) –1.7014e+38

 (i) A C variable cannot start with

 (1) An alphabet                                               
 (2) A number 
 (3) A special symbol other than underscore 
 (4) Both (2) & (3) above

 (j) Which of the following statement is wrong

 (1) Mes = 123.56 ;        (2) Con = 'T’* 'A’ ; 
 (3) This = 'T' * 20 ;       (4) 3 + a = b ;

 (k) Which of the following shows the correct hierarchy of Arithmetic operators in C 

 (1) **, * or /, + or –          (2) **, *, /, +, - 
 (3) **, /, *, +, -                   (4) / or *, - or +
('**' has no meaning in C language)

 (l) In b = 6.6 / a + 2 * n ; which operation will be performed First?

 (1) 6.6 / a             (2) A + 2 
 (3) 2 * n                  (5) Depends upon compiler 
(as per hierarchy of arithmetic operator in C  and it comes first)

 (m) Which of the following is allowed in a C Arithmetic Instruction 

 (1) [ ]                       (2) { } 
 (3) ( )                    (5) None of the above 

 (n) Which of the following statements is false

 (1) Each new C instruction has to be written on a separate Line
 (2) Usually all C statements are entered in small case letters 
 (3) Blank spaces may be inserted between two words in a C Statement 
 (4) Blank spaces cannot be inserted within a variable name 

 (o) If a is an integer variable, a = 5 / 2 ; will return a value 

 (1) 2.5           (2) 3            (3) 2                (5) 0 
(as operation on integer give integer )

 (p) The expression, a = 7 / 22 * ( 3.14 + 2 ) * 3 / 5 ; evaluates to

 (1) 8.28        (2) 6.28        (3) 3.14           (5) 0
(approximately 0.986)

 (q) The expression, a = 30 * 1000 + 2768 ; evaluates to
 (1) 32768       (2) -32768       (3) 113040        (4) 0 

(r) The expression x = 4 + 2 % - 8 evaluates to
 (1) -6          (2) 6        (3) 4         (4) None of the above
(note that on using % the sign of the remainder is always same as the sign of the numerator)
(s) Hierarchy decides which operator 

 (1) Is most important        (2) Is used first
 (3) Is fastest                         (4) Operates on largest numbers 

 (t) An integer constant in C must have:

 (1) At least one digit                               
 (2) Atleast one decimal point 
 (3) A comma along with digits 
 (4) Digits separated by commas 

 (u) A character variable can never store more than 

 (1) 32 characters                 (2) 8 characters 
 (3) 254 characters               (4) 1 character

 (v) In C a variable cannot contain 

 (1) Blank spaces                     (2) Hyphen                   (3) Decimal point                   (4) All the above

 (w) Which of the following is FALSE in C

 (1) Keywords can be used as variable names
 (2) Variable names can contain a digit
 (3) Variable names do not contain a blank space (5) Capital letters can be used in variable names 

 (x) In C, Arithmetic instruction cannot contain 

 (1) Variables                                          (2) Constants 
 (3) Variable names on right side of =       (4) Constants on left side of = 

 (y) Which of the following shows the correct hierarchy of Arithmetic operations in C
 (1) / + * -          (2) * - / +      (3) + - / *       (5) * / + -

 (z) What will be the value of d if d is a float after the operation d = 2 / 7.0? 

 (1) 0                                            (2) 0.2857
 (3) Cannot be determined.     (4) None of the above

 [H] Write C programs for the following: 

(a) Ramesh’s basic salary is input through the keyboard. His Dearness allowance is 40% of basic salary, and house rent Allowance is 20% of basic salary. Write a program to calculate His gross salary. 
Solution.
#include <stdio.h>
int main()
{
    int bs, da, hra, gs;
    printf("Enter the Ramesh's basic salary");
    scanf("%d", &bs);
    da = bs * 40 / 100;
    hra = bs * 20 / 100;
    gs = bs - da - hra;
    printf("Gross salary = %d", gs);
}

(b) The distance between two cities (in km.) is input through the Keyboard. Write a program to convert and print this distance In meters, feet, inches and centimeters. 
Solution.
#include <stdio.h>
int main()
{
    float d, in_cm, in_m, in_in, in_ft;
    printf("Enter the distance in km =");
    scanf("%f", &d);
    in_m = d * 1000;
    in_cm = d * 1000 * 100;
    in_in = d * 1000 * 100 / 2.54;
    in_ft = in_in / 12;
    printf("distance \nin meter = %f\nin cm=%f\nin inch =%f\nin feet=%f ", in_m, in_cm, in_in, in_ft);
}
 (c) If the marks obtained by a student in five different subjects Are input through the keyboard, find out the aggregate marks And percentage marks obtained by the student. Assume that The maximum marks that can be obtained by a student in each Subject is 100. 
Solution.
#include <stdio.h>
int main()
{
    float a, b, c, d, e, sum, percent;
    printf("Enter the marks obtained in 5 subjects:\n");
    scanf("%f%f%f%f%f", &a, &b, &c, &d, &e);
    sum = a + b + c + d + e;
    percent = sum / 5;
    printf("percetage =%f", percent);
}

 (d) Temperature of a city in Fahrenheit degrees is input through The keyboard. Write a program to convert this temperature Into Centigrade degrees. 
Solution.
#include <stdio.h>
int main()
{
    float F, C;
    printf("Enter the temperature of the city in fahrenheit=");
    scanf("%f", &F);
    C = 5 * (F - 32) / 9;
    printf("temperature in centigrade =%f", C);
}

(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. 
Solution.
#include <stdio.h>
int main()
{
    float l, b, r, rect, cir;
    printf("Enter the length, breadth and radius respectively:");
    scanf("%f%f%f", &l, &b, &r);
    rect=l*b;
    printf("area of rectangle = %f\n", rect);
    cir=3.14*r*r;
    printf("area of circle = %f", cir);
}

 (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.
Solution.
#include <stdio.h>
int main()
{
    int a, b, c, d;
    printf("enter a=");
    scanf("%d", &a);
    printf("enter b=");
    scanf("%d", &b);
    c = a;
    d = b;
    printf("a=%d\nb=%d", d, c);
}

(g) 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 ‘%’)
Solution. 
#include <stdio.h>
int main()
{
    int a, b, c, d, e, f, g, h, i, j, sum;
    printf("Enter a five digit number:");
    scanf("%d", &a); // let you entered a (say12345)
    b = a % 10; // this line gives past digit i.e., 5
    c = a / 10; // this gives 1234 

    d = c % 10;
    e = c / 10;

    f = e % 10;
    g = e / 10;

    h = g % 10;
    i = g / 10;

    j = i% 10;

    sum = b + d + f + h + j;
    printf("sum of digits =%d", sum);
}

 (h) If a five-digit number is input through the keyboard, write a Program to reverse the number. 
Solution.
#include <stdio.h>
int main()
{
    int a, b, c, d, e, f, g, h, i, j, k;
    printf("Enter a five digit number: ");
    scanf("%d", &a);
    b=a%10*10000;
    c=a/10;
    
    d=c%10*1000;
    e=c/10;
    
    f=e%10*100;
    g=e/10;
    
    h=g%10*10;
    i=g/10;
    
    j=i%10;
    
    k=b+d+f+h+j;
    printf("reverse of the entered number is = %d", k);
}
    

 (i) If a four-digit number is input through the keyboard, write a Program to obtain the sum of the first and last digit of this Number. 
Solution.
#include <stdio.h>
int main()
{
    int a, b, f, l, sum;
    printf("Enter the four digit number = ");
    scanf("%d", &a);
    l = a % 10;
    b = a / 1000;

    f = b % 10;
    sum = f + l;
    printf("Sum of first and last digit is %d", sum);
}


 (j) In a town, the percentage of men is 52. The percentage of Total literacy is 48. If total percentage of literate men is 35 of The total population, write a program to find the total number of illiterate men and women if the population of the town is 80,000. 
Solution.
#include <stdio.h>
int main()
{
    float m, w, ml, wl, m_il, w_il, tp = 80000, lit;
    m = 52 * tp / 100;
    w = 48 * tp / 100;
    ml = 35 * m/100;
    m_il = m - ml;
    lit = 48 * tp / 100;
    wl = lit - ml;
    w_il = w - wl;
    printf("illitrate man =%f\nillitrate women =%f", m_il, w_il);
}

 (k) A cashier has currency notes of denominations 10, 50 and 100. If the amount to be withdrawn is input through the Keyboard in hundreds, find the total number of currency notes Of each denomination the cashier will have to give to the Withdrawer. 
Solution.
#include <stdio.h>
int main()
{
    int note10, note50, note100, wdrawn_money, a, b;
    printf("enter the amount of withdrawn money: ");
    scanf("%d", &wdrawn_money);
    
    note100 = wdrawn_money / 100;
    a = wdrawn_money % 100;

    note50 = a / 50;
    b = a % 50;

    note10 = b / 10;

    printf("notes of 10=%d\nnotes of 50=%d\nnotes of 100=%d", note10, note50, note100);
}

 (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. 
Solution. 
#include <stdio.h>
int main()
{
int sp, profit, cp;
printf("Enter the selling price and profit earned:");
scanf("%d%d", &sp, &profit);
cp=sp+profit;
printf("Cost price of 15 items is =%d", cp);
}

 (m) If a five-digit number is input through the keyboard, write a Program to print a new number by adding one to each of its Digits. For example if the number that is input is 12391 then The output should be displayed as 23402.
Solution.
#include <stdio.h>
int main()
{
    int a, b, c, d, e, f, g, h, i, j, v, w, x, y, z, sum;
    printf("Enter a five digit number: ");
    scanf("%d", &a);
    b = a % 10;
    v = (b + 1);
    c = a / 10;

    d = c % 10;
    w = (d + 1) * 10;
    e = c / 10;

    f = e % 10;
    x = (f + 1) * 100;
    g = e / 10;

    h = g % 10;
    y = (h + 1) * 1000;
    i = g / 10;

    j = i % 10;
    z = (j + 1) * 10000;

    sum = z + y + x + w + v;
    printf("new number is = %d", sum);
}

Comments