ADD









ANSI C by E. Balagurusamy chapter SIX solution

Chapter 6:Decision Making and Looping



6.1 State whether the following statements are true or false:


           (a)  In a preset loop, if the body is executed n terms,  the test expression is executed                    n+1 times.

            Ans:  True.

            (b) The number of times a control variable is updated always equals the number of                   loop iterations.
            Ans:  True.

            (c)  The do… while statement first executes the loop body and then evaluate the loop                 control expression.
            Ans: True.

            (d) An exit control loop is executed a minimum of a one line.
   Ans:False.


(e) The three loop expressions used in a for loop header must be separated by commas.

            Ans: True.

(f)While loops can be used to replace for loops without any change in the body of the    loop. 
            Ans: False.

            (g)Both the preset loops include initialization within the statement. 
 Ans: True.

            (h) In a for loop expression, the starting value of the control variable must be less                     than its ending value.
  Ans: True.

(i) The initialization, test condition and increment parts may be missing in a for                        statement.
            Ans: False.

(j) The use of continue statement considered as unstructured programming.
Ans: True.

6.2: Fill in the blanks in the following statements.



            (a)The sentinel controlled loop is also; known as ________ loop.
            Ans: indefinite repetition.

            (b) .In a counter controlled loop, variable known as_______  is used to count the                        loop    operation.

            Ans: definite repetition.

            (c) In an exit controlled loop, if body is executed n times, test condition is                                      evaluated_____   ­­­­­­­­­­­­­­­ times.
            Ans: (n-1)

            (d)  A  for loop with the no test condition is known as ________ loop.
            Ans: infinite

            (e)The ___________ statements is use to skip a part of the statements in a loop.  
            Ans: continue



Chapter Seven

7.1 State whether the following statements are true or false.


a). An array can store infinite data of similar types.

b). In declaring an array, the array size can be constant or variable or an expression. (False.)

c) The declaration int x[2]={1,2,3};is illegal.(True.)

d) When an array is declared, C automatically initializes its elements to zero.(True.)

e) An expression that evaluates to an integral value may be used as a subscript.( True.)

f) In C, by default, the first subscript is zero.( True)

g) When initializing a multidimensional array, not specifying all its dimensions is an error.( True.)

h) When we use expression as a subscript, its result should be always greater than zero.( True.)

i) In C, we can use a maximum of 4 dimensions of an array.( False.)

j) Accessing an array outside its range is a compile time error.( True.)

k) A char type variable can not be used as a subscript in an array.( True.)

l) An unsigned long int type can be used as a subscript in an array.( True.)

7.2: Fill in the blanks in the following statements.

a) The variable used as a subscript in an array is popularly known as…… variable.
Ans: index.

b) An array can be initialized either at compile time or at ………..
Ans: run time.

c)……… is the process of arranging the elements of an array in order.
Ans: sorting. 

d) An array that uses more than two subscripts is referred to as …….. array.
Ans: multidimensional

e) An array created using malloc function at run time is referred to as ......array.
Ans: pointer variable.

Chapter Eight 

8.1 State whether the following statements are true or false.

a) When initializing a string variable during its declaration, we must include the null character as part of the string constant, like ‘GOOD\0’. ( False.)

(b) The gets function automatically appends the null character at the end of the string read from the keyboard.(True.)

(c) When reading a string with scanf, it automatically inserts the terminating null character.(True)

(d)The input function gets has one string parameter.( True.)

(e)The function scanf cannot be used in any way to read a line of text with the white spaces.( True.) 

(f) The function getchar skips white-space during input.( False.)

(g) In C, strings cannot be initialized at run time.( False.)

(h)  String variables cannot be used with the assignment operator.(True.)

(i)We cannot perform arithmetic operations on character variables.(True.)

(j) The ASCII character set consists of 128 distinct characters.( False.)

(k)  In the ASCII collating sequence, the uppercase letters precede lowercase letters.( True.)

(l)In C, it is illegal to mix character data with numeric data in arithmetic operations.( False.) 

(m) The function call strcpy (s2, s1); copies string s2 into string s1.(False.)

