+ All Categories
Home > Education > Let us c(by yashwant kanetkar) chapter 2 solution

Let us c(by yashwant kanetkar) chapter 2 solution

Date post: 11-Apr-2017
Category:
Upload: rohit-kumar
View: 3,085 times
Download: 120 times
Share this document with a friend
27
LET US C ( BY YASHVANTKANETKAR) CHAPTER 2 SOLUTION Exercise:- if, if-else, Nested if-elses [A] (a). garbage_value 200 (b). 300 200 (c). Nothing is going to be print. (d). 3
Transcript
Page 1: Let us c(by yashwant kanetkar) chapter 2 solution

LET US C ( BYYASHVANTKANETKAR) CHAPTER 2

SOLUTION

Exercise:-

if, if-else, Nested if-elses

[A]

(a).

garbage_value 200

(b).

300 200

(c).

Nothing is going to be print.

(d).

3

Page 2: Let us c(by yashwant kanetkar) chapter 2 solution

(e).

x & y are equal

(f).

10 10 0

(g).

0 50 0

(h).

C is WOW

(i).

15 15 0

(j).

0 20 1

[B]

(a).We need to use == (equality operator ) instead of using = (assignment) inside if( ) statement.

(b).There is written extra { } inside if( ) statement due to this it will not give any output.

(c).There is no error.

Page 3: Let us c(by yashwant kanetkar) chapter 2 solution

(d).Here 'then' is written after if statement if we want to print value of x it will not print because 'then' statement is written after if( ) statement.

(e).There is no parenthases after if.

(f).We need to use ==(equality operator) instead of using =(assignment) inside if( ) statement

(g).There is no error.

(h).Same as (d). part.

(i).After if(a>b) ';' is written which is not allowed in c because if we write like this then compiler understand that there is null statement written after if statement and if we want to print someting after if( ) statement it will not print.

[C]

(a)./*to find that seller has made profit or incurred loss & also find the value*/

#include<stdio.h>

void main()

{

float cp,sp,c,d;

printf("Enter the cost price & selling price of an item\n");

scanf("%f%f",&cp,&sp);

c=sp-cp;

d=-c;

if(c>0)

{

Page 4: Let us c(by yashwant kanetkar) chapter 2 solution

printf("seller has made profit\n");

printf("profit=%f\n",c);

}

else if(c<0)

{

printf("seller has incurred loss\n");

printf("loss=%f\n",d);

}

else

printf("seller has made neither profit nor loss\n");

}

(b)./*to find the entered integer is even or odd no.*/

#include<stdio.h>

void main()

{

int a,d;

printf("Enter the integer\n");

scanf("%d",&a);

d=a%2;

if(d==0)

printf("It is an even no.\n");

else

printf("It is a odd no.\n");

}

(c)./*to justify entered year is leap year or not*/

#include<stdio.h>

void main()

{

Page 5: Let us c(by yashwant kanetkar) chapter 2 solution

int a,b;

printf("Enter the year\n");

scanf("%d",&a);

b=a%4;

if(b==0)

printf("The year you entered is a leap year\n");

else

printf("The year you entered is not a leap year\n");

}

(d)./*what is the day on 1st january if monday was the day on 1st jan 1900*/

#include<stdio.h>

void main()

{

int yr,b,c,d,td,e;

printf("Enter the year\n");

scanf("%d",&yr);

b=(yr-1900-1);

c=b/4;

d=b-c;

td=((d*365)+(c*366)+1);

e=td%7;

if(e==0)

printf("the day is monday\n");

if(e==1)

printf("the day is tuesday\n");

if(e==2)

printf("the day is wednesday\n");

if(e==3)

printf("the day is thursday\n");

Page 6: Let us c(by yashwant kanetkar) chapter 2 solution

if(e==4)

printf("the day is friday\n");

if(e==5)

printf("the day is saturday\n");

if(e==6)

printf("the day is sunday\n");

}

(e)./*to evaluate the reverse of five digit no. & also find it is same as entered no. or not*/

#include<stdio.h>

void main()

{

int a,b,c,d,e,f,g,h,i;

printf("enter five digit no.\n");

scanf("%d",&a);

b=a/10000;

c=((a%10)*10000);

d=(((a/1000)%10)*10);

e=(((a/100)%10)*100);

f=(((a/10)%10)*1000);

h=b/10;

i=b%10;

g=(b+c+d+e+f);

if((h==0)&&(i!=0))

{

if(a==g)

{

printf("reverse no.=%d\n",g);

printf("and reverse no. is same as entered no.\n");

}

Page 7: Let us c(by yashwant kanetkar) chapter 2 solution

else

{

printf("reverse no.=%d\n",g);

printf("and the reverse no. is not same as entered no.\n");

}

}

else

printf("Entered no. is not a 5 digit no.\n");

}

