Sie sind auf Seite 1von 17

Solution Let us C by yashvant kanetkar (5

th

Nicks (The Last Assassin)

Edition) Logicsoft Corporation

Exercise
Chaptor-1 Getting Started

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


1. BASICSALARY 2. #MEAN-

(Valid)

(Not valid) as variable name doesnt contain any special symbol.

3. population in 2006- (Not valid) No blanks or space allowed in variable name.


4. FLOAT- (Valid) this is valid because all letters are in uppercase but if lowercase than not valid.
5. teamsvictory-(Not valid) variable name doesnt contain any special character ().
6._basic-

(valid)

7. group.-(Not valid) cant use (.)In variable name.


8. over time-(Not valid) No space.
9. hELLO-

(valid)

10. plot#3-(Not valid) No special symbol.


11. basic-hra: (Not valid) (-) is not allowed.
12.422-(Not valid) First letter should be (alphabets or _ ).
13. mindovermatter-

(Valid)

14. queue.-(Not valid) No (.) allowed.


15.2015_DDay-(Not valid) Variable name start with (alphabets or _).

[B]Point out Errors, if any in the following C statement.


(a) int =314.562*150; - wrong statement! int is a reserved keyword cant take as a variable.
(b)name=Ajay; -wrong statement! Character variable can hold one degit or alphabet.
(c)varchar=3;correct statement.
(d)3.14*r*r*h=vol_of_cyl;-wrong statement! This is right (Vol_of_cyl=3.14*r*r*h;)
(e)k=(a*b)(c+(2.5a+b))(d+e);wrong statement! Instead write this: k=(a*b)*(c+((2.5*a)+b))*(d+e).

(f)m_inst=rate of intrest*amount in rupees; -wrong statement! Variable name doesnt contain


blanks or space.write this : m_inst=rateofinterest*amountinrupees;.
(g)si=principle*rateof interest*noofyears/100; Correct statement.
(h)area=3.14*r**2; wrong statement! Here exponentiation is not possible this way.we can use
here inbuilt math function pow().
(i)volume=3.14*r^2*h; Correct statement.
(j)k=((a*b)+c)(2.5*a+b);-wrong statement! This is right: k=(a*b+c)*(2.5*a+b);
(k)a=b=3=4;-wrong statement! First a degit cant be a variable and variable cant hold two
different value at a time.
(l) count=count+1;correct statement if count is defined before.
(m) date=2 march 04;wrong statement! Character statement must be 1 or 2 character long.

[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)?
1st: 2/2+2*4/2-2+2.5/3;
2nd: 1+2*4/2-2+2.5/3;
3rd: 1+8/2-2+2.5/3;
4th: 1+4-2+2.5/3;
5th: 1+4-2+0.83;
6th:5-2+0.83;
7th:3+0.83;
8th:3.83;
(b) on=ink*act/2+3/2*act+2+tig;(ink=4,act=1,tig=3.2,assume on to be int)?
1st :4*1/2+3/2*1+2+3.2;
2nd:5/2+3/2*1+2+3;
3rd:2+3/2*1+2+3;
4th:2+1*1+2+3;

5th:2+1+2+3;
6th:8;
(c)s=qui*add/4-6/2+2/3*6/god;(qui=4,add=2,god=2,assume s to be an int)?
1st:4*2/4-6/2+2/3*6/2;
2nd:8/4-6/2+2/3*6/2;
3rd:2-6/2+2/3*6/2;
4th:2-3+2/3*6/2;
5th:2-3+0*6/2;(zero)
6th:-1;
(d)s=1/3*a/4-6/2+2/3*6/g;(a=4,g=3,assume s to be int)?
1st:1/3*4/4-6/2+2/3*6/3;
2nd:0*4/4-6/2+2/3*6/3;
3rd:0/4-6/2+2/3*6/3;
4th:0-6/2+2/3*6/3;
5th:-3+2/3*6/3;
6th:-3+0*6/3;
7th:-3;