(n) The function call strcmp ( ‘abc’ , ‘ABC’ ); returns a positive number.(True.)

(o) We can assign a character constant or a character variable to an int type variable.( False.)


programming problem of chapter six

Problem 6.1:  Given a number, write a program using while loop to reverse the digits of the number.For example ,the number 

12345
should be written as 
54321.

solution :

#include<stdio.h>
void main()
{
   int n;
   printf("enter your number to revice\n");
   scanf("%d",&n);


   printf("reverse of your number %d is : ",n);
   while(n!=0){
       printf("%d",n%10);
       n=n/10;
   }
   printf("\n");
}


Problem 6.2: Find factorial of given number

solution:


#include<stdio.h>
void main()
{
   int n,fact=1,temp;
   temp=n;
   printf("enter your number to for factorial\n");
   scanf("%d",&n);


   while(n!=1){
       fact=fact*n;
       n--;
   }
   printf("factorial  %d is :%d \n",temp,fact);
}


Problem 6.3: Write a program to compute the sum of the digits of a given number.



#include<stdio.h>
void main()
{
   int n,digit,sum=0,temp;
   printf("enter your number \n");
   scanf("%d",&n);
   temp=n;


   while(n!=0){
       digit=n%10;
       sum=sum+digit;
       n=n/10;
   }
    printf("sum of all digit %d is: %d\n",temp,sum);
}


6.4 a program to print this 1 1 2 3 5 8 13 21 ……n. This series until n.

#include <stdio.h>
int main()
{
   int i, n, pev_term = 0, rece_term = 1, nextTerm,sum=0;


   printf("Enter the number of terms: ");
   scanf("%d", &n);


   printf("Fibonacci Series is till %d: ",n);


   for (i = 1; i <= n; ++i)
   {
       sum=sum+pev_term;
       printf("%d ",pev_term );
       nextTerm = pev_term + rece_term;
       pev_term = rece_term;
       rece_term = nextTerm;


   }
   printf("\nsum of this serise is :%d\n",sum);
   return 0;
}

6.5 a reversing number using for loop

#include<stdio.h>
int main()
{
   int i,n;
   printf("enter your number to reverce\n");
   scanf("%d",&n);


   printf("reverce of %d is :",n);


   for(i=0;n!=0;i++){
       printf("%d",n%10);
       n=n/10;
   }
}


 6.6 Write a program to evaluate the following investment equation
                            V=P (1+r) nAnd print the tables which would give the values of various combination of the following values of P, r and n.
 P: 1000, 2000, 3000, ……………….10000 r:  0.10, 0.11, 0,12,……….. n: 1,2,3…………….10



#include<stdio.h>
#include<math.h>


void main()
{
   int i,n,p,j;
   double v,r,t;
   printf("\n\n\n");


   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");


           }


}


6.7 pyramid printing (a)

#include<stdio.h>
void main()
{
   int i,j;


   for(i=1;i<=5;i++){
       for(j=1;j<=i;j++){
           printf("%d ",i);
       }
       printf("\n");
   }
}


6.7 pyramid printing (b)

#include<stdio.h>
void main()
{
   int i,j,k;
   int n=6;
   for(i=1;i<=6;i++){
       for(j=1;j<=i;j++){
           printf(" ");
       }
       for(k=1;k<=n;k++){
           printf("* ");
       }
       printf("\n");
       n--;
   }
}


6.8 age group



#include<stdio.h>
void main()
{
   int i,age,count=0;


   printf("Enter age of 100 people\n");
   for(i=1;i<=10;i++){
       scanf("%d",&age);
       if(age>=50&&age<=60)
       {
           count++;
           continue;
       }
   }
   printf("%d people in age 50 to 60 group\n",count);
}


6.9
#include<stdio.h>
#include<math.h>
void main()
{
   
   int i;
   float a,x,y1,y2;
   a=0.4;
 printf("\n\n");
   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");
}


6.10 table

#include<stdio.h>
#include<math.h>


void main()
{


   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");
  }


}


6.11 binary number