(f)./*out of three person who is the youngest one*/

#include<stdio.h>

void main()

{

int a,b,c;

printf("Enter the ages of ram,shyam & ajay respectively\n");

scanf("%d%d%d",&a,&b,&c);

if(((a>=b)&&(b>c)) || ((b>=a)&&(a>c)))

printf("ajay is the youngest one\n");

else if(((a>=c)&&(c>b)) || ((c>=a)&&(a>b)))

printf("shyam is the youngest one\n");

else if(((c>=b)&&(b>a)) || ((b>=c)&&(c>a)))

printf("ram is the youngest one\n");

else

printf("can't determine the youngest\n");

}

(g)./*to find the triangle is valid or not*/

#include<stdio.h>

void main()

Page 8: Let us c(by yashwant kanetkar) chapter 2 solution

{

float a1,a2,a3,sum;

printf("Enter three angles of a triangle\n");

scanf("%f%f%f",&a1,&a2,&a3);

sum=a1+a2+a3;

if(sum==180)

printf("It is a valid triangle\n");

else

printf("Not a valid triangle\n");

}

(h). /*to find the absolute value*/

#include<stdio.h>

void main()

{

int a,b;

printf("Enter a no.\n");

scanf("%d",&a);

b= - a;

if(a>=0)

printf("absolute value=%d\n",a);

if(a<0)

printf("absolute value=%d\n",b);

}

(i)./*compare the value of area & perimeter of a rectangle*/

#include<stdio.h>

void main()

{

float l,b,ar,p;

Page 9: Let us c(by yashwant kanetkar) chapter 2 solution

printf("Enter the length & breath of a rectangle\n");

scanf("%f%f",&l,&b);

ar=(l*b);

p=(2*(l+b));

if(ar>p);

printf("the area of the rectangle is greater than the perimeter\n");

if(p>=ar)

printf("area of the rectangle is not greater than perimeter\n");

}

(j)./*to find the three points are on the same line or not*/

#include<stdio.h>

void main()

{

float x1,y1,x2,y2,x3,y3,a,b,c,d,e,f;

printf("Enter the x1 & y1 of first point\n");

scanf("%f%f",&x1,&y1);

printf("Enter the x2 & y2 of second point\n");

scanf("%f%f",&x2,&y2);

printf("Enter the x3 & y3 of third point\n");

scanf("%f%f",&x3,&y3);

a=((x1-x2)/(y1-y2));

b=-a;

c=((x2-x3)/(y2-y3));

d=-c;

e=((x1-x3)/(y1-y3));

f=-e;

if((a>=0)&&(b>=0)&&(c>=0))

{

if((a==c)&&(c==e))

Page 10: Let us c(by yashwant kanetkar) chapter 2 solution

printf("all the points lie on the same line\n");

else

printf("all the points are not on the same line\n");

}

else

{

if((b==d)&&(d==f))

printf("all the points lie on the same line\n");

else

printf("all the points are not on the same line\n");

}

}

(k)./*to find wheather the point lies inside,outside or on the circle*/

#include<stdio.h>

#include<math.h>

void main()

{

float x1,y1,x2,y2,r,a,dist;

printf("Enter the x coordinate of centre of the circle\n");

scanf("%f",&x1);

printf("Enter the y coordinate of centre of circle\n");

scanf("%f",&y1);

printf("Enter the x coordinate in the plane of circle\n");

scanf("%f",&x2);

printf("Enter the y coordinate in the plane of circle\n");

scanf("%f",&y2);

printf("Enter the redius of the circle\n");

scanf("%f",&r);

a=(((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)));

Page 11: Let us c(by yashwant kanetkar) chapter 2 solution

dist=sqrt(a);

if(dist<r)

printf("the point lies inside the circle\n");

if(dist==r)

printf("the point lies on the circle\n");

if(dist>r)

printf("the point lies outside the circle\n");

}

(l)./*To find wheather the point lies on the x-axis or at the origin*/

#include<stdio.h>

void main()

{

float x,y;

printf("Enter the x coordinat\n");

scanf("%f",&x);

printf("Enter the y coordinate\n");

scanf("%f",&y);

if((x==0)&&(y!=0))

printf("point lies on the y-axis\n");

else if((x!=0)&&(y==0))

printf("point lies on the x-axis\n");

else if((x==0)&&(y==0))

printf("point is at origin\n");

else

printf("point lies on the x-y plane but not on the x,y or origin\n");

}

