Breaking News

LET US C SOLUTIONS EDITION 5 (CHAPTER 1) SOLUTIONS

LET US C SOLUTIONS EDITION 5 (CHAPTER 1)

LET US C SOLUTIONS EDITION 5
(YASHWANT KANETKAR)
CHAPTER 1: Getting Started

Exercise:

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

      BASICSALARY                    _basic                    basic-hra
#MEAN                                  group.                    422
population in 2006              over time               mindovermatter
FLOAT                                  hELLO                   queue.
team’svictory                        Plot # 3                  2015_DDay
Answer: 
BASICSALARY      : valid (as it follows all the variable declaration rules)
_basic                        : valid
basic-hra                   : invalid (no special symbol other than underscore can be used)
#MEAN                     : invalid (no special symbol other than underscore can be used)
group.                        : Invalid (no special symbol other than underscore can be used)
422                             : invalid (variable name should begin with alphabet)
population in 2006   : invalid (space is not allowed in variable names)
over time                   : invalid (space is not allowed in variable names)
mindovermatter      : valid (as it follows all the variable declaration rules)
FLOAT                    : valid (because float and FLOAT are different)
hELLO                    : valid (as it follows all the variable declaration rules)
queue.                    : Invalid ( “.” Can’t be used!! no special symbol other than underscore can be used)
team’svictory        : invalid ( “ ’ ” Can’t be used!! No special symbol other than underscore can be used)
Plot # 3                : invalid ( “ # ” Can’t be used!! No special symbol other than underscore can be used)
2015_DDay            : valid (as it follows all the variable declaration rules)

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

 (a)   int = 314.562 * 150 ;
      Answer: int is a key word so should not be used as a variable.

(b)   name = ‘Ajay’ ;
Answer:  ‘Ajay’ is an invalid character constant.
Ø  A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Both the inverted commas should point to the left. For example, ’A’ is a valid character constant whereas ‘A’ is not.
Ø  The maximum length of a character constant can be 1character
(c)    varchar = ‘3’ ;
Answer: ‘3’ is invalid character constant because it uses inverted commas ‘ ’ on opposite side.
(d)   3.14 * r * r * h = vol_of_cyl ;
Answer: error on left hand side of = can only be a variable.
(e)    k = ( a * b ) ( c + ( 2.5a + b ) ( d + e ) ;
Answer: error (multiplication sign * is missing between 2.5 and a.)
(f)    m_inst = rate of interest * amount in rs ;
Answer: error (rate of interest and amount in rs are invalid variable name because space cannot be used in variable names.)
(g)   si = principal * rateofinterest * numberofyears / 100 ;
Answer: No error
(h)   area = 3.14 * r ** 2 ;
Answer: error (** does not represent any arithmetic operator in c.)
(i)     volume = 3.14 * r ^ 2 * h ;
Answer: error (^ sign cannot be used in c as power sign. Instead use pow(r,2) to rise 2 to r i.e. r^2)
(j)     k = ( (a * b ) + c ) ( 2.5 * a + b ) ;
Answer:  error (multiplication operator * between the two outer parenthesis)
(k)   a = b = 3 = 4 ;
Answer: invalid (because a and b can’t be assigned to 3 and 4 simultaneously in a line.)
(l)     count = count + 1 ;
Answer: valid (count will be incremented by 1).
(m) date = '2 Mar 04' ;
Answer: invalid ( spaces cannot be used and in character constant more than one word cannot be used)

[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)
Answer:
Step 1: g= 2/2 + 2*4 / 2-2 + 2.5/3;
According to arithmetic operator priority * and / have highest priority and then + and – so first / operator.
Step 2: g= 1 + 2*4/ 2 – 2 + 2.5/3;  
Division and multiplication operator have equal priority but by using left associativity multiplication is used (left to right associativity is preferred by * and /).
Step 3: g=1 + 8/2 – 2 + 2.5/3;
Step 4: g=1 + 4 – 2 + 2.5/3;
Step 5: g=1 + 4 – 2 + 0.8;
Step 6: g=5 – 2 + 0.8;
Step 7: g=3 + 0.8;
Step 8: g=3.8;
(b)   on = ink * act / 2 + 3 / 2 * act + 2 + tig ;
(ink = 4, act = 1, tig = 3.2, assume on to be an int)
Answer:
Step 1: on=4*1/2+3/2*1+2+3.2;
Step 2: on=4/2+3/2*1+2+3; (as on is assumed as integer type decimal is not discarded)
Step 3: on= 2+3/2*1+2+3;
Step 4: on= 2+1*1+2+3;
Step 5: on= 2+1+2+3;
Step 6: on= 3+2+3;
Step 7: on= 5+3;
Step 4: on= 8;
(c)    s = qui * add / 4 - 6 / 2 + 2 / 3 * 6 / god ;
(qui = 4, add = 2, god = 2, assume s to be an int)
Answer:
Step 1: s=4*2/4-6/2+2/3*6/2;
Step 2: s=8/4-6/2+2/3*6/2;
Step 3: s=2-6/2+2/3*6/2;
Step 4: s=2-3+2/3*6/2;
Step 5: s=2-3+0*6/2; (as s is assumed as an integer type so decimal value is discarded)
Step 6: s=2-3+0/2; (left to right associativity law)
Step 7: s=2-3 +0;
Step 8: s= -1 +0;
Step 8: s= -1;
(d)   s = 1 / 3 * a / 4 - 6 / 2 + 2 / 3 * 6 / g ;
(a = 4, g = 3, assume s to be an int)
Answer:
Step 1: s= 1/3*4/4-6/2+2/3*6/3;
Step 2: s=0*4/4-6/2+2/3*6/3; (s is an integer type and left to right associativity law)
Step 3: s=0/4-6/2+2/3*6/3;
Step 4: s=0-6/2+2/3*6/3;
Step 5: s=0-3+2/3*6/3;
Step 6: s=0-3+0*6/3;
Step 7: s=0-3+0/3;
Step 8: s=0-3+0;
Step 9: s=-3+0;
Step 10: s=-3;

[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 2 0.0 2.0
(b) main ( )
 {
int a, b ;
a = -3 - - 3 ;
b = -3 - - ( - 3 ) ;
printf ( "a = %d b = %d", a, b ) ;
}
Output: 0 -6
(c) main ( )
        {
float a = 5, b = 2 ;
int c ;
c = a % b ;
printf ( "%d", c ) ;
}

output: error (as invalid operands to binary %)

(d) main ( )
         {
printf ( "nn \n\n nn\n" ) ;
printf ( "nn /n/n nn/n" ) ;
         }
Output: nn
                nn
            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 ) ;
}
Output: enter the values of a and b
            (Assume that a entered 2 and 3)
            a=2
            b=3

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

