SlideShare une entreprise Scribd logo
1  sur  43
Télécharger pour lire hors ligne
Decision making and looping
REVIEW QUESTIONS
6.1 State whether the following statements are true or false:
(a) The do… while statement first executes the loop body and then evaluate the
loop control expression.
Ans: True.
(b) In a preset loop, if the body is executed n terms, the test expression is executed
n+1 times.
Ans: True.
(c) The number of times a control variable is updated always equals the number of
loop iterations.
Ans: True.
(d) Both the preset loops include initialization within the statement.
Ans: True.
(e) In a for loop expression, the starting value of the control variable must be less
than its ending value.
Ans: True.
(f) The initialization, test condition and increment parts may be missing in a for
statement.
Ans: False.
(g) While loops can be used to replace for loops without any change in the body of
the loop.
Ans: False.
(h) An exit control loop is executed a minimum of a one line.
Ans: False.
(i) The use of continue statement considered as unstructured programming.
Ans: True.
(j) The three loop expressions used in a for loop header must be separated
by commas.
Ans: True.
6.2: Fill in the blanks in the following statements.
(a) In an exit controlled loop, if body is executed n times, test condition is
evaluated
times.
Ans: (n-1)
(b) The statements is use to skip a part of the statements in a loop.
Ans: continue.
(c) A for loop with the no test condition is known as loop.
Ans: infinite
(d) The sentinel controlled loop is also; known as loop.
Ans: indefinite repetition.
(e)In a counter controlled loop, variable known as is used to count the loop
operation.
Ans: definite repetition.
6.8 explain the operation of each of the following for loops.
(a)for (n=1;n!=10;n+=2)
sum=sum+n;
Ans :The loop repeats 5 times.
(b)for(n=5;n<=m;n-=1)
sum+=n;
Ans: The continue until n<=m where m initializes from 5 and decrements by 1.
(c) for(n=1;n<=5)
sum+=n;
Ans: Since theren is no increment or decrement condition the loop repeats 5 times.
(d) for(n=1; ;n+=1)
sum+=n;
Ans: The loop repeats infinity times.
(e)for(n=1;n<5;n++)
n=n-1;
Ans: The loop repeats infinity times.
6.9: what would be the output of each of the following code segments?
(a)count=5;
while(count-- >0)
printf(“count”);
Output:
5 4 3 2 1
(b)count=5;
while(-- count>0)
Printf(“count”);
Output:
4 3 2 1
(c) count=5;
do printrf(“count”);
while(count>0)
Output:
5 4 3 2 1
(d)for(m=10;m>7;m-=2)
printf(“m”);
output;
10 8
6.11:Analyse each of the program segment that follow the determine how
many times the body of each loop will be executed.
(a)x=5;
y=50;
while(x<=y)
{
x=y/x;
…………………..
…………………..
}
Ans: Infinity times
(b) m=1;
do
{
……………………
……………………….
m+=2;
}
while(m<10)
Ans: 5 times.
(c) int i;
for(i=0;i<=5;i=i+2/3)
{
…………………..
…………………….
}
Ans: Infinity times.
(d) int m=10;
Int n=7;
while(m%n>=0)
{
………………
m+=1;
n+=2;
…………….
}
Ans: 4 times.
6.12: Find errors, if any, in each of the following looping segments. Assume
that all the variables have been declared and assigned values.
(a)while(count!=10);
{
count=1;
sum+=x;
count+=1;
}
Error: while(count!=10);
Correct Ans: while(count!=10)
(b) name=0;
do
{
name+=1;
printf(“my name is Dinarn”);
while(name=1);
Error: while (name=1);
Correct Ans: while(name==1);
(c) do;
total+=value;
scanf(“%f”,&value);
while(value!=999);
Error: do;
Correct Ans: do
(E) m=1;
n=0;
for(p=10;p>0;)
p-=1;
printf(“%f”,p);
Error: for(p=10;p>0;)
p-=1;
printf(“%f”,p);
Correct ans: for(p=10;p>0;)
{
p-=1;
printf(“%f”,p);
}
6.13:Write a for statement to pront each of the following sequence of integers:
(a) 1,2,4,8,16,32
Ans: for(i=1;i<=32;i=i*2)
printf(“%d”,i);
(b) 1,3,9,27,81,243
Ans: for(i=1;i<=243;i=i*i)
printf(“%d”,i);
(c) -4,-2,0,4
for(i=-4;i<=4;i=i+2)
printf(“%d”,i);
(d) -10,-12,-14,-18,-26,-42
for(i=-10;i<=-42;i=i-2)
printf(“%d”,i);
6.14: Change the following for loops to while loops :
(a)for(m=1;m<10;m=m+1)
printf(“m”);
Ans: m=1;
while(m<10)
{
…………….
m++;
}
printf(“m”);
(b)for(;scanf(“%d”,&m)!=-1;)
printf(“m”);
Ans:
while(scanf(“%d”,&m)!=-1)
printf(“m”);
6.16: What is the output of following code?
Int m=100,n=0;
while(n==0)
{
if(m<10)
break;
m=m-10;
}
Output: No output
6.17: What is output of the following code?
int m=0;
do
{
if(m>10)
continue;
m=m+10;
}
while(m<50);
printf(“%d”,m);
Output: 50
6.18: What is the output of the following code?
int n=0,m=1;
do
{
printf(“m”);
m++;
}
while(m<=n);
Output: 1
6.19: What is the output of the following code?
int n=0,m;
for(m=1;m<=n+1;m++)
printrf(“m”);
Output: 1
6.20: When do we use the following statement?
for(; ;)
Ans : When we need an infinity loop the statement for(; ;) can be used.
Programming exercises
Problem 6.1: Given a integer number write using while loop to reverse the
digit of the number. For example, the number
12345
should be written as
54321
Solution:
/*……………..…..reverse number ……………………*/
#include
#include
void main()
{
clrscr();
long int n,b;
printf("nnn Input number:");
scanf("%ld",&n);
while(n>10)
{
b=n%10;
printf("The reversed number is:%ld",b);
n=n/10;
}
printf("%ld",n);
getch();
}
Problem 6.2: The factorial of an integer m is the product of consecutive
integers from 1 to m. that is
Factorial m=m!=m*(m-1)*…………………*1.
Write a program that computes and print the result for any given m.
Solution :
/*……………………………factorial…………………………*/
#include
#include
void main()
{
int sum,i,m;
clrscr();
printf(“Input number:”);
scanf("%d",&m);
sum=1;
for(i=m;i>1;i--)
sum*=i;
printf("The result is: %d",sum);
getch();
}
Problem 6.3: Write a program that compute the sum of the digit of a given
integer number.
Solution:
/*………………..sum of given integer number………………….*/
#include
#include
void main()
{
clrscr();
long int n,b,sum=0;
printf("nnnInput number:");
scanf("%ld",&n);
while(n>10)
{
b=n%10;
printf("%ld+",b);
n=n/10;
sum+=b;
}
sum+=n;
printf("the result is= %ld",sum);
getch();
}
Problem 6.4: The numbers in the sequence
1 1 2 3 5 8 13 21……………………..
Are called Fibonacci numbers. Write a program using a do-while loop
to calculate and print the Fibonacci numbers.
Solution :
/*……………………Fibonacci sequence…….……………….*/
#include
#include
void main()
{
clrscr();
int m,x,y,n,z;
m=0;
x=0;
y=1;
printf("nnInput number of count:");
scanf("%d",&n);
printf("%d",y);
do{
z=x+y;
x=y;
y=z;
m++;
printf(" the result= is %d",z);
}while(m<n);< span=""></n);<>
getch();
}
Problem 6.5: The numbers in the sequence
1 1 2 3 5 8 13 21……………………..
Are called Fibonacci numbers. Write a program using a for loop
to calculate and print the Fibonacci numbers.
Solution :
/*……………………Fibonacci sequence…….……………….*/
#include
#include
void main()
{
clrscr();
int x,y,n,z,i;
x=0;
y=1;
printf("nnInput number:");
scanf("%d",&n);
printf("%d",y);
for(i=1;i<=n;i++)
{
z=x+y;
x=y;
y=z;
printf(" the result= %d",z);
}
getch();
}
Problem 6.6: Write a program to evaluate the following investment equation
V=P(1+r)
And print the tables which would give the value of V for various combination
of the following values of P,r and n.
Solution:
/*…………investment equation………………..*/
#include
#include
#include
void main()
{
int i,n,p,j;
double v,r,t;
printf("nnn");
clrscr();
r=0.10;
n=10;
p=1000;
t=pow((1+r),i);
// printf("nn%.6lf ",t);
printf("nP R N Vn");
for(i=1;i<=n;i++)
{
if(r<=0.20 && p<=10000)
{
t=pow((1+r),i);
v=p*t;
p=p+1000;
r=r+0.01;
}
printf("%d %lf %d ",p,r,n);
printf("V=% .6lf ",v);
printf("n");
}
getch();
}
Problem 6.7(a): Write the program to print the following outputs using for
loops.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Solution :
/*………...trivues……………*/
#include
#include
Void main()
{
int i,j,n;
clrscr();
printf("nn Input number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
printf(" ");
}
printf("n");
}
getch();
}
Problem 6.7(b): Write programs to print the following outputs using for loops.
* * * * *
* * * *
* * *
* *
*
Solution:
/*………………………….star…………………….….*/
#include
#include
void main()
{
int i,j,n,c;
clrscr();
printf(“Input number:”);
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
c=i;
if(c>j)
{
printf(" ");
c++;
}
else
printf("*");
}
printf("n");
getch();
}
Problem 6.8:Write a program to read the age of 100 persons and count the
number of persons in the age group 50 to 60. Use for and continue statements.
Solution:
/****************age count into 50 to 60***************/
#include
#include
void main()
{
clrscr();
int count,i,age;
count=0;printf(“Input age of 100 persons:”);
for(i=1;i<=100;i++)
{
scanf("%d",&age);
if(age>=50&&age<=60)
count+=1;
continue;
}
printf("the countable number is:%d",count);
getch();
}
Problem 6.9: We have two function of the type
Y1=exp(-a*x)
Y2=exp(-a*x*x/2)
Plot the graphs of these function for x verifying from 0 to 5.0.
Without using continue statement.
Solution:
/*……………..plotting two function………………..*/
#include
#include
#include
void main()
{
clrscr(); printf("nn");
int i;
float a,x,y1,y2;
a=0.4;
printf(" Y.....>n");
printf("0-----------------------------------------------------n");
for(x=0;x<5;x+=0.25)
{
y1=(int)(50*exp(-a*x)+0.5);
y2=(int)(50*exp(-a*x*x/2)+0.5);
if(y1==y2)
{
if(x==2.5)
printf("x|");
else
printf(" |");
for(i=1;i<=(y1-1);++i)
printf(" ");
printf("#n");
}
else if(y1>y2)
{
if(x==2.5)
printf("x|");
else
printf(" |");
for(i=1;i<=(y2-1);++i)
printf(" ");
printf("*");
for(i=1;i<=(y1-y2-1);++i);
printf("-");
printf("0n");
}
else
{
if(x==2.5)
printf("x|");
else
printf(" |");
for(i=1;i<=(y1-1);++i)
printf(" ");
printf("0");
for(i=1;i<=(y2-y1-1);++i)
printf("-");
printf("*n");
}
}
printf(" |n");
getch();
}
Problem 6.10: Write a program to print a table of values of the function
Y=exp(-x)
For x varying from 0.0 to 10.0 in steps of 0.10.
Solution:
/*……………….y=exp(-x)………………*/
#include
#include
#include
void main()
{
clrscr();
int i,j;
float y;
printf("nn....................................n");
printf("x");
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
y=exp(-i);
printf(" %.2f ",y);
}
printf("n");
}
getch();
}
Problem 6.11: Write a program that will read a positive integer and
determine and print its binary equivalent.
Solution:
/* ………………….to binary…………………..*/
#include
#include
void main()
{
char binary[20],temp[20];
int i,j,num;
clrscr();
i=0;
printf("nnEnter value:");
scanf("%d",&num);
while(num>=1)
{
temp[i++]=(num%2)+48;
num=num/2;
}
printf("nnthe binary equvelant is:");
for(j=i-1;j>=0;j--)
printf("%c",temp[j]);
getch();
}
Problem6.12: Write a program using for and if statement to display the
capital letter S in a grid of 15 rows and 18 columns as shown below.
******************
******************
******************
****
****
****
******************
******************
******************
****
****
****
*******************
*******************
*******************
Solution :
/*……………………S grid…………………..*/
#include
#include
void main()
{
clrscr();
int row,col,i,j,k;
row=15;
col=18;
for(i=1;i<=row;i++)
{
if((i<=3)||(i>=7&&i<=9)||(i>=13&&i<=15))
{
for(j=1;j<col;j++)< span=""></col;j++)<>
printf("*");
printf("n");
}
else if(i>=4&&i<=6)
{
for(j=1;j<=4;j++)
printf("*");
printf("n");
}
else
{
for(j=1;j<=13;j++)
printf(" ");
for(j=1;j<=4;j++)
printf("*");
printf("n");
}
}
getch();
}
Problem 6.13:Write a program to compute the value of Euler’s number e, that
is used as the base of natural logarithms. Use the following formula.
e=1+1/1!+1/2!+1/3!+…………………………………+1/n!
use a suitable loop construct. The loop must terminate when the difference
between two successive values of e is less than 0.00001.
Solution:
/*……………………….Euler’s number……………………..*/
#include
#include
float fact(float i)
{
float f=1;
int k;
for(k=1;k<=i;k++)
f*=k;
return(f);
}
void main()
{
float e,a,b,d=1,temp;
int i,j,k;
clrscr();
e=1;i=2;b=1;
while(d>=0.00001)
{
temp=fact(i);
a=1/temp;
e=e+a;
d=b-a;
if(d>=0.00001)
b=a;
i++;
}
printf("nnthe result = %f",e);
getch();
}
Problem 6.14: Write programs to evaluate the following functions to 0.0001%
accuracy.
(a) sinx =x-x3
/3!+x5
/5!-x7
/7!+………………….
Solution:
/*……………….sinx function………………..*/
#include
#include
#include
double fact(double power)
{
double f=1;
int k;
for(k=1;k<=power;k++)
f=f*k;
return f;
}
void main()
{
int i=1;
double x,term,deno,lob,sin,power=3;
clrscr();
scanf("%lf",&x);
term=x;
sin=x;
while(term>=0.0001)
{
lob=pow(x,power);
deno=fact(power);
term=lob/deno;
power+=2;
if(i%2==1)
sin=sin-term;
else
sin=sin+term;
i++
}
printf("the result= %lf",sin);
getch();
}
Problem 6.14: Write programs to evaluate the following functions to 0.0001%
accuracy.
(b) cosx = 1-x2
/2!+x4
/4!-x6
/6!+………………….
Solution:
/*……………….cosx function………………..*/
#include
#include
#include
double fact(double power)
{
double f=1;
int k;
for(k=1;k<=power;k++)
f=f*k;
return(f);
}
void main()
{
int i=1;
double x,term,deno,lob,cos,power=2;
clrscr();
scanf("%lf",&x);
term=1.0;
cos=1.0;
while(term>=0.0001)
{
lob=pow(x,power);
deno=fact(power);
term=lob/deno;
power+=2;
if(i%2==1)
cos=cos-term;
else
cos=cos+term;
i++;
}
printf("the result is= %lf",cos);
getch();
}
Problem 6.14(c): Write programs to evaluate the following functions to
0.0001% accuracy.
Sum=1+(1/2)2
+(1/3)3
+(1/4)4
+…………………………………
Solution:
/*…………………..accuracy…………….*/
#include
#include
#include
void main()
{
double term,deno,lob,sum;
clrscr();
term=1.0;
sum=1.0; lob=1.0;deno=2.0;
while(term>=0.0001)
{
term=lob/deno;
term=pow(term,deno);
sum+=term;
deno++; printf("%lf",sum);
}
printf("the sum= %lf",sum);
getch();
}
Problem6.15: The present value (popularly known as book value )of an item is
given by the relationship
P=c(1-d)n
Where c=original cost
D=rate of depreciation
N=number of years
P=present value after years.
If p is considered the scrap value at the end of useful life of the item, write a
program to compute the useful life in years given the original cost,
depreciation rate, and the scrap value.
Solution:
/*…………..useful life in years…………….*/
#include
#include
#include
void main()
{
clrscr();
int n,c;
double d,p,lob,hor;
printf(“Input original cost, rate of depreciation, present value”);
scanf("%d%lf%lf",&c,&d,&p);
lob=log(p/c);
hor=log(1-d);
n=lob/hor;
printf("year=%d",n); getch();
}
Problem 6.16(a): Write a program to print a square size 5 by using the
character S as shown below
S S S S S
S S S S S
S S S S S
S S S S S
S S S S S
Solution :
/*……………….square size………………..*/
#include
#include
void main()
{
clrscr();
int i,j;
printf("nn");
for(i=0;i<=4;i++)
{
for(j=1;j<=5;j++)
printf(" S");
printf("nn");
}
getch();
}
Problem 6.16(b): Write a program to print a square size 5 by using the
character S as shown below
S S S S S
S S
S S
S S
S S S S S
solution :
/*……………….square size………………..*/
#include
#include
void main()
{
int i,j,n;
clrscr();
n=5; printf("nn");
for(i=1;i<=n;i++)
printf("s ");
printf("nn");
for(i=1;i<=n-2;i++)
{
printf("s ");
for(j=1;j<=n-2;j++)
printf(" ");
printf("snn");
}
for(i=1;i<=n;i++)
printf("s ");
printf("n");
getch();
}
Problem 6.18 : Write the program to print all integers that are not divisible by
2 or 3 and lie between 1 and 100. Program should also account the number of
such integers and print the result.
Solution:
/*………..count the number into 1-100 which r divisible 2 or 3………………*/
#include
#include
void main()
{
int i,count,sum;
clrscr();
sum=0;
count=0;
for(i=1;i<=100;i++)
{
if(i%2!=0&&i%3!=0)
{
sum+=i;
count++;
printf("%dn",i);
}
}
printf("the sum of the value is :%d nthe countable numbers is: %d",sum,count);
getch();
}
Problem 6.19: Write a program to print a square of size 5 by using the
character S as shown below:
S S S S S
S S S S S
S S O S S
S S S S S
S S S S S
Solution:
/*……………………………..square with centre 0…………………………….*/
#include
#include
void main()
{
int n,i,j,mid;
clrscr();
printf("nn");
scanf("%d",&n);
mid=(int)(n/2);
for(i=0;i<n;i++)< span=""></n;i++)<>
{
for(j=0;j<n;j++)< span=""></n;j++)<>
{
if(i==mid&&j==mid)
printf("0 ");
else
printf("s ");
}
printf("nn");
}
getch();
}
Problem 6.20: Given a set of 10 two-digit integer containing both positive and
negative values, write a program using for loop to compute the sum of all
positive values and print the sum and the number of values added. The program
should use scanf to read the values and terminate when the sum exceeds 999.
Do not use goto statement.
Solution:
/*…………………….sum positive number……………………*/
#include
#include
void main()
{
nt sum,n,i,j=0;
sum=0;
clrscr();
printf("nnInput ten number both positive and negative:");
for(i=0;i<10;i++)
{
scanf("%d",&n);
if(n>0)
{
sum+=n; j++;}
if(sum>999)
break;
}
printf("nnThe value of positive numbers is:%dnand the countable number is:
%d",sum,j);
getch();
}
Reference:
http://hstuadmission.blogspot.com/2010/12/solution-programming-in-ansi-c-
chapter.html

Contenu connexe

Tendances

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 RobinShariful 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 solutionrohit kumar
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 
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 1rohit kumar
 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C LanguageRAJWANT KAUR
 
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 robinAbdullah Al Naser
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in CRAJ 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 solutionHazrat Bilal
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in CMuthuganesh S
 
Let us c chapter 4 solution
Let us c chapter 4 solutionLet us c chapter 4 solution
Let us c chapter 4 solutionrohit kumar
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
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 SolutionHazrat Bilal
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and StringTasnima Hamid
 

Tendances (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
 
C Programming
C ProgrammingC Programming
C Programming
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
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
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
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
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
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
 
C string
C stringC string
C string
 
Strings in C
Strings in CStrings in C
Strings in C
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
 
Let us c chapter 4 solution
Let us c chapter 4 solutionLet us c chapter 4 solution
Let us c chapter 4 solution
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Function in c program
Function in c programFunction in c program
Function in c program
 
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
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 

Similaire à Chapter 6 Balagurusamy Programming ANSI in c

C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2Amit Kapoor
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops Hemantha Kulathilake
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 
Functions Practice Sheet.docx
Functions Practice Sheet.docxFunctions Practice Sheet.docx
Functions Practice Sheet.docxSwatiMishra364461
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview QuestionsGradeup
 
Classical programming interview questions
Classical programming interview questionsClassical programming interview questions
Classical programming interview questionsGradeup
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitionsaltwirqi
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...DR B.Surendiran .
 
Programming Fundamentals lecture 8
Programming Fundamentals lecture 8Programming Fundamentals lecture 8
Programming Fundamentals lecture 8REHAN IJAZ
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptISHANAMRITSRIVASTAVA
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop kapil078
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptxManojKhadilkar1
 

Similaire à Chapter 6 Balagurusamy Programming ANSI in c (20)

C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
 
CP Handout#5
CP Handout#5CP Handout#5
CP Handout#5
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Functions Practice Sheet.docx
Functions Practice Sheet.docxFunctions Practice Sheet.docx
Functions Practice Sheet.docx
 
Struct examples
Struct examplesStruct examples
Struct examples
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
 
Looping statements
Looping statementsLooping statements
Looping statements
 
06.Loops
06.Loops06.Loops
06.Loops
 
C important questions
C important questionsC important questions
C important questions
 
Data Structure and Algorithm
Data Structure and AlgorithmData Structure and Algorithm
Data Structure and Algorithm
 
Classical programming interview questions
Classical programming interview questionsClassical programming interview questions
Classical programming interview questions
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 
Programming Fundamentals lecture 8
Programming Fundamentals lecture 8Programming Fundamentals lecture 8
Programming Fundamentals lecture 8
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.ppt
 
C code examples
C code examplesC code examples
C code examples
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptx
 

Plus de 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 pageBUBT
 
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 SystemBUBT
 
Student Attendance
Student AttendanceStudent Attendance
Student AttendanceBUBT
 
Reasoning for Artificial Intelligence Expert
Reasoning for Artificial Intelligence ExpertReasoning for Artificial Intelligence Expert
Reasoning for Artificial Intelligence ExpertBUBT
 
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 ReportBUBT
 
Auto Room Lighting System
Auto Room Lighting SystemAuto Room Lighting System
Auto Room Lighting SystemBUBT
 
Doorlock
DoorlockDoorlock
DoorlockBUBT
 
Ultra Dense Netwok
Ultra Dense NetwokUltra Dense Netwok
Ultra Dense NetwokBUBT
 
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 SystemBUBT
 
Shop management system
Shop management systemShop management system
Shop management systemBUBT
 

Plus de 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
 

Dernier

USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 

Dernier (20)

YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 

Chapter 6 Balagurusamy Programming ANSI in c

  • 1. Decision making and looping REVIEW QUESTIONS 6.1 State whether the following statements are true or false: (a) The do… while statement first executes the loop body and then evaluate the loop control expression. Ans: True. (b) In a preset loop, if the body is executed n terms, the test expression is executed n+1 times. Ans: True. (c) The number of times a control variable is updated always equals the number of loop iterations. Ans: True. (d) Both the preset loops include initialization within the statement. Ans: True. (e) In a for loop expression, the starting value of the control variable must be less than its ending value. Ans: True. (f) The initialization, test condition and increment parts may be missing in a for statement. Ans: False. (g) While loops can be used to replace for loops without any change in the body of the loop. Ans: False. (h) An exit control loop is executed a minimum of a one line.
  • 2. Ans: False. (i) The use of continue statement considered as unstructured programming. Ans: True. (j) The three loop expressions used in a for loop header must be separated by commas. Ans: True. 6.2: Fill in the blanks in the following statements. (a) In an exit controlled loop, if body is executed n times, test condition is evaluated times. Ans: (n-1) (b) The statements is use to skip a part of the statements in a loop. Ans: continue. (c) A for loop with the no test condition is known as loop. Ans: infinite (d) The sentinel controlled loop is also; known as loop. Ans: indefinite repetition. (e)In a counter controlled loop, variable known as is used to count the loop operation. Ans: definite repetition. 6.8 explain the operation of each of the following for loops. (a)for (n=1;n!=10;n+=2) sum=sum+n;
  • 3. Ans :The loop repeats 5 times. (b)for(n=5;n<=m;n-=1) sum+=n; Ans: The continue until n<=m where m initializes from 5 and decrements by 1. (c) for(n=1;n<=5) sum+=n; Ans: Since theren is no increment or decrement condition the loop repeats 5 times. (d) for(n=1; ;n+=1) sum+=n; Ans: The loop repeats infinity times. (e)for(n=1;n<5;n++) n=n-1; Ans: The loop repeats infinity times. 6.9: what would be the output of each of the following code segments? (a)count=5; while(count-- >0) printf(“count”); Output: 5 4 3 2 1 (b)count=5; while(-- count>0)
  • 4. Printf(“count”); Output: 4 3 2 1 (c) count=5; do printrf(“count”); while(count>0) Output: 5 4 3 2 1 (d)for(m=10;m>7;m-=2) printf(“m”); output; 10 8 6.11:Analyse each of the program segment that follow the determine how many times the body of each loop will be executed. (a)x=5; y=50; while(x<=y) { x=y/x; ………………….. ………………….. }
  • 5. Ans: Infinity times (b) m=1; do { …………………… ………………………. m+=2; } while(m<10) Ans: 5 times. (c) int i; for(i=0;i<=5;i=i+2/3) { ………………….. ……………………. } Ans: Infinity times. (d) int m=10; Int n=7; while(m%n>=0) {
  • 6. ……………… m+=1; n+=2; ……………. } Ans: 4 times. 6.12: Find errors, if any, in each of the following looping segments. Assume that all the variables have been declared and assigned values. (a)while(count!=10); { count=1; sum+=x; count+=1; } Error: while(count!=10); Correct Ans: while(count!=10) (b) name=0; do { name+=1; printf(“my name is Dinarn”); while(name=1);
  • 7. Error: while (name=1); Correct Ans: while(name==1); (c) do; total+=value; scanf(“%f”,&value); while(value!=999); Error: do; Correct Ans: do (E) m=1; n=0; for(p=10;p>0;) p-=1; printf(“%f”,p); Error: for(p=10;p>0;) p-=1; printf(“%f”,p); Correct ans: for(p=10;p>0;) { p-=1; printf(“%f”,p); }
  • 8. 6.13:Write a for statement to pront each of the following sequence of integers: (a) 1,2,4,8,16,32 Ans: for(i=1;i<=32;i=i*2) printf(“%d”,i); (b) 1,3,9,27,81,243 Ans: for(i=1;i<=243;i=i*i) printf(“%d”,i); (c) -4,-2,0,4 for(i=-4;i<=4;i=i+2) printf(“%d”,i); (d) -10,-12,-14,-18,-26,-42 for(i=-10;i<=-42;i=i-2) printf(“%d”,i); 6.14: Change the following for loops to while loops : (a)for(m=1;m<10;m=m+1) printf(“m”); Ans: m=1; while(m<10) { ……………. m++;
  • 9. } printf(“m”); (b)for(;scanf(“%d”,&m)!=-1;) printf(“m”); Ans: while(scanf(“%d”,&m)!=-1) printf(“m”); 6.16: What is the output of following code? Int m=100,n=0; while(n==0) { if(m<10) break; m=m-10; } Output: No output 6.17: What is output of the following code? int m=0; do { if(m>10)
  • 10. continue; m=m+10; } while(m<50); printf(“%d”,m); Output: 50 6.18: What is the output of the following code? int n=0,m=1; do { printf(“m”); m++; } while(m<=n); Output: 1 6.19: What is the output of the following code? int n=0,m; for(m=1;m<=n+1;m++) printrf(“m”); Output: 1 6.20: When do we use the following statement?
  • 11. for(; ;) Ans : When we need an infinity loop the statement for(; ;) can be used. Programming exercises Problem 6.1: Given a integer number write using while loop to reverse the digit of the number. For example, the number 12345 should be written as 54321 Solution: /*……………..…..reverse number ……………………*/ #include #include void main() { clrscr(); long int n,b; printf("nnn Input number:"); scanf("%ld",&n); while(n>10) { b=n%10; printf("The reversed number is:%ld",b);
  • 12. n=n/10; } printf("%ld",n); getch(); } Problem 6.2: The factorial of an integer m is the product of consecutive integers from 1 to m. that is Factorial m=m!=m*(m-1)*…………………*1. Write a program that computes and print the result for any given m. Solution : /*……………………………factorial…………………………*/ #include #include void main() { int sum,i,m; clrscr(); printf(“Input number:”); scanf("%d",&m); sum=1; for(i=m;i>1;i--) sum*=i;
  • 13. printf("The result is: %d",sum); getch(); } Problem 6.3: Write a program that compute the sum of the digit of a given integer number. Solution: /*………………..sum of given integer number………………….*/ #include #include void main() { clrscr(); long int n,b,sum=0; printf("nnnInput number:"); scanf("%ld",&n); while(n>10) { b=n%10; printf("%ld+",b); n=n/10; sum+=b;
  • 14. } sum+=n; printf("the result is= %ld",sum); getch(); } Problem 6.4: The numbers in the sequence 1 1 2 3 5 8 13 21…………………….. Are called Fibonacci numbers. Write a program using a do-while loop to calculate and print the Fibonacci numbers. Solution : /*……………………Fibonacci sequence…….……………….*/ #include #include void main() { clrscr(); int m,x,y,n,z; m=0; x=0; y=1; printf("nnInput number of count:"); scanf("%d",&n);
  • 15. printf("%d",y); do{ z=x+y; x=y; y=z; m++; printf(" the result= is %d",z); }while(m<n);< span=""></n);<> getch(); } Problem 6.5: The numbers in the sequence 1 1 2 3 5 8 13 21…………………….. Are called Fibonacci numbers. Write a program using a for loop to calculate and print the Fibonacci numbers. Solution : /*……………………Fibonacci sequence…….……………….*/ #include #include void main() { clrscr(); int x,y,n,z,i;
  • 16. x=0; y=1; printf("nnInput number:"); scanf("%d",&n); printf("%d",y); for(i=1;i<=n;i++) { z=x+y; x=y; y=z; printf(" the result= %d",z); } getch(); } Problem 6.6: Write a program to evaluate the following investment equation V=P(1+r) And print the tables which would give the value of V for various combination of the following values of P,r and n. Solution: /*…………investment equation………………..*/ #include #include
  • 17. #include void main() { int i,n,p,j; double v,r,t; printf("nnn"); clrscr(); r=0.10; n=10; p=1000; t=pow((1+r),i); // printf("nn%.6lf ",t); printf("nP R N Vn"); for(i=1;i<=n;i++) { if(r<=0.20 && p<=10000) { t=pow((1+r),i); v=p*t; p=p+1000; r=r+0.01;
  • 18. } printf("%d %lf %d ",p,r,n); printf("V=% .6lf ",v); printf("n"); } getch(); } Problem 6.7(a): Write the program to print the following outputs using for loops. 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Solution : /*………...trivues……………*/ #include #include Void main() { int i,j,n; clrscr();
  • 19. printf("nn Input number:"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { printf("%d",i); printf(" "); } printf("n"); } getch(); } Problem 6.7(b): Write programs to print the following outputs using for loops. * * * * * * * * * * * * * * * Solution: /*………………………….star…………………….….*/
  • 20. #include #include void main() { int i,j,n,c; clrscr(); printf(“Input number:”); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { c=i; if(c>j) { printf(" "); c++; } else printf("*"); }
  • 21. printf("n"); getch(); } Problem 6.8:Write a program to read the age of 100 persons and count the number of persons in the age group 50 to 60. Use for and continue statements. Solution: /****************age count into 50 to 60***************/ #include #include void main() { clrscr(); int count,i,age; count=0;printf(“Input age of 100 persons:”); for(i=1;i<=100;i++) { scanf("%d",&age); if(age>=50&&age<=60) count+=1; continue; } printf("the countable number is:%d",count);
  • 22. getch(); } Problem 6.9: We have two function of the type Y1=exp(-a*x) Y2=exp(-a*x*x/2) Plot the graphs of these function for x verifying from 0 to 5.0. Without using continue statement. Solution: /*……………..plotting two function………………..*/ #include #include #include void main() { clrscr(); printf("nn"); int i; float a,x,y1,y2; a=0.4; printf(" Y.....>n"); printf("0-----------------------------------------------------n"); for(x=0;x<5;x+=0.25)
  • 25. } Problem 6.10: Write a program to print a table of values of the function Y=exp(-x) For x varying from 0.0 to 10.0 in steps of 0.10. Solution: /*……………….y=exp(-x)………………*/ #include #include #include void main() { clrscr(); int i,j; float y; printf("nn....................................n"); printf("x"); for(i=0;i<10;i++) { for(j=0;j<10;j++) { y=exp(-i);
  • 26. printf(" %.2f ",y); } printf("n"); } getch(); } Problem 6.11: Write a program that will read a positive integer and determine and print its binary equivalent. Solution: /* ………………….to binary…………………..*/ #include #include void main() { char binary[20],temp[20]; int i,j,num; clrscr(); i=0; printf("nnEnter value:"); scanf("%d",&num); while(num>=1) {
  • 27. temp[i++]=(num%2)+48; num=num/2; } printf("nnthe binary equvelant is:"); for(j=i-1;j>=0;j--) printf("%c",temp[j]); getch(); } Problem6.12: Write a program using for and if statement to display the capital letter S in a grid of 15 rows and 18 columns as shown below. ****************** ****************** ****************** **** **** **** ****************** ****************** ****************** **** **** ****
  • 28. ******************* ******************* ******************* Solution : /*……………………S grid…………………..*/ #include #include void main() { clrscr(); int row,col,i,j,k; row=15; col=18; for(i=1;i<=row;i++) { if((i<=3)||(i>=7&&i<=9)||(i>=13&&i<=15)) { for(j=1;j<col;j++)< span=""></col;j++)<> printf("*"); printf("n"); }
  • 29. else if(i>=4&&i<=6) { for(j=1;j<=4;j++) printf("*"); printf("n"); } else { for(j=1;j<=13;j++) printf(" "); for(j=1;j<=4;j++) printf("*"); printf("n"); } } getch(); } Problem 6.13:Write a program to compute the value of Euler’s number e, that is used as the base of natural logarithms. Use the following formula. e=1+1/1!+1/2!+1/3!+…………………………………+1/n! use a suitable loop construct. The loop must terminate when the difference between two successive values of e is less than 0.00001.
  • 30. Solution: /*……………………….Euler’s number……………………..*/ #include #include float fact(float i) { float f=1; int k; for(k=1;k<=i;k++) f*=k; return(f); } void main() { float e,a,b,d=1,temp; int i,j,k; clrscr(); e=1;i=2;b=1; while(d>=0.00001) { temp=fact(i);
  • 31. a=1/temp; e=e+a; d=b-a; if(d>=0.00001) b=a; i++; } printf("nnthe result = %f",e); getch(); } Problem 6.14: Write programs to evaluate the following functions to 0.0001% accuracy. (a) sinx =x-x3 /3!+x5 /5!-x7 /7!+…………………. Solution: /*……………….sinx function………………..*/ #include #include #include double fact(double power) { double f=1; int k;
  • 32. for(k=1;k<=power;k++) f=f*k; return f; } void main() { int i=1; double x,term,deno,lob,sin,power=3; clrscr(); scanf("%lf",&x); term=x; sin=x; while(term>=0.0001) { lob=pow(x,power); deno=fact(power); term=lob/deno; power+=2; if(i%2==1) sin=sin-term; else
  • 33. sin=sin+term; i++ } printf("the result= %lf",sin); getch(); } Problem 6.14: Write programs to evaluate the following functions to 0.0001% accuracy. (b) cosx = 1-x2 /2!+x4 /4!-x6 /6!+…………………. Solution: /*……………….cosx function………………..*/ #include #include #include double fact(double power) { double f=1; int k; for(k=1;k<=power;k++) f=f*k; return(f); }
  • 34. void main() { int i=1; double x,term,deno,lob,cos,power=2; clrscr(); scanf("%lf",&x); term=1.0; cos=1.0; while(term>=0.0001) { lob=pow(x,power); deno=fact(power); term=lob/deno; power+=2; if(i%2==1) cos=cos-term; else cos=cos+term; i++; } printf("the result is= %lf",cos);
  • 35. getch(); } Problem 6.14(c): Write programs to evaluate the following functions to 0.0001% accuracy. Sum=1+(1/2)2 +(1/3)3 +(1/4)4 +………………………………… Solution: /*…………………..accuracy…………….*/ #include #include #include void main() { double term,deno,lob,sum; clrscr(); term=1.0; sum=1.0; lob=1.0;deno=2.0; while(term>=0.0001) { term=lob/deno; term=pow(term,deno); sum+=term; deno++; printf("%lf",sum);
  • 36. } printf("the sum= %lf",sum); getch(); } Problem6.15: The present value (popularly known as book value )of an item is given by the relationship P=c(1-d)n Where c=original cost D=rate of depreciation N=number of years P=present value after years. If p is considered the scrap value at the end of useful life of the item, write a program to compute the useful life in years given the original cost, depreciation rate, and the scrap value. Solution: /*…………..useful life in years…………….*/ #include #include #include void main() { clrscr(); int n,c;
  • 37. double d,p,lob,hor; printf(“Input original cost, rate of depreciation, present value”); scanf("%d%lf%lf",&c,&d,&p); lob=log(p/c); hor=log(1-d); n=lob/hor; printf("year=%d",n); getch(); } Problem 6.16(a): Write a program to print a square size 5 by using the character S as shown below S S S S S S S S S S S S S S S S S S S S S S S S S Solution : /*……………….square size………………..*/ #include #include void main() { clrscr();
  • 38. int i,j; printf("nn"); for(i=0;i<=4;i++) { for(j=1;j<=5;j++) printf(" S"); printf("nn"); } getch(); } Problem 6.16(b): Write a program to print a square size 5 by using the character S as shown below S S S S S S S S S S S S S S S S solution : /*……………….square size………………..*/ #include #include void main()
  • 39. { int i,j,n; clrscr(); n=5; printf("nn"); for(i=1;i<=n;i++) printf("s "); printf("nn"); for(i=1;i<=n-2;i++) { printf("s "); for(j=1;j<=n-2;j++) printf(" "); printf("snn"); } for(i=1;i<=n;i++) printf("s "); printf("n"); getch(); } Problem 6.18 : Write the program to print all integers that are not divisible by 2 or 3 and lie between 1 and 100. Program should also account the number of such integers and print the result.
  • 40. Solution: /*………..count the number into 1-100 which r divisible 2 or 3………………*/ #include #include void main() { int i,count,sum; clrscr(); sum=0; count=0; for(i=1;i<=100;i++) { if(i%2!=0&&i%3!=0) { sum+=i; count++; printf("%dn",i); } } printf("the sum of the value is :%d nthe countable numbers is: %d",sum,count); getch();
  • 41. } Problem 6.19: Write a program to print a square of size 5 by using the character S as shown below: S S S S S S S S S S S S O S S S S S S S S S S S S Solution: /*……………………………..square with centre 0…………………………….*/ #include #include void main() { int n,i,j,mid; clrscr(); printf("nn"); scanf("%d",&n); mid=(int)(n/2); for(i=0;i<n;i++)< span=""></n;i++)<> { for(j=0;j<n;j++)< span=""></n;j++)<>
  • 42. { if(i==mid&&j==mid) printf("0 "); else printf("s "); } printf("nn"); } getch(); } Problem 6.20: Given a set of 10 two-digit integer containing both positive and negative values, write a program using for loop to compute the sum of all positive values and print the sum and the number of values added. The program should use scanf to read the values and terminate when the sum exceeds 999. Do not use goto statement. Solution: /*…………………….sum positive number……………………*/ #include #include void main() { nt sum,n,i,j=0; sum=0;
  • 43. clrscr(); printf("nnInput ten number both positive and negative:"); for(i=0;i<10;i++) { scanf("%d",&n); if(n>0) { sum+=n; j++;} if(sum>999) break; } printf("nnThe value of positive numbers is:%dnand the countable number is: %d",sum,j); getch(); } Reference: http://hstuadmission.blogspot.com/2010/12/solution-programming-in-ansi-c- chapter.html