1. LET US C (5th
EDITION) QUESTIONS AND ANSWERS
BY-KAVYA SHARMA
(A)Which of these variables are valid and which are not? Why?
BASICSALARY-valid
_basic-valid
basic-hra-no special symbol except a underscore is allowed
#MEANno -special symbol except a underscore is allowed
group. -no special symbol except a underscore is allowed
422-a variable cannot start with a number
population in 2006-a variable cannot have spaces
over time-a variable cannot have spaces
mindovermatter-valid
FLOAT-a variable cannot be a key word
hELLO -valid
queue.-no special symbol except a underscore is allowed
team’svictory- no special symbol except a underscore is allowed
Plot # 3-no special symbol except a underscore is allowed
2015_Dday-a variable cannot start with a number
(B) Point out any errors in the following C statements. If any:
(a) int = 314.562 * 150 ;-no error
(b) name = ‘Ajay’ ;-no error
(c) varchar = ‘3’ ; - no inverted commas are required
(d) 3.14 * r * r * h = vol_of_cyl ;-no error
(e) k = ( a * b ) ( c + ( 2.5a + b ) ( d + e ) ; - a * should be added between two brackets
(f) m_inst = rate of interest * amount in rs ;-no error
(g) si = principal * rateofinterest * numberofyears / 100 ;-no error
(h) area = 3.14 * r ** 2 ; - there is no arithmetic operator such as **
(i) volume = 3.14 * r ^ 2 * h ; - there is no arithmetic opertor such as ^
(j) k = ( (a * b ) + c ) ( 2.5 * a + b ) ; - a * should be added between two brackets
(k) a = b = 3 = 4 ; - 3 and 4 are not equal
(l) count = count + 1 ;-no error
(m) date = '2 Mar 04' ; -no error
(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
g = 1 + 4/2 - 2 + 2.5/3 (DIVISION)
g = 1 + 2 - 2 + 2.5/3 (MULTIPLICATION)
g = 1 + 2 - 2 + 0.83 (DIVISION)
g = 3 - 2 + 0.83 (ADDITION)
g = 1 + 0.83 (SUBTRACTION)
g = 1.83 (ADDITION)
(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
on = 4/2 + 3/2*1 + 2 + 3.2 (MULTIPLICATION)
on = 2 + 3/2*1 + 2 + 3.2 (DIVISION)
2. LET US C (5th
EDITION) QUESTIONS AND ANSWERS
BY-KAVYA SHARMA
on = 2 + 1*1 + 2 + 3.2 (DIVISION)
on = 2 + 1 + 2 + 3.2 (MULTIPLICATION)
on = 3 + 2 + 3.2 (ADDITION)
on = 5 + 3.2 (ADDITION)
on = 8 (ADDITION)
(c) s = qui * add / 4 - 6 / 2 + 2 / 3 * 6 / god ;
(qui = 4, add = 2, god = 2, assume s to be an int)
Ans) s = 4/2*2/4 - 6/2 + 2/3*6/2
s = 2*2/4 - 6/2 + 2/3*6/2 (DIVISION)
s = 4/4 - 6/2 + 2/3*6/2 (MULTIPLICATION)
s = 1 - 6/2 + 2/3*6/2 (DIVISION)
s = 1 - 3 + 2/3*6/2 (DIVISION)
s = 1 - 3 + 0*6/2 (DIVISION)
s = 1 - 3 + 0/2 (MULTIPLICATION)
s = 1 - 3 + 0 (DIVISION)
s = -2 + 0 (SUBTRACTION)
s = -2 (ADDITION)
(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/3 + 2/3*6/3
s = 0*4/4 - 6/3 + 2/3 *6/3 (DIVISION)
s = 0/4 - 6/3 + 2/3*6/3 (MULTIPLICATION)
s = 0 - 6/3 + 2/3*6/3 (DIVISION)
s = 0 - 2 + 2/3*6/3 (DIVISION)
s = 0 - 2 + 0*6/3 (DIVISION)
s = 0 - 2 + 0/3 (MULTIPLICATION)
s = 0 - 2 + 0 (DIVISION)
s = -2 + 0 (SUBTRACTION)
s = -2 (ADDITION)
[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.
Expression Operator Left Right Remark
(a) g = 10 / 5 /2 / 1
;
= a 1 or 10/5/2/1 Left operand is
unambiguous, and right
side is not
(b) b = 3 / 2 + 5 * 4
/ 3 ;
= b 7 or 3/2 + 5*4/3 Left operand is
unambiguous, and right
side is not
(c) a = b = c = 3 + 4
;
= c or a=b=c 7 or 3 + 4 Both the sides are not
unambiguous
[E] Convert the following equations into corresponding C statements
(a) Z = 8.8(a + b)2/c - 0.5 + 2a/(q + r)
(a + b) * (l/m)
Ans) Z = (8.8*(a + b)*2/c - 0.5 + (2*a)/(q + r))/((a + b)*(l/m)
3. LET US C (5th
EDITION) QUESTIONS AND ANSWERS
BY-KAVYA SHARMA
(b) X = -b + (b*b) + 2 4ac
2a
Ans) X = (-b + (b*b) + 2*4*a*c)/(2*a)
(c) R = 2v + 6.22(c + d)
g + v
Ans) R = (2*v + 6(c+d))/(g + v)
(d) A = (7.7b (xy + a)/c - 0.8 + 2b
(x+y) (1/y)
Ans) A = (7.7*b*(x*y + a)/c - 0.8 + 2*b)/((x + y)*(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 ) ;
}
Ans) 0 2 0.000000 2.000000
(b) main( )
{
int a, b ;
a = -3 - - 3 ;
b = -3 - - ( - 3 ) ;
printf ( "a = %d b = %d", a, b ) ;
}
Ans) a = 0 b = -6
(c) main( )
{
float a = 5, b = 2 ;
int c ;
c = a % b ;
printf ( "%d", c ) ;
}
Ans) error: invalid operands to binary % (have ‘float’ and ‘float’)
c = a % b ;
4. LET US C (5th
EDITION) QUESTIONS AND ANSWERS
BY-KAVYA SHARMA
(d) main( )
{
printf ( "nn nn nnn" ) ;
printf ( "nn /n/n nn/n" ) ;
}
Ans) 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 ) ;
}
Ans) Enter the values of a and b: 1 and 2
a = 1 b = 2
(f) main( )
{
int p, q ;
printf ( "Enter values of p and q" ) ;
scanf ( " %d %d ", p, q ) ;
printf ( "p = %d q =%d", p, q ) ;
}
Ans) Enter the values of p and q
Segmentation fault (Core dumped)
[G] Pick up the correct alternative for each of the following questions (Highlighted are the correct
answers):
(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
5. LET US C (5th
EDITION) QUESTIONS AND ANSWERS
BY-KAVYA SHARMA
(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
6. LET US C (5th
EDITION) QUESTIONS AND ANSWERS
BY-KAVYA SHARMA
(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
7. LET US C (5th
EDITION) QUESTIONS AND ANSWERS
BY-KAVYA SHARMA
(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.
#include <stdio.h>
int main()
{
float basic_sal = 0;
printf("Enter the basic salary:");
scanf("%f",&basic_sal);
float DA = 0;
DA = basic_sal*40/100;
float HRA = 0;
HRA = basic_sal*20/100;
float gross_sal = 0;
gross_sal = basic_sal + DA + HRA;
8. LET US C (5th
EDITION) QUESTIONS AND ANSWERS
BY-KAVYA SHARMA
printf("The gross salary is:%f+%f+%f=%f",basic_sal,DA,HRA,gross_sal);
return 0;
}
(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.
#include <stdio.h>
int main()
{
float dis_in_km = 0;
printf("Enter the distance in kms:");
scanf("%f",&dis_in_km);
float dis_in_cm = 0;
dis_in_cm = dis_in_km*100000;
float dis_in_m = 0;
dis_in_m = dis_in_km*1000;
float dis_in_foot = 0;
dis_in_foot = dis_in_km*3281;
float dis_in_inches = 0;
dis_in_inches = dis_in_km*39370;
printf("The distance in centimeteres = %fn",dis_in_cm);
printf("The distance in meteres = %fn",dis_in_m);
printf("The distance in inches = %fn",dis_in_inches);
printf("The distance in feet = %fn",dis_in_foot);
return 0;
}
(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.
#include <stdio.h>
int main()
{
float mark1,mark2,mark3,mark4,mark5;
printf("Enter the marks of the 5 subjects:");
scanf("%f,%f,%f,%f,%f",&mark1,&mark2,&mark3,&mark4,&mark5);
float average_of_marks;
average_of_marks = (mark1+mark2+mark3+mark4+mark5)/5;
float per_of_marks;
per_of_marks = (mark1+mark2+mark3+mark4+mark5)/500*100;
printf("The average of marks is:%fn",average_of_marks);
printf("The percentage of marks is%fn",per_of_marks);
}
(d) Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to
convert this temperature into Centigrade degrees.
#include <stdio.h>
int main()
9. LET US C (5th
EDITION) QUESTIONS AND ANSWERS
BY-KAVYA SHARMA
{
float temp_in_far;
printf("Enter the temperature in farenheit:");
scanf("%f",&temp_in_far);
float temp_in_cel1;
temp_in_cel1 = temp_in_far-32;
float temp_in_cel2;
temp_in_cel2 = temp_in_cel1/9;
float temp_in_cel3;
temp_in_cel3 = temp_in_cel2*5;
printf("The temperature in celsius is: %f",temp_in_cel3);
}
(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.
#include <stdio.h>
int main()
{
float length,breadth,radius;
printf("Enter the length of the rectangle:");
scanf("%f",&length);
printf("Enter the breadth of the rectangle:");
scanf("%f",&breadth);
printf("Enter the radius of the circle:");
scanf("%f",&radius);
float area_of_rect,peri_of_rect,area_of_circle,circum_of_circle;
area_of_rect = length*breadth;
peri_of_rect = 2*(length + breadth);
area_of_circle = 22*radius*radius/7;
circum_of_circle = 2*22*radius/7;
printf("The area of the rectangle = %fn",area_of_rect);
printf("The perimeter of the rectangle = %fn",peri_of_rect);
printf("The circumference of the circle = %fn",circum_of_circle);
printf("The area of the circle = %fn",area_of_circle);
return 0;
}
(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.
#include <stdio.h>
int main()
{
int c,d;
printf("Enter the value of c:");
scanf("%d",&c);
printf("Enter the value of d:");
scanf("%d",&d);
c = c+d;
10. LET US C (5th
EDITION) QUESTIONS AND ANSWERS
BY-KAVYA SHARMA
d = c-d;
c = c-d;
printf("After interchanging the values of c and d we have c = %d and d = %d",c,d);
return 0;
}
(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 ‘%’)
#include <stdio.h>
int main()
{
int num,sum,a,b,c,d,e,d1,d2,d3,d4,d5;
printf("Enter any five digit number: ");
scanf("%d",&num);
a = num/10;
d5 = num%10;
b = a/10;
d4 = a%10;
c = b/10;
d3 = b%10;
d = c/10;
d2 = c%10;
e = d/10;
d1 = d%10;
sum = d1 + d2 + d3 + d4 + d5;
printf("The sum of the digits of the five-digit number you had entered is: %d",sum);
return 0;
}
(h) If a five-digit number is input through the keyboard, write a program to reverse the number.
#include <stdio.h>
int main()
{
int num,reversed_num,a,b,c,d,e,d1,d2,d3,d4,d5;
printf("Enter any five digit number: ");
scanf("%d",&num);
a = num/10;
d5 = num%10;
b = a/10;
d4 = a%10;
c = b/10;
d3 = b%10;
d = c/10;
d2 = c%10;
e = d/10;
d1 = d%10;
reversed_num = (d5*10000) + (d4*1000) + (d3*100) + (d2*10) + (d1*1);
11. LET US C (5th
EDITION) QUESTIONS AND ANSWERS
BY-KAVYA SHARMA
printf("The sum of the reversed digits of the five-digit number you had entered is:
%d",reversed_num);
return 0;
}
(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.
#include <stdio.h>
int main()
{
int num,sum,a,b,c,d,d1,d2,d3,d4;
printf("Enter any five digit number: ");
scanf("%d",&num);
a = num/10;
d4 = num%10;
b = a/10;
d3 = a%10;
c = b/10;
d2 = b%10;
d = c/10;
d1 = c%10;
sum = d4 + d1;
printf("The sum of the first and last digits of the four-digit number you had entered is: %d",sum);
return 0;
}
(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.
#include <stdio.h>
int main()
{
int
total_pop,illiterate_men,illiterate_women,literate_men,literate_women,per_of_men,total_literacy,
women;
printf("Enter the total population of the area:");
scanf("%d",&total_pop);
printf("Enter the percentage of men:");
scanf("%d",&per_of_men);
printf("Enter the percentage of total literacy:");
scanf("%d",&total_literacy);
printf("Enter the percentage of literate men:");
scanf("%d",&literate_men);
illiterate_men = per_of_men-literate_men;
illiterate_men = total_pop*illiterate_men/100;
literate_women = total_literacy - literate_men;
women = 100 - per_of_men;
12. LET US C (5th
EDITION) QUESTIONS AND ANSWERS
BY-KAVYA SHARMA
illiterate_women = women - literate_women;
illiterate_women = total_pop*illiterate_women/100;
printf("The number of illiterate men = %d ",illiterate_men);
printf("The number of illiterate women = %d ",illiterate_women);
return 0;
}
(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.
#include <stdio.h>
int main()
{
int amount;
printf("Please enter the amount to be withdrawen:");
scanf("%d",&amount);
int notes_100,notes_50,notes_10,notes_left;
notes_100 = amount / 100;
notes_50 = (amount % 100) / 50;
notes_10 = ((amount % 100) % 50) / 10;
notes_left = (((amount % 100) % 50) % 10);
printf("The number of rupee 100 notes to be withdrawen are: %dn",notes_100);
printf("The number of rupee 50 notes to be withdrawen are: %dn",notes_50);
printf("The number of rupee 10 notes to be withdrawen are: %dn",notes_10);
printf("The value of money left is: %dn",notes_left);
return 0;
}
(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.
#include <stdio.h>
int main()
{
float sp,gain,cp;
printf("Enter the sp of 15 items:");
scanf("%f",&sp);
printf("Enter the gain :");
scanf("%f",&gain);
cp = (sp-gain)/15;
printf("The cp of the 15 items = %f",cp);
return 0;
}
(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
#include <stdio.h>
int main()
13. LET US C (5th
EDITION) QUESTIONS AND ANSWERS
BY-KAVYA SHARMA
{
int a,b,c,d,e,f,n,sum;
printf("Enter a five digit numbern");
scanf("%d",&n);
a=((n%10)+1)%10;
f=n/10;
b=((f%10)+1)%10;
f=f/10;
c=((f%10)+1)%10;
f=f/10;
d=((f%10)+1)%10;
f=f/10;
e=((f%10)+1)%10;
f=f/10;
sum = (e*10000) + (d*1000) + (c*100) + (b*10) + (a*1);
printf("Result is %d",sum);
return 0;
}