1. 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
2. (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.
3. (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 itemn");
scanf("%f%f",&cp,&sp);
c=sp-cp;
d=-c;
if(c>0)
{
4. printf("seller has made profitn");
printf("profit=%fn",c);
}
else if(c<0)
{
printf("seller has incurred lossn");
printf("loss=%fn",d);
}
else
printf("seller has made neither profit nor lossn");
}
(b).
/*to find the entered integer is even or odd no.*/
#include<stdio.h>
void main()
{
int a,d;
printf("Enter the integern");
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()
{
5. int a,b;
printf("Enter the yearn");
scanf("%d",&a);
b=a%4;
if(b==0)
printf("The year you entered is a leap yearn");
else
printf("The year you entered is not a leap yearn");
}
(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 yearn");
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 mondayn");
if(e==1)
printf("the day is tuesdayn");
if(e==2)
printf("the day is wednesdayn");
if(e==3)
printf("the day is thursdayn");
6. if(e==4)
printf("the day is fridayn");
if(e==5)
printf("the day is saturdayn");
if(e==6)
printf("the day is sundayn");
}
(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.=%dn",g);
printf("and reverse no. is same as entered no.n");
}
7. else
{
printf("reverse no.=%dn",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 respectivelyn");
scanf("%d%d%d",&a,&b,&c);
if(((a>=b)&&(b>c)) || ((b>=a)&&(a>c)))
printf("ajay is the youngest onen");
else if(((a>=c)&&(c>b)) || ((c>=a)&&(a>b)))
printf("shyam is the youngest onen");
else if(((c>=b)&&(b>a)) || ((b>=c)&&(c>a)))
printf("ram is the youngest onen");
else
printf("can't determine the youngestn");
}
(g).
/*to find the triangle is valid or not*/
#include<stdio.h>
void main()
8. {
float a1,a2,a3,sum;
printf("Enter three angles of a trianglen");
scanf("%f%f%f",&a1,&a2,&a3);
sum=a1+a2+a3;
if(sum==180)
printf("It is a valid trianglen");
else
printf("Not a valid trianglen");
}
(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=%dn",a);
if(a<0)
printf("absolute value=%dn",b);
}
(i).
/*compare the value of area & perimeter of a rectangle*/
#include<stdio.h>
void main()
{
float l,b,ar,p;
9. printf("Enter the length & breath of a rectanglen");
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 perimetern");
if(p>=ar)
printf("area of the rectangle is not greater than perimetern");
}
(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 pointn");
scanf("%f%f",&x1,&y1);
printf("Enter the x2 & y2 of second pointn");
scanf("%f%f",&x2,&y2);
printf("Enter the x3 & y3 of third pointn");
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))
10. printf("all the points lie on the same linen");
else
printf("all the points are not on the same linen");
}
else
{
if((b==d)&&(d==f))
printf("all the points lie on the same linen");
else
printf("all the points are not on the same linen");
}
}
(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 circlen");
scanf("%f",&x1);
printf("Enter the y coordinate of centre of circlen");
scanf("%f",&y1);
printf("Enter the x coordinate in the plane of circlen");
scanf("%f",&x2);
printf("Enter the y coordinate in the plane of circlen");
scanf("%f",&y2);
printf("Enter the redius of the circlen");
scanf("%f",&r);
a=(((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)));
11. dist=sqrt(a);
if(dist<r)
printf("the point lies inside the circlen");
if(dist==r)
printf("the point lies on the circlen");
if(dist>r)
printf("the point lies outside the circlen");
}
(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 coordinatn");
scanf("%f",&x);
printf("Enter the y coordinaten");
scanf("%f",&y);
if((x==0)&&(y!=0))
printf("point lies on the y-axisn");
else if((x!=0)&&(y==0))
printf("point lies on the x-axisn");
else if((x==0)&&(y==0))
printf("point is at originn");
else
printf("point lies on the x-y plane but not on the x,y or originn");
}
logical operators
If a=10, b=12, c=0, find the values of the expression in the following table:-
12. 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
13. (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.
14. (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>
15. void main()
{
int y,a,b,c;
printf("Enter the yearn");
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 yearn");
else
printf("the year you entered is not a leap yearn");
}
(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 cahractern");
scanf("%c",&c);
if((c<=90)&&(c>=65))
printf("Entered character is capital lettern");
if((c<=122)&&(c>=97))
printf("Entered character is small case lettern");
if((c<=57)&&(c>=48))
printf("Entered character is a digitn");
if(((c<=47)&&(c>=0))||((c<=64)&&(c>=58))||((c<=96)&&(c>=91))||
((c<=127)&&(c>+123)))
printf("Entered character is an special symboln");
16. }
(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 personn");
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 femalen");
scanf("%d",&sex);
if((H==1)&&((age<=35)&&(age>=25))&&(live==1)&&(sex==1))
{
printf("the person should be insuredn");
printf("the premium rate=Rs. 4 per thousandn");
printf("maximum amount =Rs. 2 lakhsn");
}
else if((H==1)&&((age<=35)&&(age>=25))&&(live==1)&&(sex==0))
{
printf("the person should be insuredn");
printf("the premium rate=Rs. 3 per thousandn");
printf("maximum amount =Rs. 1 lakhsn");
}
else if((H==0)&&((age<=35)&&(age>=25))&&(live==0)&&(sex==1))
{
17. printf("the person should be insuredn");
printf("the premium rate=Rs. 6 per thousandn");
printf("maximum amount =Rs. 10000n");
}
else
printf("person should not be insuredn");
}
(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 steeln");
scanf("%f",&hard);
printf("Enter the carbon content in the steeln");
scanf("%f",&carbon);
printf("enter the value of tensile strength of the steeln");
scanf("%f",&tens);
if((hard>50)&&(carbon<0.7)&&(tens>5600))
printf("grade of steel is 10n");
if((hard>50)&&(carbon<0.7)&&(tens<=5600))
printf("grade of sttel is 9n");
if((hard<50)&&(carbon<0.7)&&(tens>5600))
printf("grade of steel is 8n");
if((hard>50)&&(carbon>0.7)&&(tens>5600))
printf("grade of steel is 7n");
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 6n");
18. if((hard<50)&&(carbon>0.7)&&(tens<5600))
printf("grade of steel is 5n");
}
(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 bookn");
scanf("%d",&day);
if(day<=5)
{
printf("fine = 50 paisen");
printf("your membership will not be cancelledn");
}
else if(day<=10)
{
printf("fine = 1 rupeen");
printf("your membership will not be cancelledn");
}
else if(day<=30)
{
printf("fine = 5 rupeesn");
printf("your membership will not be cancelledn");
}
else if(day>30)
printf("your membership will be cancelledn");
}
19. (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 trianglen");
scanf("%d%d%d",&s1,&s2,&s3);
if((s1>=s2 && s2>=s3)||(s1>=s3 && s3>=s2))
{
if(s2+s3>s1)
printf("triangle is validn");
else
printf("triangle is not validn");
}
if((s2>=s1 && s1>=s3)||(s2>=s3 && s3>=s1))
{
if(s1+s3>s2)
printf("triangle is validn");
else
printf("triangle is not validn");
}
if((s3>=s1 && s1>=s2)||(s3>=s2 && s2>=s1))
{
if(a+b>c)
printf("triangle is validn");
else
printf("triangle is not validn");
}
}
20. (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 trianglen");
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 trianglen");
if(((a==b)&&(b!=c))||((b==c)&&(c!=a))||((c==a)&&(a!=b)))
printf("the triangle is an isoscales trianglen");
if((a!=b)&&(b!=c)&&(c!=a))
printf("the triangle is scalene trianglen");
if((d==0)||(e==0)||(f==0))
printf("the triangle is a right angled trianglen");
}
(h).
/*to find the efficiency of worker*/
#include<stdio.h>
void main()
{
float time;
printf("Enter the time taken by the workern");
scanf("%f",&time);
if((time>=2)&&(time<3))
printf("the worker is highly efficientn");
21. if((time>=3)&&(time<4))
printf("the worker is ordered to improve speedn");
if((time>=4)&&(time<5))
printf("the worker is given tranning to improve the speedn");
if(time>=5)
printf("the worker has to leave the companyn");
if(time<2)
printf("for any worker this is not possiblen");
}
(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 Bn");
scanf("%f%f",&A,&B);
if((A>=55)&&(B>=45))
printf("the student has passedn");
else if((A<55)&&(B>=55)&&(A>=45))
printf("the student has passedn");
else if((B<45)&&(A>=65))
printf("To qualify the student is allowed to reappear in an
examinationn");
else
printf("the student is decleared to have failedn");
}
(j).
/*To find the goods will be supply or not*/
#include<stdio.h>
22. void main()
{
int a,b;
printf("write the credit of the customer wheather it is OK or not(1 for OK & 0
for NOT OKn");
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 notn");
scanf("%d",&b);
if((a==1)&&(b==1))
printf("supply has requirementn");
else if(((a==0)&&(b==1))||((a==0)&&(b==0)))
printf("do not supplyn");
else if((a==1)&&(b==0))
printf("supply what is in the stock rest will supply latern");
}
Conditional operators
[G]
(a).
unpredictable : no. Not initialised
(b).
200
(c).
Welcome
23. [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.
25. [J]
(a).
#include<stdio.h>
void main()
{
char c;
printf("Enter any charactern");
scanf("%c",&c);
c<=122&&c>=97 ? printf("Entered character is lower case alphabetn") :
printf("Entered character is not a lower case alphabetn");
c<=47&&c>=0||c>=58&&c<=64||c>=91&&c<=96||c>=123&&c<=127 ?
printf("Entered character is a special symboln") :
printf("Entered character is not a special symboln");
}
(b).
#include<stdio.h>
void main()
{
int year;
printf("Enter the yearn");
scanf("%d",&year);
year%4==0 ? printf("Entered year is a leap yearn") : printf("Entered year is
not a leap yearn");
}
(c).
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter three numbersn");