[D]fill the following table for the expression given below and than evaluate the
result. A sample entry has been filled in the table for expression (a).
(a) g=10/5/2/1;

Operator
/

Left
10

Right
5 or 5/2/1

2 or 2/1

1 or 1/1

Remark
Left operand is
unambiguous right is
not
Left operand is
unambiguous right is
not
Left operand is

unambiguous right is
not
(b)b=3/2+5*4/3;
Operator
/

Left
3

Right
2 or 2+5*4/3

4 or 4/3

20

1.5

6.67

Left
3

Right
4

Remark
Left operand is
unambiguous right is
not
Left operand is
unambiguous right is
not
both operand is
unambiguous
both operand is
unambiguous

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

Remark
both operand is
unambiguous

[E] Convert the following equations in to corresponding C stataement.


(a)z=8.8(a+b)2/c-0.5+2a/(q+r)

(a+b)*(1/m)

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
X=(-b+b*b+2*(4*a*c)) / ( 2*a);
(c)R=2v+6.22(c+d) / g+v
R=2*v+6.22*(c+d)/(g+v) ;
(d)A=7.7b(xy+a)/c-0.8+2b / (x+a)(1/y)
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);
}
ans: output=021.993
(b)main(){
Int a,b;
a=-33;
b=-3(-3);
printf(a=%d b=%d,a,b);
}
Output=Throw Error value required.
(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 n/n
(e)main(){
Int a,b;
printf(enter the value of a and b);
scanf(%d%d,&a,&b);
printf(a=%d b=%d,a,b);
}
Output = this will print the value entered by the user.
(f)main(){
Int p,q;
Printf(enter the value of p and q);
Scanf(%d%d,p,q);
Printf(p=%d q=%d,p,q);
}
Output =hear we will get garbage value or any random value.

[G]pick up the correct alternative for each of the following question?


(a)C language has been developed by-(2)dennis Ritchie.
(b)C can be used on-(4)all of the above,DOS,Linux,Windows
(c)C programs are converted into machine language with the help of-(2)A compiler.
(d)The real constant in C can be Expressed in which of the following forms-(4)Both Fractional
and Exponential form.
(e)A character variable can at a time store-(1)1 character actually 1 or 2 character.
(f)The statement (char ch=Z) would store in ch-(4) both character Z and ASCII value of Z.

(g)Which of the following is not a character constant-(4) All of the above.


(h)The maximum value that integer constant can have is-(2)32767
(i)A C variable cannot start with-(4)both 2nd and 3rd A number and A special symbol except (_).
(j) which of the following statement is wrong(1)mes=123.56;
(2)con=T*A;
(3)this=T*20;
(4)3+a=b;
Here 4th is wrong.
(k) which of the following shows correct hierarchy of arithmetic oprators in C.-(4) /or*,-or+
(l) In b=6.6/a+2*n;which operation will be performed first.-(1)6.6/a
(m) Which of the following is allowed in a C arithmetic instruction.(3)parenthesis ().
(n)which of the following statements falls.
(1)Each new C instruction has to be written on a separate line. Wrong! C is a free from
language we can write any whare in editor.
(o) If the integer variable, a=5/2; will return a value-(3)2.
(p)The expression, a=7/22 *(3.22+2)*3/5; evaluates to.-(4) 0.
(q)The expression, a=30*1000+2768; evaluates to.-(1)32768.
(r)The expression,x=4+2%-8;evaluates to.-(2)6.
(s) Hierarchy decides which operator-(2) is used first.
(t)An integer constant must have.(1)At least one digit.
(u)A character variable can never store more than.(4)1 character.
(v)In c a variable can not contain.-(4) All the above.
(w) Which of the following is falls in C.-(1) keywords can be used as variable names.
(x)In C arithmetic instruction cannot contain.-(4) Constant on left side of =.
(y) Which of the following shows correct hierarchy of arithmetic operations in C.-(4)*/+-

(z) What will be the value of d if d is float after the operation d=2/7.0.-(2)0.2857

[H]Write C program for the following:


(a)Ramesh basic salary input through keyboard.hid dearness allowances is 40% of basic salary
and house rent allowances is 20% of basic salary.write a programme to calculate gis gross salary.
Ans:

#include<stdio.h>
#include<conio.h>
main(){
int salary,dAllowance,hAllowance,gSalary;
printf("Enter the amount of salary");
scanf("%d",&salary);
dAllowance=salary*0.4;
printf("your dearness allowance is %d \n",dAllowance);
hAllowance=salary*0.2;
printf("your house rent allowance is %d\n",hAllowance);
gSalary=salary+dAllowance+hAllowance;
printf("your gross salary is %d",gSalary);
getch();
}

(b)The distance between two cities (in km.)Is input throw the keyboard. Write a program to
convert and print this distance in mtr, inches and centimeters?
Ans:

#include<stdio.h>
#include<conio.h>
main(){
int distance,meters,inches,centimeters;
printf("Enter the distance in km. :");
scanf("%d",&distance);
meters=distance*1000;
printf("your distance in meters is : %d \n",meters);
inches=meters*3;

printf("your distance in inches is %d\n",inches);


centimeters=inches*2.5;
printf("your distance in centimeters is %d",centimeters);
getch();
}

(c)If the marks obtained by the 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 the student in each subject is 100.
Ans:

#include<stdio.h>
#include<conio.h>
main(){
int agMarks,sub1,sub2,sub3,sub4,sub5;
float pMarks;
printf("Enter the marks of subject :");
scanf("%d%d%d%d%d",&sub1,&sub2,&sub3,&sub4,&sub5);
agMarks=(sub1+sub2+sub3+sub4+sub5)/5;
printf("your aggregate marks

is : %d \n",agMarks);

pMarks=(sub1+sub2+sub3+sub4+sub5)/500.0*100;
printf("your pecentage marks is %f",pMarks);
getch();
}

(d)Temprature of a city in Fahrenheit degrees is input through the keyboard write a


programme to convert this temperature into centigrade degrees.
Ans:

#include<stdio.h>
#include<conio.h>
main(){
int dgfahren;
float dgcent;
printf("Enter degrees in fahrenheit :");
scanf("%d",&dgfahren);

dgcent=5.0/9*(dgfahren-32);
printf("Degree in centigrade is : %f",dgcent);
getch();
}

(e)The length and breadth of a rectangle and radius of a circle are input through the keyboard
write a program to calculate area and perimeter of the rectangle,and the area and the circumference
of the circle.
Ans:

#include<stdio.h>
#include<conio.h>
main(){
int length,breadth,rArea,rPerimeter;
float radius,cArea,cCircum;
printf("Enter the Length and Breadth of ractangle :");
scanf("%d%d",&length,&breadth);
rArea=length*breadth;
printf("Area of a Ractangle is :%d\n",rArea);
rPerimeter=2*(length+breadth);
printf("Perimeter of a Ractangle is :%d\n\n\n",rPerimeter);
printf("Enter the Radius of a Circle :");
scanf("%f",&radius);
cArea=3.14*(radius*radius);
printf("Area of a circle is :%f\n",cArea);
cCircum=2*3.14*radius;
printf("Circumference of a circle is:%f",cCircum);
getch();
}

(f)Two numbers are input through the keyboard in two location C and D.write a program to
interchange the contents of C and D.
Ans:

#include<stdio.h>
#include<conio.h>

main(){
int c,d,e;
float radius,cArea,cCircum;
printf("Enter the value of C and D :");
scanf("%d%d",&c,&d);
e=c;
c=d;
d=e;
printf("Value after interchange is :%d%d\n",c,d);
getch();
}

(g)If a five digit Number is input through the keyboard,write a program to calculate the sum of
its digit.
Ans:

#include<stdio.h>
#include<conio.h>
main(){
long

int number,one,two,three,four,five,total;

clrscr();
printf("Enter a five degit number");
scanf("%ld",&number);
one=number%10;
number=number/10;
two=number%10;
number=number/10;
three=number%10;
number=number/10;
four=number%10;
number=number/10;
five=number%10;
total=one+two+three+four+five;

printf("Total of your number is :%ld",total);


getch();
}

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

#include<stdio.h>
#include<conio.h>
main(){
long int number,one,two,three,four,five,rNumber=0;
clrscr();
printf("Enter a five degit number");
scanf("%ld",&number);
one=number%10;
number=number/10;
rNumber=rNumber*10+one;
two=number%10;
rNumber=rNumber*10+two;
number=number/10;
three=number%10;
rNumber=rNumber*10+three;
number=number/10;
four=number%10;
rNumber=rNumber*10+four;
number=number/10;
rNumber=rNumber*10+number;
printf("Reverse number is :%ld",rNumber);
getch();
}

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

Ans:

#include<stdio.h>
#include<conio.h>
main(){
int number,first,last;
printf("Enter any Four digit number");
scanf("%d",&number);
first=number%10;
number=number/10;
number=number/10;
number=number/10;
last=number;
number=first+last;
printf("This is your answer :%d",number);
getch();
}

(j)In a town,the percentage of man is 52.the percentage of total literacy is 48.if total
percentage of literate man is 35 of the population,write a program to find a total number of illiterate
man and women if the population of town is 80,000.
Ans:

#include<stdio.h>
#include<conio.h>
main(){
long int
population,man,women,litMan,illitMan,litWomen,illitWomen,literacy;
clrscr();
printf("Enter the Total population :");
scanf("%ld",&population);
printf("Enter percentage of Man :");
scanf("%ld",&man);
printf("Enter the percentage of literacy :");
scanf("%ld",&literacy);

printf("Enter the percentage of lliterate man :");


scanf("%ld",&litMan);
literacy=population*(literacy/100.0);
printf("Total literacy :%ld\n",literacy);
man=population*(man/100.0);
printf("Total number of man :%ld\n",man);
litMan=man*(litMan/100.0);
printf("Total number of literate man :%ld\n",litMan);
illitMan=man-litMan;
printf("Total number of illiterate man :%ld\n",illitMan);
women=population-man;
printf("Total number of women :%ld\n",women);
litWomen=literacy-litMan;
printf("Total number of literate women :%ld\n",litWomen);
illitWomen=women-litWomen;
printf("Total number of illiterate women :%ld\n",illitWomen);
getch();
}

(k)A cashier has a currency notes of denomination 10, 50,100.if the amount to be withdrawn
is input throw the keyboard in hundreds, find the total number of currency notes of each
domination the cashier will have to give to the withdrawer.
Ans:Remaining
(l)If the total selling price of 15 items and the total profit earned on them is input through
keyboard, write a program to find cost price of one item.
Ans:

#include<stdio.h>
#include<conio.h>
main(){
long int tsPrice,profit,cPrice,sPrice,pProfit;
clrscr();
printf("Enter total selling price :");

scanf("%ld",&tsPrice);
printf("Enter profit price :");
scanf("%ld",&profit);
sPrice=tsPrice/15;
pProfit=profit/15;
cPrice=sPrice-pProfit;
printf("Cost price per Unit :%ld",cPrice);
getch();
}

(m)If a five digit number is input through keyboard,write a program to print a new number by
adding one to each of its digit for example if the number that is input 12391 than output should be
displayed as 23402
Ans:

#include<stdio.h>
#include<conio.h>
main(){
long int number,one,two,three,four,five;
clrscr();
printf("Enter the five digit number :");
scanf("%ld",&number);
one=number%10+1;
number=number/10;
two=number%10+1;
number=number/10;
three=number%10+1;
number=number/10;
four=number%10+1;
number=number/10;
five=number%10+1;
printf("now your new value is
:%ld%ld%ld%ld%ld",five,four,three,two,one);
getch();

Happy journey in the programming world


by Nikunj Gajera.
mail:nicks_gajera@yahoo.in