#include<stdio.h>
int main()
{
    int ar[10],n,i=0;
    printf("Enter your number to find binary\n");
    scanf("%d",&n);


    printf("binary of %d is :",n);
     while(n!=0)
    {
     ar[i]=n%2;
       i++;
     n=n/2;
    }


      for(i=i-1;i>=0;i--)
                     {
     printf("%d",ar[i]);
   }
   return 0;
}


6.12 print capital S



#include<stdio.h>


void main()
{
   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++)
                   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");
       }
    }


}


6.13 Eulur’s number



#include<stdio.h>
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;


   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("\n\nthe result = %f",e);
}


6.14 function(a sinx)

/*……………….sinx function………………..*/
#include<stdio.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;


   printf("angle sin x=\n");
   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= %0.2lf",sin);


}


6.14 function(b cosx)

/*……………….cosx function………………..*/
#include<stdio.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,cos,power=3;


   printf("angle  cos x=\n");
   scanf("%lf",&x);
   term=x;
    cos=x;
     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= %0.2lf",cos);


}


6.14 function(c sum)

#include<stdio.h>
#include<math.h>
void main()
{
   double term,demo,lob,sum;
   term=1.0;
    sum=1.0; lob=1.0;demo=2.0;
    while(term>=0.0001)
   {
   term=lob/demo;
    term=pow(term,demo);
    sum+=term;
    demo++; printf("%lf",sum);
   }
  printf("the sum= %lf",sum);


}


6.15
#include<stdio.h>
#include<math.h>
void main()
{


   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);
}


6.16( a)

#include<stdio.h>
#include<math.h>
void main()
{


   int i,j;


   for(i=0;i<5;i++){
       for(j=0;j<5;j++){
           printf("S ");
       }
       printf("\n");
   }
}


6.16(b)

#include<stdio.h>
void main()
{
    int i,j,n;


   n=5; printf("\n\n");
   for(i=1;i<=n;i++)
   printf("s ");
   printf("\n\n");
   for(i=1;i<=n-2;i++)
   {
   printf("s ");
   for(j=1;j<=n-2;j++)
      printf("  ");
      printf("s\n\n");
   }
   for(i=1;i<=n;i++)
      printf("s ");
      printf("\n");


}


6.17



#include<stdio.h>
#include<math.h>
void main()
{
   float y;
   int x,i;


   printf("X      Sin(X)\n");
   for(i=0;i<=180;i+=15)
       {
           x=i;
       y=sin(x);
       printf("%d     %0.3f\n",x,y);
   }
}


6.18

#include<stdio.h>


void main()
{
   int i,count,sum;


   sum=0;
   count=0;
       for(i=1;i<=100;i++)
          {
            if(i%2!=0&&i%3!=0)
              {
               sum+=i;
                count++;
               printf("%d\n",i);
              }
          }
    printf("the sum of the value is :%d \nthe countable numbers is: %d\n",sum,count);


}


6.19

#include<stdio.h>


void main()
{
  int n,i,j,mid;


  n=5;
    mid=(int)(n/2);
   for(i=0;i<n;i++)
      {
          for(j=0;j<n;j++)
             {
              if(i==mid&&j==mid)
                   printf("0 ");
               else
                   printf("S ");
               }
              printf("\n\n");
       }


}


6.20

 #include<stdio.h>
void main()
{
   int sum,n,i,j=0;
   sum=0;


  printf("\n\nInput 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("\n\nThe value of positive numbers is:%d\nand the countable                                                                                   number is: %d",sum,j);

}




















লিখেছেন ,
 Md. Rusul Azom Sumon
 CEO & FOUNDER azomTech


 (লেখাটি লেখক কতৃক সংরক্ষিত কপি করা কপি করে পরিমার্জন করে অন্যকোথোও প্রকাশ করা সম্পূর্ণ নিষিদ্ধ ,কিন্তু অবাণিজ্যিক উদ্যেশে(শিক্ষার জন্য ) লেখকের অনুমতি নিয়ে ব্যবহার করা যাবে )

No comments

Theme images by lishenjun. Powered by Blogger.