logical operators If a=10, b=12, c=0, find the values of the expression in the following table:-

Page 12: Let us c(by yashwant kanetkar) chapter 2 solution

Expression value

a!=6 && b>5

a==9 || b<3

! ( a<10 )

! ( a>5 && c )

5 && c != 8 || !c

1

0 1

0

1

[D]

(a).

Dean of student affairs

(b).

Let us c

(c).

W=1 x=0 y=1 z=1

(d).

Y=1 z=1

(e).

Bennarivo

(f).a

Page 13: Let us c(by yashwant kanetkar) chapter 2 solution

(g).

Definitely C !

(h).1 1

(i).z is big

(j).0 0

(k).0

[E]

(a).Incorrect comment:- /* This program

/* is an example of

/* using Logical operators */

correct comment:- /* This program

is an example of

using Logical operators*/

( Between /* and */ we use to write the comments, and comments can not be nested we have to write only in /* and */) like

(b).Incorrect statement:- if ( code == 1 & flag == 0)

correct statement :- if ( code == 1 && flag == 0)

In if ( ) statement we always use '&&'(logical operator ) but here only '&' used.

Page 14: Let us c(by yashwant kanetkar) chapter 2 solution

(c).Incorrect statement:- if ( spy=='a' or password =='z' )

correct statement:- if ( spy=='a' || password =='z' )

Here 'or' is written in the if ( ) statement instead of using or operator( '||' ).

(d).Incorrect statement :- If ( i=5) && if(j=10);

correct statement :- if((i==5)&&(j==10))

this statement is totally wrong

1.&& operator always use between the airthematic operation inside the if ( ) statement but here used between two if ( ) statement.

2. =(assignment) is used instead of using ==(relational operator)

complete statement is

(e).Here 'and' used in if ( ) statement instead of using &&( and operator)

incorrect statement:- if(x>=2 and y<=50)

correct statement :- if(x>=2 && y<=50)

(f).Incorrect statement:- if(a==1 & b==0)

correct statement ;- if(a==1 && b==0)

here also & (assignment ) used istead of && ( logical operator)

(g).No error.

[F]

(a)./*To find wheather the year you entered is leap year or not*/

#include<stdio.h>

Page 15: Let us c(by yashwant kanetkar) chapter 2 solution

void main()

{

int y,a,b,c;

printf("Enter the year\n");

scanf("%d",&y);

a=y%100;

b=y%400;

c=y%4;

if(((a==0)&&(b==0))||((a!=0)&&(c==0)))

printf("the year you entered is a leap year\n");

else

printf("the year you entered is not a leap year\n");

}

(b)./*To find the charcter written by you is a capital letter or small letter or a digit or a special symbol*/

#include<stdio.h>

void main()

{

char c;

printf("Enter any cahracter\n");

scanf("%c",&c);

if((c<=90)&&(c>=65))

printf("Entered character is capital letter\n");

if((c<=122)&&(c>=97))

printf("Entered character is small case letter\n");

if((c<=57)&&(c>=48))

printf("Entered character is a digit\n");

if(((c<=47)&&(c>=0))||((c<=64)&&(c>=58))||((c<=96)&&(c>=91))||((c<=127)&&(c>+123)))

printf("Entered character is an special symbol\n");

Page 16: Let us c(by yashwant kanetkar) chapter 2 solution

}

(c)./*to find wheather the person is insured or not also find the premium rate and maximum amount*/

#include<stdio.h>

void main()

{

int H,age,live,sex;

printf("write the person's health(write 1 if excellent and 0 if poor)\n");

scanf("%d",&H);

printf("Enter the age of the person\n");

scanf("%d",&age);

printf("write the person's living standard(write 1 if live in the city & 0 if live in the village)\n");

scanf("%d",&live);

printf("write the sex of the person(write 1 for male & 0 for female\n");

scanf("%d",&sex);

if((H==1)&&((age<=35)&&(age>=25))&&(live==1)&&(sex==1))

{

printf("the person should be insured\n");

printf("the premium rate=Rs. 4 per thousand\n");

printf("maximum amount =Rs. 2 lakhs\n");

}

else if((H==1)&&((age<=35)&&(age>=25))&&(live==1)&&(sex==0))

{

printf("the person should be insured\n");

printf("the premium rate=Rs. 3 per thousand\n");

printf("maximum amount =Rs. 1 lakhs\n");

}

else if((H==0)&&((age<=35)&&(age>=25))&&(live==0)&&(sex==1))

{

Page 17: Let us c(by yashwant kanetkar) chapter 2 solution

printf("the person should be insured\n");

printf("the premium rate=Rs. 6 per thousand\n");

printf("maximum amount =Rs. 10000\n");

}

else

printf("person should not be insured\n");

}