(a) C language has been developed by
(1) Ken Thompson
(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
(4) 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 +
(l) In b = 6.6 / a + 2 * n ; which operation will be performed first?
(1) 6.6 / a
(2) a + 2
(3) 2 * n
(4) Depends upon compiler
(m) Which of the following is allowed in a C Arithmetic instruction
(1) [ ]
(2) { }
(3) ( )
(4) 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
(4) 0
(p) The expression, a = 7 / 22 * ( 3.14 + 2 ) * 3 / 5 ; evaluates to
(1) 8.28
(2) 6.28
(3) 3.14
(4) 0
(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
(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
(4) 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) + - / *
(4) * / + -
(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.
Answer: 


 #include<stdio.h>
#include<conio.h>
main( )
{
            float basic_sal,dear_al,house_rent_al,gross_sal;
            printf("enter the basic salary of ramesh\n");
            scanf("%f",&basic_sal);
            dear_al=0.4*basic_sal;
            house_rent_al=0.2*basic_sal;
            gross_sal= basic_sal*dear_al*house_rent_al;
            printf("dear allowence=%f\n",dear_al);
            printf("house rent allowence=%f\n",house_rent_al);
            printf("gross salary=%f\n",gross_sal);
            getchar();
           
}

 




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

#include <STDIO.H>
#include <conio.h>
main( )
{
            float km,meter,feet,inch,cm;
            printf("enter the distance between the two cities in kilometeres: ");
            scanf("%f",&amp;km);
            meter=1000*km;
            inch=39.3700787*meter;
            feet=3.2808399*meter;
            cm=100*meter;
            printf("distance between the two cities in meter=%f\n",meter);
            printf("distance between the two cities in inch=%f\n",inch);
            printf("distance between the two cities in feet=%f\n",feet);
            printf("distance between the two cities in centi-meter=%f\n",cm);
            getchar();                   
}


(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 are 100.
Answer:

#include<stdio.h>
#include<conio.h>
main( )
{
            int sub1,sub2,sub3,sub4,sub5,aggregate;
            float percentage;
            printf("the marks obtained in each subject:\n");
            scanf("%d%d%d%d%d",&amp;sub1,&amp;sub2,&amp;sub3,&amp;sub4,&amp;sub5);
            aggregate=sub1+sub2+sub3+sub4+sub5;
            percentage=aggregate/5;
            printf("aggregate=%d\n",aggregate);
            printf("percentage=%f",percentage);
}


(d)   Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to convert this temperature into Centigrade degrees.
Answer: 

#include<stdio.h>

main( )

{

            float fahrenheit,degree_celsius;

            printf("enter the temperature in fahrenheit: ");

            scanf("%f",&amp;fahrenheit);

            degree_celsius=((fahrenheit-32)*5)/9;

            printf("fehrenheit to celsius=%f\n",degree_celsius);

}

fahrenheit=(degree_celsius*9)/5+32;

 

   

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

#include<stdio.h>
main( )
{
        float length,breadth,radius,area_of_rectangale,area_of_circle,perimeter,circumference;
            printf("enter the length of a rectangle: ");
            scanf("%f",&amp;length);
            printf("enter the breadth of a rectangle: ");
            scanf("%f",&amp;breadth);
            area_of_rectangale=length*breadth;
            perimeter=2*length+2*breadth;
            printf("area of the rectangle=%f\n",area_of_rectangale);
            printf("perimeter of the rectangle=%f\n",perimeter);
            getchar();
            getchar();
            printf("enter the radius of the circle: ");
            scanf("%f",&amp;radius);
            area_of_circle=3.14*radius*radius;
            circumference=3.14*(2*radius);
          
            printf("area of the circle=%f\n",area_of_circle);
            printf("circumference of the circle=%f\n",circumference);
          
            printf("\n\n\n\n\n\n\n\n\npress any key to exit");
            getchar();       
}

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

         
#include<stdio.h>
#include<conio.h>
main( )
{
            int a,b,c;
            printf("enter the value of a and b\n");
            scanf("%d%d",&amp;a,&amp;b);
            c=a;
            a=b;
            b=c;
            printf("a changed to %d\n",a);
            printf("b changed to %d",b);
}

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

#include<stdio.h>
#include<conio.h>
main()
{
            int num,sum,n,a;
            sum=0;
            printf("enter the five digit number\n");
            scanf("%d",&amp;num);
            a=num%10;/*by using modular division we remove last digit of the number entered*/
            sum=sum+a;/*this gives the sum of the last number extracted i.e remainder of the number and intioal sum value which is intiated to zero for our convinience*/
            n=num/10;/*the n is nothing but the quetient of the division num/10 which contains only 4 digits of actual number*/
            a=n%10;/*now the new number is n as it has ectracted the last number in last calculation and last 4th digit is extracted*/
            sum=sum+a;/*sum of first two numbers*/
            n=n/10;/*the new number contains only 3 digits*/
            a=n%10;/* 3rd digit is extracted*/
            sum=sum+a;
            n=n/10;/*new number contains 2 digits*/
            a=n%10;/* 2nd digit is extracted*/
            sum=sum+a;
            n=n/10;/*new number contains 1 digits*/
            a=n%10;/* 2nd digit is extracted*/
            sum=sum+a;
            printf("the sum of the five digit number entered is=%d",sum);
            getchar();
                       
}


(h)    If a five-digit number is input through the keyboard, write a program to reverse the number
Answer: 


#include<stdio.h>

main()

{

    int num,rev,n,a;

   rev=0;

   printf("enter the five digit number\n");

   scanf("%d",&amp;num);

   a=num%10;/*by using modular division we remove last digit of the number entered*/

  rev=rev+a*10000;/*this gives the sum of the last number extracted i.e remainder of the number and intioal sum value which is intiated to zero for our convinience*/

  n=num/10;/*the n is nothing but the quetient of the division num/10 which contains only 4 digits of actual number*/

  a=n%10;/*now the new number is n as it has ectracted the last number in last calculation and last 4th digit is extracted*/

    rev=rev+a*1000;/*rev of first two numbers*/

    n=n/10;/*the new number contains only 3 digits*/

    a=n%10;/* 3rd digit is extracted*/

    rev=rev+a*100;

    n=n/10;/*new number contains 2 digits*/

     a=n%10;/* 2nd digit is extracted*/

      rev=rev+a*10;

     n=n/10;/*new number contains 1 digits*/

            a=n%10;/* 2nd digit is extracted*/

            rev=rev+a;

            printf("the rev of the five digit number entered is=%d",rev);

            getchar();

            if(rev==num)

            printf("the two numbers are equal");              

}

 

         

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



#include<stdio.h>

main()

{

            int a,num,sum;

            sum=0;

            printf("enter the 4 digit number\n");

            scanf("%d",&amp;num);

            a=num%10;/*last digit*/

            sum=sum+a;n

            a=num/1000;/*first digit*/

            sum=sum+a;

            printf("sum of last two digits 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.
Answer:


 
#include<stdio.h>

main()

{

       int pop,litmen,litwomen,totallit,totalmen;

       pop=80000;

       totalmen=0.52*80000;

       litmen=0.35*totalmen;

       totallit=0.48*pop;

       printf("total litteral men in town=%d\n",litmen);

       litwomen=totallit-litmen;

       printf("total literal women in town=%d\n",litwomen);

}


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

#include<stdio.h>

main()
{
 int ten,fifty,hundred,cash;
 printf("Enter amount in multiple of hundred\n");
 scanf("%d",&amp;cash);
 ten=cash/10;
  fifty=cash/50;
 hundred=cash/100;
 printf("Cashier will have to give %d ten rupees notes\n or \nCashier will have to give %d fifty rupees notes\n or \nCashier will have to give %d hundred rupees notes\n",ten,fifty,hundred);
}

 



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


#include<stdio.h>

main()

{

       float totalselprice,totalprofit,costofeach;

       printf("enter the totalselprice of the 15 items: ");

       scanf("%f",&amp;totalselprice);

       printf("enter the totalprofit earned on the 15 items: ");

       scanf("%f",&amp;totalprofit);

       costofeach=(totalselprice-totalprofit)/15;

       printf("cost of each item is =%f",costofeach);

}

 



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

#include<stdio.h>

main()

{

       int num,res;

       printf("enter any five digit number: ");

       scanf("%d",&amp;num);

       res=num=num+11111;

       printf("outpur is %d",res);

}

 



No comments