SlideShare a Scribd company logo
PROGRAMMING IN ANSI C: Chapter 5
DECISION MAKING AND BRANCHING
REVIEW QUESTION:
RQ-5.1: State whether the following are true or false:
(a)When if statements are nested, the last else gets associated with the nearest
if without an else.
Ans: False.
(b)One if can have more than one else clause.
Ans: False.
(c)A switch statement can always be replaced by a series of if..else statements.
Ans: False.
(d)A switch expression can be of any type.
Ans: False.
(e)A program stops its execution when a break statement is encountered.
Ans: False.
(f)Each expression in the else if must test the same variable.
Ans: True.
(g)Any expression can be used for the if expression.
Ans: True.
(h)Each case label can have only one statement.
Ans: True.
(i)The default case is required in the switch statement.
Ans: True.
(j)The predicate !( (x>=10) (y==5) ) is equivalent to (x<10) && (y!=5 ).
Ans: True.
RQ-5.2:Fill in the blanks in the following statements:
(a)The ……….operator is true only when both the operands are true.
Ans: logical AND (&&).
(b)Multiway section can be accomplished using an else if statement or the .
……………statement.
Ans: switch.
(c)The……….. statement when executed in a switch statement causes.
immediate exit from the structure
Ans: break.
(d)The ternary conditional expression using the operator ?: code be easily
coded using ………..statement.
Ans: if…else.
(e)The expression !(x!=y)can be replaced by the expression…………
Ans: x==y.
RQ-5.3:Find errors, if any, in each of the following segments:
Solution:
(a)if((x+y=z) && (y>0) )
printf(" ");
Ans: Error.
Correct ans: if((x+y==z) && (y>0) )
printf(" ");
(b) if (code >1)
a= b+c
else
a=0
Ans: Error.
Correct ans: if (code >1)
a= b+c;
else
a=0;
(c) if(p>0) || (q <0)
printf("Sign is negative”);
Ans: Error.
Correct ans:if((p>0) || (q <0))
printf("Sign is negative”);
RQ-5.4:The following is a segment of a program:
x=1;
y=1;
if(n>0)
x=x+1;
y=y-1;
printf("%d %d", x,y);
what will be the values of x and y if n assumes a value of (a) 1and (b) 0.
Solution:
(a)The value of x is 2 & y is 0.
(b)The value of x & y is imaginary.
RQ-5.5:Rewrite each of the following without using compound relations:
(a) if(grade<=59&&grade>=50)
second=second+1;
Solution:
if(grade<=59)
second=second+1;
if(grade>=50)
second=second+1;
(b) if ( number>100||number<0)
printf(“Out of range”);
else
sum=sum+number;
Solution:
if ( number>100)
printf(“Out of range”);
else if(number<0)
printf(“Out of range”);
else
sum=sum+number;
(c) if (M1>60&&M2>60||T>200)
printf(“Admittedn”);
else
printf (“Not admitted”);
Solution:
if (M1>60)
printf (“Admittedn”);
if (M2>60)
printf (“Admittedn”);
else if(T>200)
printf (“Admittedn”);
else
printf (“Not admitted”);
RQ-5.6:Assuming x=10 ,state whether the following logical expressions are true
or false:
(a)x==10 && x>10 && !x Ans:False.
(b)x==10 || x> 10 && !x Ans:True.
(c)x==10 && x>10 ||!x Ans:False.
(d)x==10 ||x>10 || !x Ans:True.
RQ-5.7:Find errors,if any, in the following switch related statements.Assume
that the variables x and y are of int type and x=1 and y=2.
Solution:
(a)switch(y);
Ans: Error.
Correct ans: switch(y)
(b)case 10;
Ans: Error.
Correct ans: case 10:
(c)switch(x+y)
Ans:No error.
(d)switch(x) {Case 2: y= x+y; break};
Ans: Error.
Correct ans: switch(x) {Case 2: y= x+y; break;}
RQ-5.8:Simplify the following compound logical expressions:
(a) !(x<=10) (b)!(x==10)||!((y==5)||(z<0))
Ans:(x>10) Ans: (x>0)
(c)!((x+y==z)&&!(z>5)) (d)!((x<=5)&&(y==10)&&(z<5))
Ans: (x<z) Ans: (x>5)
RQ-5.9:Assuming that x=5, y=0,and z=1 initially ,what will be their values after
executing the following code segments?
(a)if(x && y)
x=10;
else
y=10;
Output:
10
10
(b)if(x|| y ||z)
y=10;
else
z=0;
Output:
1
0
(c)if(x)
if(y)
z=10;
else
z=0;
Output:
10
0
(d)if(x ==0 || x && y)
if(!y)
z=0;
else
y=1;
Output:
0
1
RQ-5.10:Assuming that x=2,y=1 and z=0 initially ,what will be their values
after executing the following code segments?
(a)
switch(x)
{
case 2:
x=1;
y=x+1;
case 1:
x=0;
break;
default:
x=1;
y=0;
}
Output:
1
0
(b)
switch(y)
{
case 0:
x=0;
y=0;
case 2:
x=2;
z=2;
default:
x=1;
y=2;
}
Output:
0 0 0
RQ-5.11:Find the error ,if any,in the following statements:
Solution:
(a)if(x>=10)
printf("n");
Ans: No error.
(b)if(x>=10)
printf("OK");
Ans: No error.
(c)if(x==10)
printf ("Good");
Ans : No error.
(d)if(x=<10)
printf("Welcome");
Ans : Error.
Correct ans: if(x<=10)
Printf(“Welcome”);
RQ-5.12:What is the output of the following program?
Program:
main()
{
int m=5;
if(m<3) printf("%d", m+1);
else if (m<5) printf("%d", m+2);
else if (m<7) printf("%d", m+3);
else printf("%d", m+4);
getch();
}
Output:
8
RQ-5.13:What is the output of the following program?
Program:
main ()
{
int m=1;
if( m==1)
{
printf ("Delhi");
if(m==2)
printf("Chennai");
else
printf("Banglore");
}
else
Printf("END");
getch();
}
Output:
1
Delhi
2
Chennai
3
Banglore
RQ-5.14:What is the output of the following program?
Program:
main()
{
int m;
for(m=1; m<5; m++)
printf("%dn",(m%2) ? m : m*2);
getch();
}
Output:
1 4 3 8
RQ-5.15:What is the output of following program?
Program:
main()
{
int m,n,p;
for(m=0; m<3;m++)
for(n=0;n<3;n++)
for(p=0;p<3;p++)
if(m+n+p==2)
goto print;
print:
printf("%d %d %d",m,n,p);
getch();
}
Output:
0 0 2
RQ-5.16:What will be the value of x when the following segment is executed?
int x=10,y=15;
x= (x<y)? (y+x) : (y-x) ;
Solution:
The value of x after execution is :-25.
RQ-5.17:What will be the output when the following segment is executed?
int x=0;
if(x>=0)
if(x>0)
printf("Number is positive");
else
printf("Number is negative");
Output:
0
Number is positive
1
Number is negative
RQ-5.18: What will be the output when the following segment is executed?
Program:
char ch = ‘a’
switch(ch)
{
case ‘a’:
printf(“A”);
case ‘b’:
printf(“B”);
case ‘c’:
printf(“C”);
}
Output:
a
A
b
B
c
C
RQ-5.19:What will be the output of the following segment when executed?
Program:
main()
{
int x=10,y=20;
if(( x<y)|| (x+5)>10)
printf("%d",x);
else
printf("%d",y);
getch();
}
Output:
10
RQ-5.20:What will be the output of the following segment when executed?
Program:
main()
{
int a=10, b=5;
if(a>b)
{
if(b>5)
printf("%d",b);
}
else
printf("%d",a);
getch();
}
Output:
10
CHAPTER 5
Decision Making and Branching
EXERCISE-5.1 Write a program to determine whether a given number is odd or
even and print the message:
NUMBER IS EVEN or NUMBER IS ODD
(a) without using else option, and (b) with using else option.
Solution:
(a) without using else option:
/*………………even or odd……………*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“Enter a numbern”)
scanf("%d",&n);
if(n%2==0)
printf("NUMBER IS EVEN ");
if(n%2==1)
printf("NUMBER IS ODD ");
getch();
}
(b) with else option:
/*………………even or odd……………*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“Enter a numbern”)
scanf("%d",&n);
if(n%2==0)
printf("Even");
else
printf("Odd");
getch();
}
EXERCISE-5.2 Write a program to find the number of and sum of all integers
greater than 100 and less than 200 that are divisible by 7.
Solution:
/*…..number between 100-200 divisible by 7……*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,r,sum;
sum=0;
clrscr();
for(i=100;i<=200;i++)
{
r=i%7;
if(r==0)
{
printf(" %d",i);
sum=sum+i;
}
printf(“Sum=%d”,sum);
}
getch();
}
EXERCISE-5.3 A set of two linear equations with two unknowns x1 and x2 is
given below:
ax1 +bx2=m and cx1+dx2=n
The set has unique solution
x1= and x2=
provided the determinate ad-cb is not equal to zero.
Write a program that will read the values of constants a,b,c,d,m and n and
compute the values of x1 and x2 .An appropriate message should be printed if
ad-cb=0.
Solution:
/*…….two linear equation………*/
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c,d,m,n,x1,x2;
clrscr();
printf("Input a,b,c,d,m,n:n");
scanf("a=%f b=%f c=%f d=%f m=%f
n=%f",&a,&b,&c,&d,&m,&n);
x1=(m*d-b*n)/(a*d-c*b);
x2=(n*a-m*c)/(a*d-c*b);
if((a*d-c*b)!=0)
printf("x1=%f x2= %f",x1,x2);
else
printf("The value is infinity.n");
getch();
}
EXERCISE-5.4 Given a list of marks ranging from 0 to 100, write a program to
print number of students:
(a)Who have obtained more than 80 marks, (b) who have obtained more
than 60 marks,
(c)Who have obtained more than 40 marks, (d) who have obtained 40 or
less marks,
(e)In the range 81 to 100, (f) in the range 61 to 80,
(g)in the range 41 to 60, and (h) in the range 0 to 40.
The program should use a minimum numbers of if statements.
Solution:
/*….marks obtain……*/
#include<stdio.h>
#include<conio.h>
void main()
{
int marks,count,a,b,c,d,i;
a=0; b=0; c=0;d=0;
clrscr();
printf("Input 20 boy's marksn");
for(i=1;i<=20;i++)
{
scanf("%d",&marks);
if(marks>80)
a++;
else if(marks>60)
b++;
else if(marks>40)
c++;
else if(marks<=40)
d++;
}
printf("Number of students who have obtained more than 80
marks=%dnNumber of
students who have obtained more than 60 marks=%dn Number of students who
have obtained more than 40 marks=%dn Number of students who have
obtained 40 or less marks=%d",a,b,c,d);
getch();
}
EXERCISE-5.5 Admission to a professional course is subjects to the following
conditions:
(a) Marks in Mathematics>=60
(b) Marks in Physics>=50
(c) Marks in Chemistry>=40
(d) Total in all three subjects>=200 or
Total in Mathematics and Physics>=150
Given the marks in the three subjects, write a program to process the
applications to list the eligible candidates.
Solution:
/*……..admission for a professional course……*/
#include<stdio.h>
#include<conio.h>
void main()
{
int r,m,c,p,b;
clrscr();
printf("Input Mathmatics,Physics and Chemistry");
scanf("%d%d%d",&m,&p,&c);
r=m+p+c;
b=m+p;
if(m>=60&&p>=50&&c>=40&&r>=200&&b>=150)
printf("The candidate is eligible");
else
printf("The candidate is not eligible");
getch();
}
EXERCISE-5.7: Shown below is a Floyd’s triangle .
1
2 3
4 5 6
7 8 9 10
11………..15
79........ .. .. .. .. ..91
(a) Write a program to print this triangle.
Solution:
/*……….Floyd’s triangle………..*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,count,n;
clrscr();
count=0;
printf("nnHow many rows of Floyd triangle: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
count++;
printf("%d",count);
printf(" ");
}
printf("n");
}
getch();
}
(b) Modify the program the following from of Floyd’s triangle.
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
Solution:
/*……….Floyd’s triangle………..*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,count,n;
clrscr();
count=0;
printf("nnHow many rows of Floyd triangle: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=2;j<=i+1;j++)
{
printf("%d",(i+j)%2);
printf(" ");
}
printf("n");
}
getch();
}
EXERCISE-5.8 A cloth showroom has announced the following seasonal
discounts on purchase of items:
Purchase amount Discount
Mill cloth
Handloom items
0-100
5%
101-200 5%
7.5%
201-300 7.5%
10.0%
Above300 10.0%
15.0%
Write a program using switch and if statements to compute the net amount to be
paid by a coustomer.
Solution:
/*………….marketing of a showroom………………*/
#define MC1 0
#define MC2 0.05
#define MC3 0.075
#define MC4 0.10
#define HI1 0.05
#define HI2 0.075
#define HI3 0.10
#define HI4 0.15
#include<stdio.h>
#include<conio.h>
void main()
{
float price,net,discount;
int level,jobnumber;
clrscr();
input:
printf("Enter level jobnumber and purchase amountn");
printf("Enter zero for level to Endn");
scanf("%d%d%f",&level,&jobnumber,&price);
if(level==0) goto stop;
if(0<=price<=100)
level=1;
else if(101<=price<=200)
level=2;
else if(201<=price<=300)
level=3;
else
level=4;
switch(level)
{
case 1:
discount=MC1+HI1;
break;
case 2:
discount=MC2+HI2;
break;
case 3:
discount=MC3+HI3;
break;
case 4:
discount=MC4+HI4;
break;
default:
printf("Error in level coden");
goto stop;
}
net=price-(price*discount);
printf("Net amount=%fn",net);
goto input;
stop:printf("nnEND OF THE PROGRAM");
getch();
}
EXERCISE-5.9 Write a program that will read the value of x and evaluate the
following function
y=
using
(a) nested if statements.
(b) else if statements and
(c) conditional operator ?
Solution:
/*…………evaluate the equation………..*/
(a)nested if statements:
#include<stdio.h>
#include<conio.h>
void main()
{
float x,y;
clrscr();
printf("Input xn");
scanf("%f",&x);
if(x!=0)
{
if(x>0)
printf("y=1");
if(x<0)
printf("y=-1");
}
if(x==0)
printf("y=0");
getch();
}
(b)else if statements:
#include<stdio.h>
#include<conio.h>
void main()
{
float x,y;
clrscr();
printf("Input xn");
scanf("%f",x);
if(x!=0)
{
if(x>0)
{
printf("1");
}
else
printf("-1");
}
else
printf("0");
getch();
}
(c)conditional operator:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
float y,x;
printf("Input xn");
scanf("%f",&x);
y=(x!=0)?((x>0)?1:-1):0;
printf("%d",y);
getch();
}
EXERCISE-5.10 Write a program to compute the real roots of a quadratic
equation
ax2+bx2+c=0
The roots are given by the equtions:
x1 and x2
The program should request for the values of the constants a,b and c print the
values of x1
and x2.Use the following:
(a) No solution, if both a and b are zero
(b) There is only one root if a=0(x=-c/b)
(c) There are no real roots, if b2-4ac is negative
(d) Otherwise, there no real roots
Test your program with appropriate data so that all logical paths are working as
per your design. Incorporate appropriate output messages.
Solution:
/*…….roots of quadratic equation ….*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,x,discriminant,root1,root2;
clrscr();
printf("Input values of a, b and cn");
scanf("%f %f %f",&a,&b,&c);
discriminant=b*b-4*a*c;
if(a==0&&b==0)
printf("No solutionn");
else if(a==0)
{
x=-(c/b);
printf("x=%f",x);
}
else if(discriminant<0)
printf("Roots are imaginaryn");
else
{
root1=-b+sqrt(discriminant)/2*a;
root2=-b-sqrt(discriminant)/2*a;
printf("Root1=%f Root2=%f",root1,root2);
}
getch();
}
EXERCISE-5.11: Write a program to read three integer values from the
keyboard and displays the output stating that they are the sides of right-angled
triangle.
Solution:
/*………………right-angled triangle……..*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,x,y,z;
clrscr();
printf("Input three integer values a b and cn");
scanf("%d%d%d",&a,&b,&c);
x=a*a;
y=b*b;
z=c*c;
if(a>b&&a>c&&(x==y+z))
printf("The values are sides of right-angled triangle");
else if(b>a&&b>c&&(y==x+z))
printf("The values are sides of right-angled triangle");
else if(c>a&&c>b&&z==x+y)
printf("The values are sides of right-angled triangle");
else
printf("The values are not sides of right-angled triangle");
getch();
EXERCISE-5.12: An electricity board charges the following rates for the use of
electricity:
For the first 200 units: 80 per unit
For the next 100 units: 90per unit
Beyond 300 units: Rs.1.00 per unit
All users are charged a minimum of Rs. 100 as meter charge. If the total amount
is more than Rs.400, then an additional surcharge of 15% of total amount is
charged. Write a program to read the names of users and number of units
consumed and print out the charges with names.
Solution:
/*………….pay bill…………..*/
#include<stdio.h>
#include<conio.h>
void main()
{
float units,total,net;
char name;
clrscr();
printf("Input users name and unitsn");
scanf("%s %f",&name,&units);
{
if(units<=200)
total=100+0.80*units;
else if(units<=300)
total=100+0.90*units;
else if(units>300)
total=100+1.00*units;
}
if(total>400)
{
net=total+total*0.15;
printf("Total=%f",net); }
else
printf("Total=%f",total);
getch();
}
EXERCISE-5.13: Write a program to compute and display the sum of all
integers that are divisible by 6 but not divisible by 4 and lie between 0 to 100.
The program should also count and display the number of such values.
Solution:
/*……numbers between 0-100 divisible by 6 but not divisible by 4….*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,count;
count=0;
clrscr();
for(i=0;i<=100;i++)
{
if(i%6==0&&i%4!=0)
{
count=count+1;
printf(" %d",i);
}
}
printf(“n”);
printf("count=%d",count);
getch();
}
EXERCISE-5.14 Write an interactive program that could read a positive integer
number and decide whether the number is a prime number display the output
accordingly. Modify the program to count all prime numbers that lie 100 to 200.
[Note: A prime number is positive integer that is divisible only by 1 or by itself]
Solution:
/*………….prime number …………*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,count;
count=0;
clrscr();
printf("nnSeries of prime number from 100 to 200:n");
for(i=100;i<=200;i++)
{
for(j=2;j<=i;j++)
{
if(i%j==0)
break;
}
if(i==j)
{
printf("%4dn",i);
count+=1;
}
}
printf("The countable number is: %d",count);
getch();
}
EXERCISE-5.15: Write a program read a double-type value x that represents
angle in radians and a character-type variable t that represents the type of
trigonometric function and display the value of
(a) sin(x), if s or S is a assigned to T,
(b) cos(x), if c or C is assigned to T, and
(c) tan(x), if t or T is assigned to T
Using (i) if…else statement and (ii) switch statement.
Solution-1:
(i)if…else statement :
/*……………trigonometric function……….*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<ctype.h>
void main()
{
int x,c,s,d,t; clrscr();
float r,result;
s=1;
c=2;
t=3;
printf(“Input the value of x and character valuen”);
scanf("%d",&x);
r=x*(180/3.1416);
scanf("%d",&d);
{
if(d==1)
result=sin(r);
else if(d==2)
result=cos(r);
else if(d==3)
result==tan(r);
else
printf("no response.");
}
printf("n%f",result);
getch();
}
Solution-2:
(ii) switch statement:
/*………….trigonometric function…………..*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,x;
float v,r;
char t;
clrscr();
printf("Input the value of xn");
scanf("%d",&x);
r=x*(180/3.1416);
printf("Input charecter");
scanf("%c",&t);
switch(t)
{
case 's':
case 'S':
v=sin(r);
case 'c':
case 'C':
v=cos(r);
case 't':
case 'T':
v=tan(r);
}
printf("%f",v);
getch()}
Reference:
http://hstuadmission.blogspot.com/2010/12/solution-programming-in-ansi-c-
chapter.html

More Related Content

What's hot

The solution manual of programming in ansi by Robin
The solution manual of programming in ansi by RobinThe solution manual of programming in ansi by Robin
The solution manual of programming in ansi by Robin
Shariful Haque Robin
 
Let us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solutionLet us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solution
rohit kumar
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
Hazrat Bilal
 
Let us c chapter 4 solution
Let us c chapter 4 solutionLet us c chapter 4 solution
Let us c chapter 4 solution
rohit kumar
 
Let us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solutionLet us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solution
Hazrat Bilal
 
PPS Notes Unit 5.pdf
PPS Notes Unit 5.pdfPPS Notes Unit 5.pdf
PPS Notes Unit 5.pdf
Sreedhar Chowdam
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
Abdullah Al Naser
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
KavyaSharma65
 
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
rohit kumar
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
Wingston
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
Rahul Pandit
 
Call by value
Call by valueCall by value
Call by valueDharani G
 
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solutionLet us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Hazrat Bilal
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
RAJWANT KAUR
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
RAJ KUMAR
 
String C Programming
String C ProgrammingString C Programming
String C Programming
Prionto Abdullah
 
C functions
C functionsC functions

What's hot (20)

The solution manual of programming in ansi by Robin
The solution manual of programming in ansi by RobinThe solution manual of programming in ansi by Robin
The solution manual of programming in ansi by Robin
 
Let us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solutionLet us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solution
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
 
Let us c chapter 4 solution
Let us c chapter 4 solutionLet us c chapter 4 solution
Let us c chapter 4 solution
 
Let us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solutionLet us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solution
 
PPS Notes Unit 5.pdf
PPS Notes Unit 5.pdfPPS Notes Unit 5.pdf
PPS Notes Unit 5.pdf
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
 
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
Call by value
Call by valueCall by value
Call by value
 
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solutionLet us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
Function
FunctionFunction
Function
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
C functions
C functionsC functions
C functions
 
Function in c
Function in cFunction in c
Function in c
 

Similar to Chapter 5 Balagurusamy Programming ANSI in c

(Www.entrance exam.net)-tcs placement sample paper 2
(Www.entrance exam.net)-tcs placement sample paper 2(Www.entrance exam.net)-tcs placement sample paper 2
(Www.entrance exam.net)-tcs placement sample paper 2Pamidimukkala Sivani
 
C Programming Example
C Programming Example C Programming Example
C Programming Example
University of Potsdam
 
important C questions and_answers praveensomesh
important C questions and_answers praveensomeshimportant C questions and_answers praveensomesh
important C questions and_answers praveensomesh
praveensomesh
 
C programs
C programsC programs
C programs
Vikram Nandini
 
BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
Sanjjaayyy
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
happycocoman
 
programs of c www.eakanchha.com
 programs of c www.eakanchha.com programs of c www.eakanchha.com
programs of c www.eakanchha.com
Akanchha Agrawal
 
Decisions in C or If condition
Decisions in C or If conditionDecisions in C or If condition
Decisions in C or If condition
yarkhosh
 
Review questions and answers
Review questions and answersReview questions and answers
Review questions and answers
IIUM
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
Munazza-Mah-Jabeen
 
C aptitude scribd
C aptitude scribdC aptitude scribd
C aptitude scribdAmit Kapoor
 
Qust & ans inc
Qust & ans incQust & ans inc
Qust & ans incnayakq
 
Tech-1.pptx
Tech-1.pptxTech-1.pptx
Tech-1.pptx
HussainPashaShaik1
 

Similar to Chapter 5 Balagurusamy Programming ANSI in c (20)

(Www.entrance exam.net)-tcs placement sample paper 2
(Www.entrance exam.net)-tcs placement sample paper 2(Www.entrance exam.net)-tcs placement sample paper 2
(Www.entrance exam.net)-tcs placement sample paper 2
 
C Programming Example
C Programming Example C Programming Example
C Programming Example
 
important C questions and_answers praveensomesh
important C questions and_answers praveensomeshimportant C questions and_answers praveensomesh
important C questions and_answers praveensomesh
 
C programs
C programsC programs
C programs
 
Revision1schema C programming
Revision1schema C programmingRevision1schema C programming
Revision1schema C programming
 
BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
BCSL 058 solved assignment
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
Revision1 C programming
Revision1 C programmingRevision1 C programming
Revision1 C programming
 
programs of c www.eakanchha.com
 programs of c www.eakanchha.com programs of c www.eakanchha.com
programs of c www.eakanchha.com
 
Decisions in C or If condition
Decisions in C or If conditionDecisions in C or If condition
Decisions in C or If condition
 
Review questions and answers
Review questions and answersReview questions and answers
Review questions and answers
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Progr3
Progr3Progr3
Progr3
 
Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
 
C aptitude scribd
C aptitude scribdC aptitude scribd
C aptitude scribd
 
Qust & ans inc
Qust & ans incQust & ans inc
Qust & ans inc
 
Tech-1.pptx
Tech-1.pptxTech-1.pptx
Tech-1.pptx
 
3 flow
3 flow3 flow
3 flow
 
3 flow
3 flow3 flow
3 flow
 

More from BUBT

Lab report cover page
Lab report cover page Lab report cover page
Lab report cover page
BUBT
 
Project report title cover page
Project report title cover pageProject report title cover page
Project report title cover page
BUBT
 
Implementation Of GSM Based Fire Alarm and Protection System
Implementation Of GSM Based Fire Alarm and Protection SystemImplementation Of GSM Based Fire Alarm and Protection System
Implementation Of GSM Based Fire Alarm and Protection System
BUBT
 
Student Attendance
Student AttendanceStudent Attendance
Student Attendance
BUBT
 
Reasoning for Artificial Intelligence Expert
Reasoning for Artificial Intelligence ExpertReasoning for Artificial Intelligence Expert
Reasoning for Artificial Intelligence Expert
BUBT
 
Auto Room Lighting and Door lock Report
Auto Room Lighting and Door lock ReportAuto Room Lighting and Door lock Report
Auto Room Lighting and Door lock Report
BUBT
 
Auto Room Lighting System
Auto Room Lighting SystemAuto Room Lighting System
Auto Room Lighting System
BUBT
 
Doorlock
DoorlockDoorlock
Doorlock
BUBT
 
Ultra Dense Netwok
Ultra Dense NetwokUltra Dense Netwok
Ultra Dense Netwok
BUBT
 
Bangladesh University of Business and Technology
Bangladesh University of Business and Technology Bangladesh University of Business and Technology
Bangladesh University of Business and Technology
BUBT
 
Art Gallery Management System
Art Gallery Management SystemArt Gallery Management System
Art Gallery Management System
BUBT
 
Shop management system
Shop management systemShop management system
Shop management system
BUBT
 

More from BUBT (12)

Lab report cover page
Lab report cover page Lab report cover page
Lab report cover page
 
Project report title cover page
Project report title cover pageProject report title cover page
Project report title cover page
 
Implementation Of GSM Based Fire Alarm and Protection System
Implementation Of GSM Based Fire Alarm and Protection SystemImplementation Of GSM Based Fire Alarm and Protection System
Implementation Of GSM Based Fire Alarm and Protection System
 
Student Attendance
Student AttendanceStudent Attendance
Student Attendance
 
Reasoning for Artificial Intelligence Expert
Reasoning for Artificial Intelligence ExpertReasoning for Artificial Intelligence Expert
Reasoning for Artificial Intelligence Expert
 
Auto Room Lighting and Door lock Report
Auto Room Lighting and Door lock ReportAuto Room Lighting and Door lock Report
Auto Room Lighting and Door lock Report
 
Auto Room Lighting System
Auto Room Lighting SystemAuto Room Lighting System
Auto Room Lighting System
 
Doorlock
DoorlockDoorlock
Doorlock
 
Ultra Dense Netwok
Ultra Dense NetwokUltra Dense Netwok
Ultra Dense Netwok
 
Bangladesh University of Business and Technology
Bangladesh University of Business and Technology Bangladesh University of Business and Technology
Bangladesh University of Business and Technology
 
Art Gallery Management System
Art Gallery Management SystemArt Gallery Management System
Art Gallery Management System
 
Shop management system
Shop management systemShop management system
Shop management system
 

Recently uploaded

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 

Recently uploaded (20)

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 

Chapter 5 Balagurusamy Programming ANSI in c

  • 1. PROGRAMMING IN ANSI C: Chapter 5 DECISION MAKING AND BRANCHING REVIEW QUESTION: RQ-5.1: State whether the following are true or false: (a)When if statements are nested, the last else gets associated with the nearest if without an else. Ans: False. (b)One if can have more than one else clause. Ans: False. (c)A switch statement can always be replaced by a series of if..else statements. Ans: False. (d)A switch expression can be of any type. Ans: False. (e)A program stops its execution when a break statement is encountered. Ans: False. (f)Each expression in the else if must test the same variable. Ans: True. (g)Any expression can be used for the if expression. Ans: True. (h)Each case label can have only one statement. Ans: True. (i)The default case is required in the switch statement. Ans: True. (j)The predicate !( (x>=10) (y==5) ) is equivalent to (x<10) && (y!=5 ). Ans: True. RQ-5.2:Fill in the blanks in the following statements:
  • 2. (a)The ……….operator is true only when both the operands are true. Ans: logical AND (&&). (b)Multiway section can be accomplished using an else if statement or the . ……………statement. Ans: switch. (c)The……….. statement when executed in a switch statement causes. immediate exit from the structure Ans: break. (d)The ternary conditional expression using the operator ?: code be easily coded using ………..statement. Ans: if…else. (e)The expression !(x!=y)can be replaced by the expression………… Ans: x==y. RQ-5.3:Find errors, if any, in each of the following segments: Solution: (a)if((x+y=z) && (y>0) ) printf(" "); Ans: Error. Correct ans: if((x+y==z) && (y>0) ) printf(" "); (b) if (code >1) a= b+c else a=0 Ans: Error.
  • 3. Correct ans: if (code >1) a= b+c; else a=0; (c) if(p>0) || (q <0) printf("Sign is negative”); Ans: Error. Correct ans:if((p>0) || (q <0)) printf("Sign is negative”); RQ-5.4:The following is a segment of a program: x=1; y=1; if(n>0) x=x+1; y=y-1; printf("%d %d", x,y); what will be the values of x and y if n assumes a value of (a) 1and (b) 0. Solution: (a)The value of x is 2 & y is 0. (b)The value of x & y is imaginary. RQ-5.5:Rewrite each of the following without using compound relations: (a) if(grade<=59&&grade>=50) second=second+1; Solution: if(grade<=59) second=second+1;
  • 4. if(grade>=50) second=second+1; (b) if ( number>100||number<0) printf(“Out of range”); else sum=sum+number; Solution: if ( number>100) printf(“Out of range”); else if(number<0) printf(“Out of range”); else sum=sum+number; (c) if (M1>60&&M2>60||T>200) printf(“Admittedn”); else printf (“Not admitted”); Solution: if (M1>60) printf (“Admittedn”); if (M2>60) printf (“Admittedn”); else if(T>200) printf (“Admittedn”); else
  • 5. printf (“Not admitted”); RQ-5.6:Assuming x=10 ,state whether the following logical expressions are true or false: (a)x==10 && x>10 && !x Ans:False. (b)x==10 || x> 10 && !x Ans:True. (c)x==10 && x>10 ||!x Ans:False. (d)x==10 ||x>10 || !x Ans:True. RQ-5.7:Find errors,if any, in the following switch related statements.Assume that the variables x and y are of int type and x=1 and y=2. Solution: (a)switch(y); Ans: Error. Correct ans: switch(y) (b)case 10; Ans: Error. Correct ans: case 10: (c)switch(x+y) Ans:No error. (d)switch(x) {Case 2: y= x+y; break}; Ans: Error. Correct ans: switch(x) {Case 2: y= x+y; break;} RQ-5.8:Simplify the following compound logical expressions: (a) !(x<=10) (b)!(x==10)||!((y==5)||(z<0)) Ans:(x>10) Ans: (x>0) (c)!((x+y==z)&&!(z>5)) (d)!((x<=5)&&(y==10)&&(z<5))
  • 6. Ans: (x<z) Ans: (x>5) RQ-5.9:Assuming that x=5, y=0,and z=1 initially ,what will be their values after executing the following code segments? (a)if(x && y) x=10; else y=10; Output: 10 10 (b)if(x|| y ||z) y=10; else z=0; Output: 1 0 (c)if(x) if(y) z=10; else z=0; Output: 10 0 (d)if(x ==0 || x && y) if(!y)
  • 7. z=0; else y=1; Output: 0 1 RQ-5.10:Assuming that x=2,y=1 and z=0 initially ,what will be their values after executing the following code segments? (a) switch(x) { case 2: x=1; y=x+1; case 1: x=0; break; default: x=1; y=0; } Output: 1 0 (b) switch(y) {
  • 8. case 0: x=0; y=0; case 2: x=2; z=2; default: x=1; y=2; } Output: 0 0 0 RQ-5.11:Find the error ,if any,in the following statements: Solution: (a)if(x>=10) printf("n"); Ans: No error. (b)if(x>=10) printf("OK"); Ans: No error. (c)if(x==10) printf ("Good"); Ans : No error. (d)if(x=<10) printf("Welcome"); Ans : Error.
  • 9. Correct ans: if(x<=10) Printf(“Welcome”); RQ-5.12:What is the output of the following program? Program: main() { int m=5; if(m<3) printf("%d", m+1); else if (m<5) printf("%d", m+2); else if (m<7) printf("%d", m+3); else printf("%d", m+4); getch(); } Output: 8 RQ-5.13:What is the output of the following program? Program: main () { int m=1; if( m==1) { printf ("Delhi"); if(m==2) printf("Chennai");
  • 10. else printf("Banglore"); } else Printf("END"); getch(); } Output: 1 Delhi 2 Chennai 3 Banglore RQ-5.14:What is the output of the following program? Program: main() { int m; for(m=1; m<5; m++) printf("%dn",(m%2) ? m : m*2); getch(); } Output: 1 4 3 8
  • 11. RQ-5.15:What is the output of following program? Program: main() { int m,n,p; for(m=0; m<3;m++) for(n=0;n<3;n++) for(p=0;p<3;p++) if(m+n+p==2) goto print; print: printf("%d %d %d",m,n,p); getch(); } Output: 0 0 2 RQ-5.16:What will be the value of x when the following segment is executed? int x=10,y=15; x= (x<y)? (y+x) : (y-x) ; Solution: The value of x after execution is :-25. RQ-5.17:What will be the output when the following segment is executed? int x=0;
  • 12. if(x>=0) if(x>0) printf("Number is positive"); else printf("Number is negative"); Output: 0 Number is positive 1 Number is negative RQ-5.18: What will be the output when the following segment is executed? Program: char ch = ‘a’ switch(ch) { case ‘a’: printf(“A”); case ‘b’: printf(“B”); case ‘c’: printf(“C”); } Output: a A b
  • 13. B c C RQ-5.19:What will be the output of the following segment when executed? Program: main() { int x=10,y=20; if(( x<y)|| (x+5)>10) printf("%d",x); else printf("%d",y); getch(); } Output: 10 RQ-5.20:What will be the output of the following segment when executed? Program: main() { int a=10, b=5; if(a>b) { if(b>5) printf("%d",b); }
  • 14. else printf("%d",a); getch(); } Output: 10 CHAPTER 5 Decision Making and Branching EXERCISE-5.1 Write a program to determine whether a given number is odd or even and print the message: NUMBER IS EVEN or NUMBER IS ODD (a) without using else option, and (b) with using else option. Solution: (a) without using else option: /*………………even or odd……………*/ #include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf(“Enter a numbern”)
  • 15. scanf("%d",&n); if(n%2==0) printf("NUMBER IS EVEN "); if(n%2==1) printf("NUMBER IS ODD "); getch(); } (b) with else option: /*………………even or odd……………*/ #include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf(“Enter a numbern”) scanf("%d",&n); if(n%2==0) printf("Even"); else printf("Odd"); getch(); }
  • 16. EXERCISE-5.2 Write a program to find the number of and sum of all integers greater than 100 and less than 200 that are divisible by 7. Solution: /*…..number between 100-200 divisible by 7……*/ #include<stdio.h> #include<conio.h> void main() { int i,n,r,sum; sum=0; clrscr(); for(i=100;i<=200;i++) { r=i%7; if(r==0) { printf(" %d",i); sum=sum+i; } printf(“Sum=%d”,sum); } getch(); } EXERCISE-5.3 A set of two linear equations with two unknowns x1 and x2 is given below:
  • 17. ax1 +bx2=m and cx1+dx2=n The set has unique solution x1= and x2= provided the determinate ad-cb is not equal to zero. Write a program that will read the values of constants a,b,c,d,m and n and compute the values of x1 and x2 .An appropriate message should be printed if ad-cb=0. Solution: /*…….two linear equation………*/ #include<stdio.h> #include<conio.h> void main() { float a,b,c,d,m,n,x1,x2; clrscr(); printf("Input a,b,c,d,m,n:n"); scanf("a=%f b=%f c=%f d=%f m=%f n=%f",&a,&b,&c,&d,&m,&n); x1=(m*d-b*n)/(a*d-c*b); x2=(n*a-m*c)/(a*d-c*b); if((a*d-c*b)!=0) printf("x1=%f x2= %f",x1,x2); else printf("The value is infinity.n"); getch();
  • 18. } EXERCISE-5.4 Given a list of marks ranging from 0 to 100, write a program to print number of students: (a)Who have obtained more than 80 marks, (b) who have obtained more than 60 marks, (c)Who have obtained more than 40 marks, (d) who have obtained 40 or less marks, (e)In the range 81 to 100, (f) in the range 61 to 80, (g)in the range 41 to 60, and (h) in the range 0 to 40. The program should use a minimum numbers of if statements. Solution: /*….marks obtain……*/ #include<stdio.h> #include<conio.h> void main() { int marks,count,a,b,c,d,i; a=0; b=0; c=0;d=0; clrscr(); printf("Input 20 boy's marksn"); for(i=1;i<=20;i++) { scanf("%d",&marks); if(marks>80) a++; else if(marks>60) b++; else if(marks>40)
  • 19. c++; else if(marks<=40) d++; } printf("Number of students who have obtained more than 80 marks=%dnNumber of students who have obtained more than 60 marks=%dn Number of students who have obtained more than 40 marks=%dn Number of students who have obtained 40 or less marks=%d",a,b,c,d); getch(); } EXERCISE-5.5 Admission to a professional course is subjects to the following conditions: (a) Marks in Mathematics>=60 (b) Marks in Physics>=50 (c) Marks in Chemistry>=40 (d) Total in all three subjects>=200 or Total in Mathematics and Physics>=150 Given the marks in the three subjects, write a program to process the applications to list the eligible candidates. Solution: /*……..admission for a professional course……*/ #include<stdio.h>
  • 20. #include<conio.h> void main() { int r,m,c,p,b; clrscr(); printf("Input Mathmatics,Physics and Chemistry"); scanf("%d%d%d",&m,&p,&c); r=m+p+c; b=m+p; if(m>=60&&p>=50&&c>=40&&r>=200&&b>=150) printf("The candidate is eligible"); else printf("The candidate is not eligible"); getch(); } EXERCISE-5.7: Shown below is a Floyd’s triangle . 1 2 3 4 5 6 7 8 9 10 11………..15 79........ .. .. .. .. ..91 (a) Write a program to print this triangle. Solution:
  • 21. /*……….Floyd’s triangle………..*/ #include<stdio.h> #include<conio.h> void main() { int i,j,count,n; clrscr(); count=0; printf("nnHow many rows of Floyd triangle: "); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { count++; printf("%d",count); printf(" "); } printf("n"); } getch(); } (b) Modify the program the following from of Floyd’s triangle. 1 0 1
  • 22. 1 0 1 0 1 0 1 1 0 1 0 1 Solution: /*……….Floyd’s triangle………..*/ #include<stdio.h> #include<conio.h> void main() { int i,j,count,n; clrscr(); count=0; printf("nnHow many rows of Floyd triangle: "); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=2;j<=i+1;j++) { printf("%d",(i+j)%2); printf(" "); } printf("n"); } getch();
  • 23. } EXERCISE-5.8 A cloth showroom has announced the following seasonal discounts on purchase of items: Purchase amount Discount Mill cloth Handloom items 0-100 5% 101-200 5% 7.5% 201-300 7.5% 10.0% Above300 10.0% 15.0% Write a program using switch and if statements to compute the net amount to be paid by a coustomer. Solution: /*………….marketing of a showroom………………*/ #define MC1 0 #define MC2 0.05 #define MC3 0.075 #define MC4 0.10 #define HI1 0.05 #define HI2 0.075 #define HI3 0.10 #define HI4 0.15
  • 24. #include<stdio.h> #include<conio.h> void main() { float price,net,discount; int level,jobnumber; clrscr(); input: printf("Enter level jobnumber and purchase amountn"); printf("Enter zero for level to Endn"); scanf("%d%d%f",&level,&jobnumber,&price); if(level==0) goto stop; if(0<=price<=100) level=1; else if(101<=price<=200) level=2; else if(201<=price<=300) level=3; else level=4; switch(level) { case 1: discount=MC1+HI1; break; case 2:
  • 25. discount=MC2+HI2; break; case 3: discount=MC3+HI3; break; case 4: discount=MC4+HI4; break; default: printf("Error in level coden"); goto stop; } net=price-(price*discount); printf("Net amount=%fn",net); goto input; stop:printf("nnEND OF THE PROGRAM"); getch(); } EXERCISE-5.9 Write a program that will read the value of x and evaluate the following function y= using (a) nested if statements. (b) else if statements and (c) conditional operator ? Solution:
  • 26. /*…………evaluate the equation………..*/ (a)nested if statements: #include<stdio.h> #include<conio.h> void main() { float x,y; clrscr(); printf("Input xn"); scanf("%f",&x); if(x!=0) { if(x>0) printf("y=1"); if(x<0) printf("y=-1"); } if(x==0) printf("y=0"); getch(); } (b)else if statements: #include<stdio.h> #include<conio.h> void main() {
  • 27. float x,y; clrscr(); printf("Input xn"); scanf("%f",x); if(x!=0) { if(x>0) { printf("1"); } else printf("-1"); } else printf("0"); getch(); } (c)conditional operator: #include<stdio.h> #include<conio.h> void main() { clrscr(); float y,x; printf("Input xn"); scanf("%f",&x); y=(x!=0)?((x>0)?1:-1):0;
  • 28. printf("%d",y); getch(); } EXERCISE-5.10 Write a program to compute the real roots of a quadratic equation ax2+bx2+c=0 The roots are given by the equtions: x1 and x2 The program should request for the values of the constants a,b and c print the values of x1 and x2.Use the following: (a) No solution, if both a and b are zero (b) There is only one root if a=0(x=-c/b) (c) There are no real roots, if b2-4ac is negative (d) Otherwise, there no real roots Test your program with appropriate data so that all logical paths are working as per your design. Incorporate appropriate output messages. Solution: /*…….roots of quadratic equation ….*/ #include<stdio.h> #include<conio.h> #include<math.h> void main() { float a,b,c,x,discriminant,root1,root2; clrscr();
  • 29. printf("Input values of a, b and cn"); scanf("%f %f %f",&a,&b,&c); discriminant=b*b-4*a*c; if(a==0&&b==0) printf("No solutionn"); else if(a==0) { x=-(c/b); printf("x=%f",x); } else if(discriminant<0) printf("Roots are imaginaryn"); else { root1=-b+sqrt(discriminant)/2*a; root2=-b-sqrt(discriminant)/2*a; printf("Root1=%f Root2=%f",root1,root2); } getch(); } EXERCISE-5.11: Write a program to read three integer values from the keyboard and displays the output stating that they are the sides of right-angled triangle. Solution: /*………………right-angled triangle……..*/ #include<stdio.h> #include<conio.h>
  • 30. void main() { int a,b,c,x,y,z; clrscr(); printf("Input three integer values a b and cn"); scanf("%d%d%d",&a,&b,&c); x=a*a; y=b*b; z=c*c; if(a>b&&a>c&&(x==y+z)) printf("The values are sides of right-angled triangle"); else if(b>a&&b>c&&(y==x+z)) printf("The values are sides of right-angled triangle"); else if(c>a&&c>b&&z==x+y) printf("The values are sides of right-angled triangle"); else printf("The values are not sides of right-angled triangle"); getch(); EXERCISE-5.12: An electricity board charges the following rates for the use of electricity: For the first 200 units: 80 per unit For the next 100 units: 90per unit Beyond 300 units: Rs.1.00 per unit All users are charged a minimum of Rs. 100 as meter charge. If the total amount is more than Rs.400, then an additional surcharge of 15% of total amount is charged. Write a program to read the names of users and number of units consumed and print out the charges with names.
  • 31. Solution: /*………….pay bill…………..*/ #include<stdio.h> #include<conio.h> void main() { float units,total,net; char name; clrscr(); printf("Input users name and unitsn"); scanf("%s %f",&name,&units); { if(units<=200) total=100+0.80*units; else if(units<=300) total=100+0.90*units; else if(units>300) total=100+1.00*units; } if(total>400) { net=total+total*0.15; printf("Total=%f",net); } else printf("Total=%f",total); getch();
  • 32. } EXERCISE-5.13: Write a program to compute and display the sum of all integers that are divisible by 6 but not divisible by 4 and lie between 0 to 100. The program should also count and display the number of such values. Solution: /*……numbers between 0-100 divisible by 6 but not divisible by 4….*/ #include<stdio.h> #include<conio.h> void main() { int i,count; count=0; clrscr(); for(i=0;i<=100;i++) { if(i%6==0&&i%4!=0) { count=count+1; printf(" %d",i); } } printf(“n”); printf("count=%d",count); getch();
  • 33. } EXERCISE-5.14 Write an interactive program that could read a positive integer number and decide whether the number is a prime number display the output accordingly. Modify the program to count all prime numbers that lie 100 to 200. [Note: A prime number is positive integer that is divisible only by 1 or by itself] Solution: /*………….prime number …………*/ #include<stdio.h> #include<conio.h> void main() { int i,j,count; count=0; clrscr(); printf("nnSeries of prime number from 100 to 200:n"); for(i=100;i<=200;i++) { for(j=2;j<=i;j++) { if(i%j==0) break; } if(i==j) { printf("%4dn",i); count+=1;
  • 34. } } printf("The countable number is: %d",count); getch(); } EXERCISE-5.15: Write a program read a double-type value x that represents angle in radians and a character-type variable t that represents the type of trigonometric function and display the value of (a) sin(x), if s or S is a assigned to T, (b) cos(x), if c or C is assigned to T, and (c) tan(x), if t or T is assigned to T Using (i) if…else statement and (ii) switch statement. Solution-1: (i)if…else statement : /*……………trigonometric function……….*/ #include<stdio.h> #include<conio.h> #include<math.h> #include<ctype.h> void main() { int x,c,s,d,t; clrscr(); float r,result; s=1; c=2;
  • 35. t=3; printf(“Input the value of x and character valuen”); scanf("%d",&x); r=x*(180/3.1416); scanf("%d",&d); { if(d==1) result=sin(r); else if(d==2) result=cos(r); else if(d==3) result==tan(r); else printf("no response."); } printf("n%f",result); getch(); } Solution-2: (ii) switch statement: /*………….trigonometric function…………..*/ #include<stdio.h> #include<conio.h> #include<math.h> void main() { int i,x;
  • 36. float v,r; char t; clrscr(); printf("Input the value of xn"); scanf("%d",&x); r=x*(180/3.1416); printf("Input charecter"); scanf("%c",&t); switch(t) { case 's': case 'S': v=sin(r); case 'c': case 'C': v=cos(r); case 't': case 'T': v=tan(r); } printf("%f",v); getch()} Reference: http://hstuadmission.blogspot.com/2010/12/solution-programming-in-ansi-c- chapter.html