(d)./*to find the grade of the steel*/

#include<stdio.h>

void main()

{

float hard,carbon,tens;

printf("Enter the value of hardness of the steel\n");

scanf("%f",&hard);

printf("Enter the carbon content in the steel\n");

scanf("%f",&carbon);

printf("enter the value of tensile strength of the steel\n");

scanf("%f",&tens);

if((hard>50)&&(carbon<0.7)&&(tens>5600))

printf("grade of steel is 10\n");

if((hard>50)&&(carbon<0.7)&&(tens<=5600))

printf("grade of sttel is 9\n");

if((hard<50)&&(carbon<0.7)&&(tens>5600))

printf("grade of steel is 8\n");

if((hard>50)&&(carbon>0.7)&&(tens>5600))

printf("grade of steel is 7\n");

if(((hard>50)&&(carbon>0.7)&&(tens<5600))||((hard<50)&&(carbon<0.7)&&(tens<5600))||((hard<50)&&(carbon>0.7)&&(tens>5600)))

printf("grade of steel is 6\n");

Page 18: Let us c(by yashwant kanetkar) chapter 2 solution

if((hard<50)&&(carbon>0.7)&&(tens<5600))

printf("grade of steel is 5\n");

}

(e)./*to find the library fine & also find the membership will be cancelled or not*/

#include<stdio.h>

void main()

{

int day;

printf("Enter the no. of days the member is late to return the book\n");

scanf("%d",&day);

if(day<=5)

{

printf("fine = 50 paise\n");

printf("your membership will not be cancelled\n");

}

else if(day<=10)

{

printf("fine = 1 rupee\n");

printf("your membership will not be cancelled\n");

}

else if(day<=30)

{

printf("fine = 5 rupees\n");

printf("your membership will not be cancelled\n");

}

else if(day>30)

printf("your membership will be cancelled\n");

}

Page 19: Let us c(by yashwant kanetkar) chapter 2 solution

(f)./*write a program to find the triangle is valid or not*/

#include<stdio.h>

void main()

{

int s1,s2,s3;

printf("Enter the three sides of a triangle\n");

scanf("%d%d%d",&s1,&s2,&s3);

if((s1>=s2 && s2>=s3)||(s1>=s3 && s3>=s2))

{

if(s2+s3>s1)

printf("triangle is valid\n");

else

printf("triangle is not valid\n");

}

if((s2>=s1 && s1>=s3)||(s2>=s3 && s3>=s1))

{

if(s1+s3>s2)

printf("triangle is valid\n");

else

printf("triangle is not valid\n");

}

if((s3>=s1 && s1>=s2)||(s3>=s2 && s2>=s1))

{

if(a+b>c)

printf("triangle is valid\n");

else

printf("triangle is not valid\n");

}

}

Page 20: Let us c(by yashwant kanetkar) chapter 2 solution

(g)./*To find wheather the triangle is equilateral or isoscales or scalene or right angled

triangle*/

#include<stdio.h>

void main()

{

float a,b,c,d,e,f;

printf("Enter the three sides of the triangle\n");

scanf("%f%f%f",&a,&b,&c);

d=((a*a)+(b*b)-(c*c));

e=((b*b)+(c*c)-(a*a));

f=((c*c)+(a*a)-(b*b));

if((a==b)&&(b==c))

printf("the triangle is an equilateral triangle\n");

if(((a==b)&&(b!=c))||((b==c)&&(c!=a))||((c==a)&&(a!=b)))

printf("the triangle is an isoscales triangle\n");

if((a!=b)&&(b!=c)&&(c!=a))

printf("the triangle is scalene triangle\n");

if((d==0)||(e==0)||(f==0))

printf("the triangle is a right angled triangle\n");

}

(h)./*to find the efficiency of worker*/

#include<stdio.h>

void main()

{

float time;

printf("Enter the time taken by the worker\n");

scanf("%f",&time);

if((time>=2)&&(time<3))

printf("the worker is highly efficient\n");

Page 21: Let us c(by yashwant kanetkar) chapter 2 solution

if((time>=3)&&(time<4))

printf("the worker is ordered to improve speed\n");

if((time>=4)&&(time<5))

printf("the worker is given tranning to improve the speed\n");

if(time>=5)

printf("the worker has to leave the company\n");

if(time<2)

printf("for any worker this is not possible\n");

}

