Chapter - 6
Chapter - 6
Problem No-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.
Problem No-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 there 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.
Problem No-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
Problem No-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.
Problem No-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 Dinar\n”);
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);
}
Problem No-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);
Problem No-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”);
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
Problem No-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
Problem No-6.20:When do we use the following statement?
for(; ;)
Ans : When we need an infinity loop the statement for(; ;) can be used.
Problem No-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<stdio.h>
#include<conio.h>
void main()
{
clrscr();
long int n,b;
printf("\n\n\n 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 No-6.3:Write a program that compute the sum of the digit of a given
integer number.
Solution:
/*………………..sum of given integer number………………….*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
long int n,b,sum=0;
printf("\n\n\nInput 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 No-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<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int m,x,y,n,z;
m=0;
x=0;
y=1;
printf("\n\nInput 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);
getch();
}
Problem No-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<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int x,y,n,z,i;
x=0;
y=1;
printf("\n\nInput 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 No-6.6:Write a program to evaluate the following investment
equation
V=P(1+r)n
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<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
int i,n,p,j;
double v,r,t;
printf("\n\n\n");
clrscr();
r=0.10;
n=10;
p=1000;
t=pow((1+r),i);
// printf("\n\n%.6lf ",t);
printf("\nP R N V\n");
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 No-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<stdio.h>
#include<conio.h>
Void main()
{
int i,j,n;
clrscr();
printf("\n\n 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 No-6.7(b): Write programs to print the following outputs using for
loops.
*****
****
***
**
*
Solution:
/*………………………….star…………………….….*/
#include<stdio.h>
#include<conio.h>
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 No-6.8:Write a program to read the age of 100 persons and count the
number of persons in the age group 50to 60. Use for and continue statements.
Solution:
/****************age count into 50 to 60***************/
#include<stdio.h>
#include<conio.h>
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 No-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<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr(); printf("\n\n");
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("0\n");
}
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 No-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<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
float y;
printf("\n\n....................................\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 No-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<stdio.h>
#include<conio.h>
#include<math.h>
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 No-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<stdio.h>
#include<math.h>
#include<conio.h>
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 No-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<stdio.h>
#include<conio.h>
#include<math.h>
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();
}
Problem No-6.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<stdio.h>
#include<conio.h>
#include<math.h>
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();
}