(i)./*to find the student is pass or fail*/

#include<stdio.h>

void main()

{

float A,B;

printf("Enter the percentage got by a student in main subject A & the subsidiary subject B\n");

scanf("%f%f",&A,&B);

if((A>=55)&&(B>=45))

printf("the student has passed\n");

else if((A<55)&&(B>=55)&&(A>=45))

printf("the student has passed\n");

else if((B<45)&&(A>=65))

printf("To qualify the student is allowed to reappear in an examination\n");

else

printf("the student is decleared to have failed\n");

}

(j)./*To find the goods will be supply or not*/

#include<stdio.h>

Page 22: Let us c(by yashwant kanetkar) chapter 2 solution

void main()

{

int a,b;

printf("write the credit of the customer wheather it is OK or not(1 for OK & 0for NOT OK\n");

scanf("%d",&a);

printf("write wheather the customer order is less than or equal to that in stock (1 if it is in the stock & 0 if it is not\n");

scanf("%d",&b);

if((a==1)&&(b==1))

printf("supply has requirement\n");

else if(((a==0)&&(b==1))||((a==0)&&(b==0)))

printf("do not supply\n");

else if((a==1)&&(b==0))

printf("supply what is in the stock rest will supply later\n");

}

Conditional operators

[G]

(a).

unpredictable : no. Not initialised

(b).

200

(c).

Welcome

Page 23: Let us c(by yashwant kanetkar) chapter 2 solution

[H]

(a).Incorrect statement:- ( code > 1 ? printf(“\nHello”) ? printf(“\nHi”));

correct statement :- ( code > 1 ? printf(“\nHello”) : printf(“\nHi”));

in the conditional operator the format is

satement 1 ? statement 2 : statement 3;

but in this it is written like this

statement 1 ? statement 2 ? statement 3;

(b).Format string written is wrong there is no character we had defined but then also we have used in the printf function.

(c).No error.

(d).The format of conditional operator is

statement1 ? Statement2 : statement3

but here after statement2 ':' is not written so this statement is wrong.

(e).Incorrect statement:- (n==9 ? printf(“You are correct”) ; : printf(“You are wrong”););

correct statement:- (n==9?printf(“You are correct”) : printf(“You are wrong”));

format of conditional operator is

statement1 ? Statement2 : statement3;

but here '; :' written instead of ':'

(f).Variable name is wrong

in the variable name we always use to write alphabet,digits & underscore

and also we can't use the digit as the first symbol of name of variable.

Page 24: Let us c(by yashwant kanetkar) chapter 2 solution

(g).No error

[I]

(a).main()

{

int x,min,max;

scanf("\n%d%d",&max,&x);

x>max?(max=x):(min=x);

}

(b).main()

{

int code;

scanf("%d",&code);

code>1 ? printf("\nJerusalem") : (code<1 ? printf("\nEddie") : printf("\nC Brain"));

}

(c).main()

{

float sal;

printf("Enter the salary");

scanf("%f",&sal);

sal<40000 && sal>25000 ? printf("Manager"):(sal<25000 && sal>15000 ? printf("Accountant") : printf("Clerk"));

}

Page 25: Let us c(by yashwant kanetkar) chapter 2 solution

[J]

(a).#include<stdio.h>

void main()

{

char c;

printf("Enter any character\n");

scanf("%c",&c);

c<=122&&c>=97 ? printf("Entered character is lower case alphabet\n") : printf("Entered character is not a lower case alphabet\n");

c<=47&&c>=0||c>=58&&c<=64||c>=91&&c<=96||c>=123&&c<=127 ? printf("Entered character is a special symbol\n") : printf("Entered character is not a special symbol\n");

}

(b).#include<stdio.h>

void main()

{

int year;

printf("Enter the year\n");

scanf("%d",&year);

year%4==0 ? printf("Entered year is a leap year\n") : printf("Entered year is not a leap year\n");

}

(c).#include<stdio.h>

void main()

{

int a,b,c;

printf("Enter three numbers\n");

Page 26: Let us c(by yashwant kanetkar) chapter 2 solution

scanf("%d%d%d",&a,&b,&c);

a>b&&b>=c||a>c&&c>=b ? printf("greatest no. =%d\n",a) : (b>c&&c>=a||b>a&&a>=c ? printf("greatest no. = %d\n",b):(c>b&&b>=a||

c>a&&a>=b ? printf("greatest no. = %d\n",c) : printf("can't find the greatest no.\n")));

}

THANKS............

Page 27: Let us c(by yashwant kanetkar) chapter 2 solution

Recommended