ADD









ANSI C by E. Balagurusamy chapter FIVE solution



5.1: STATE WHETHER THE FOLLOWING STATEMENTS ARE  TRUE OR FALSE:

(a) A switch expression can be of any type.

Ans: False.


(b).A program stops its execution when a break statement is encountered.

Ans: False.


(c Each case label can have only one statement.

Ans: True.


(d).The default case is required in the switch statement.

Ans: True.


(e)When if statements are nested , the last else gets associated with the nearest

if without an else.

Ans: False

(f)One if can have more than one else clause.

Ans: False.


(g)Each expression in the else if must test the same variable.

Ans: True.


(h)A switch statement can always be replaced by a series of if..else statements.

Ans: False.


(i)Any expression can be used for the if expression.

Ans: True


(j)The predicate! ( (x>=10) (y==5) ) is equivalent to (x<10) && (y!=5 ).Ans: True.




5.2  Fill in the blanks with appropriate words:


(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 structureAns: break.


(d) The expression !(x!=y)can be replaced by the expression…………

Ans: x==y.


(e) The ternary conditional expression using the operator ?: code be easily

coded using ………..statement.Ans: if…else.


PROGRAMMING EXERCISES 



 problem 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 the else option.      (b) With else option.
#include<stdio.h>
void main()
{

     int n;
    printf("enter your number \n");
    scanf("%d",&n);
 
    if(n%2==0){
        printf("NUMBER IS EVEN\n",n);
    }else{
         printf("NUMBER IS ODD\n",n);
    }
}




 problem 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.


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

     int i,sum;
 
    printf(" number are\n");
    for(i=100;i<=200;i++){
        if(i%7==0){
        printf(" %d",i);
        sum=sum+i;
        }
 
    }
    printf("\n sum of this number is:%d",sum);
 
}

problem 5.3: A set of two linear equation two unknows x1 and x2 is given below:

                                                           ax1 + bx2 = m                                    cx1 + dx2 = n      The set has a unique solution                                       x1=(md-bn)/(ad-cb)                                      x2=(na-mc)/(ad-cb)


#include<stdio.h>
void main()
{
int a,b,m,c,d,n;
float x1,x2,den;
printf("enter value of 1st liner equation a,b,m\n");
        scanf("%d%d%d",&a,&b,&m);
printf("enter value of 2nd liner equation c,d,n\n");
        scanf("%d%d%d",&c,&d,&n);

den=(a*d-c*b);
if(den!=0){
x1=(m*d-b*n)/den;
x2=(n*a-m*c)/den;

printf("x1=%0.2f\n",x1);
        printf("x2=%0.2f\n",x2);
}else{
printf("both equation are parallel,so no x1,x2");
}
}

 problem 5.4 

Given the list of marks ranging from 0 to 100,write a program to compute and print the 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

(h) in the range 0 to 40

#include<stdio.h>
void main()
{
int n,num[100],i,j,c;

printf("enter number of stusdent\n");
        scanf("%d",&n);
printf("enter mark of all student\n");

        for(i=0;i<n;i++){
scanf("%d",&num[i]);
}
for(j=80;j>=40;j=j-20){
        c=0;
for(i=0;i<n;i++){
            if(num[i]>j){
         c++;
            }
}
printf("%d stident got more then %d\n",c,j);

    }

    c=0;
for(i=0;i<n;i++){
if(num[i]<40)
c++;
}
printf("%d stident got less then 40\n",c);

c=0;
for(j=80;j>=40;j=j-20){
        c=0;
for(i=0;i<n;i++){
            if(num[i]>j&&num[i]<=j+20){
         c++;
            }
}
printf("%d stident got %d to %d marks\n",c,j,j+20);

    }
c=0;
for(i=0;i<n;i++){
if(num[i]<=40)
c++;
}
printf("%d stident got 0 to 40 marks\n",c);


}


problem 5.5:  Admission to a professional course in subject to the following conditions:

a) Marks in mathematics >=60b) Marks in Physics >=50c) Marks in Chemistry >=40d) Total in all three subjects >=200orTotal in mathematics and physics>=150.Given the marks in the three subjects, write a program to process the applications to the eligible candidates. 


#include<stdio.h>
void main()
{
int n,m,p,c,can[100],i,j;

printf("enter how many candidate\n");
scanf("%d",&n);

     printf("Enter mark of all candidate math,physics and chemistry\n");
for(i=1;i<=n;i++){
           scanf("%d%d%d",&m,&p,&c);
           if((m>=60&&p>=50&&c>=40)&&((m+p+c)>=200||(m+p)>=150))
        printf("candidate %d is Eligible\n",i);

}
}

problem 5.6:

  Write a program to print a two-dimensional Sqaure Root Table as shown below, to provide the square root of any number from 0 to 9.9

                                           Square Root table Number         0.0   0.1  0.2……………………0.90.01.03.0                                  x                                   y9.0


#include<stdio.h>
#include<math.h>
int main()
{
    double i,j,sq;
    for(i=0;i<=9;i++){
        for(j=0;j<=0.9;j=j+0.1){
            sq=sqrt(i+j);
            printf("%0.2lf\t",sq);
        }
        printf("\n");
    }
}

 problem 5.7(a)

#include<stdio.h>
int main()
{
    int i,j,n=1;
    for(i=1;i<=13;i++){
        for(j=1;j<=i;j++){
            printf("%d ",n);
        n++;
        }
        printf("\n");
    }
}

 problem 5.7(b)

#include<stdio.h>
int main()
{
    int i,j,n=1;
    for(i=1;i<=5;i++){
        for(j=1;j<=i;j++){
           (i+j)%2==0? printf("1 "):printf("0 ");
        }
        printf("\n");
    }
}

 problem 5.8(purchase amount calculating)

#include <stdio.h>
void main()
{
int ch, pamnt,p,dis;
printf("press  1 for mill cloth\npress 2 for Handloom items\n");
printf("\npress 0 for End:\n");
input:
scanf("%d",&ch);
printf("enter purchase amount\n");
scanf("%d",&pamnt);
if(ch==0)
goto end;
switch(ch)
{
case 1: if(pamnt<=100)
 dis=0;
 else if(pamnt<=200)
 dis=pamnt*0.05;
 else
 if(pamnt<=300)
 dis=pamnt*0.075;
 else
 if(pamnt>300)
 dis=pamnt*0.10;
 break;
case 2: if(pamnt<=100)
 dis=pamnt*0.05;
 else if(pamnt<=200)
 dis=pamnt*0.075;
 else
 if(pamnt<=300)
 dis=pamnt*0.10;
 else
 if(pamnt>300)
 dis=pamnt*0.15;
 break;
 default: printf("your choise is Error ");
 goto end;
 }
p=pamnt-dis;
printf("You have to pay %d",p);
goto input;
end:
printf("welcome");
}

 problem 5.9:  

Write a program that will read the value of x and evaluate the following function
Y=     1 for x>0          0 for x=0         -1 for x<0 Using(a)   Nested if statements(b)   Else if statements(c)    Conditional operators

Nested if(a)


#include<stdio.h>
void main()
{
    int x,y;
    printf("Enter value of x\n");
    scanf("%d",&x);
    if(x>0){
        y=1;
    printf("for x=%d value of  y=%d\n",x,y);
        if(x==0){
                y=0;
            printf("for x=%d value of y=%d\n",x,y);
        }
    }
    if(x<0){
        y=-1;
    printf("for x=%d value of y=%d\n",x,y);
        if(x==0){
                y=0;
            printf("for x=%d value of y=%d\n",x,y);
        }
    }
}


Using if  (b)

#include<stdio.h>
void main()
{
    int x,y;
    printf("Enter value of x\n");
    scanf("%d",&x);
    if(x>0){
        y=1;
    printf("for x=%d value of  y=%d\n",x,y);
        if(x==0){
                y=0;
            printf("for x=%d value of y=%d\n",x,y);
        }
    }
    if(x<0){
        y=-1;
    printf("for x=%d value of y=%d\n",x,y);
        if(x==0){
                y=0;
            printf("for x=%d value of y=%d\n",x,y);
        }
    }
}
}

Conditional operator(c)

#include<stdio.h>
void main()
{
    int x,y;
    printf("Enter value of x\n");
    scanf("%d",&x);
    (x>0)?printf("for x=%d value of y=1\n",x,y):(x==0)?printf("for x=%d value of y=%d\n",x,x):printf("for x=%d value of y=-1\n",x);
}

problem 5.10:

 Write a program to compute the real roots of a quadratic equation

  ax2 + bx+c=0          The roots are given by the equations: X1=(-b+sqrt(b*b-4*a*c))/(2*a) X2=(-b-sqrt(b*b-4*a*c))/(2*a) The program should request for the values of the constants a,b and c and print the values of x1 and x2. Use the following rules:(a)   No solution , if both a nd b are zero(b)   There is only one root,if a=0 (x=-c/b)(c)    There is no real root if b*b-4ac is negative(d)   Otherwise there are real roots. Test your program with appropriate data.
#include<stdio.h>
void main()
{
        int x1,x2,a,b,c,v,x;
        printf("enter value of your quadric equation a,b,c\n");
        scanf("%d%d%d",&a,&b,&c);
       v=sqrt(b*b-(4*a*c));
       x=-(c/b);
       x1=-b+(v/(2*a));
        x2=-b-(v/(2*a));
        
        if(a==0&&b==0) {
                printf("there are no roots for this quDRIC Equation\n");
        }else if(a==0)
        {
             printf("there are only one roots for this quDRIC Equation x= %d\n",x);
        } else if(v<0){
                
            printf("there are two unreal  roots for this quDRIC Equation x1= %d\nx2=%d",x1,x2);
            }else{
                   printf("there are two real  roots for this quDRIC Equation x1= %d\nx2=%d",x1,x2);
            }
                
}

problem 5.11:

 Write a program to read three integer values from the keyboard and display the

        output stating that they are the sides of right-angled triangle.


#include<stdio.h>
void main()
{
        int a,b,c,max,min,total,mid,sum;
        printf("enter value of three side of your triangel\n");
        scanf("%d%d%d",&a,&b,&c);
        total=a+b+c;
        max=a;
        if(b>max)
        max=b;
        if(c>max)
        max=c;

        min=a;
        if(b<min)
            min=b;
        if(c<min)
            min=c;

        mid=total-(max+min);

        max=max*max;
        min=min*min;
        mid=mid*mid;
        sum=mid+min;

        if(sum==max)
           printf("they are side of a rightangle triangel\n");
        else
           printf("they are not side of a rightangle triangel\n");

}

problem 5.12:

 An electricity board charges the following rates for the use of electricity:             For the first 200 units; 80 P per unit            For the next 100 units; 90 P per unit            Beyond 300 units; Rs. 1 per unitAll 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 printout the charges with names.


#include<stdio.h>
void main()
{
        double n,total,extra;
        char name[100];
        printf("enter your name :\n");
        scanf("%[^\n]",&name);
        printf(" %s enter your uses unit:\n",name);
        scanf("%lf",&n);
        if(n<=200){
            total=(n*80)+100;
            }
        if(n>200&&n<=300) {
            total=(n*90)+100;
            }
        if(n<300){
            total=(n*1)+100;
            }
        if(total>400){
            extra=total*0.15;
            total=total+extra;
            }
        printf("%s your bill is %0.2lf tk only\n",name,total);
}

problem 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 and 100. The program should also count and       display the number of such values.


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

    for(i=0;i<=100;i++)
        {
       if(i%6==0&&i%4!=0){
      sum=sum+i;
      printf("%d ",i);
       }
    }

    printf("\nsum of those number devideable by 6 but not 4 is %d\n",sum);

}


problem 5.14: 1st part:

 Write an interactive program that could read a positive integer number and decide whether the number is prime number and display the output accordingly. Modify the program to count all the prime numbers that lie between 100 and 200.



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

    for(i=2;i<n;i++)
        {
       if(n%i==0){
      c++;
       }
        }
       if(c!=0)
       {
        printf("%d is a not prime number \n",n);
       }
      else
        {
         printf("%d is a prime number \n",n);

        }

}


problem 5.14: second part:

#include<stdio.h>
void main()
{
    int i,j,prime=0;
   for(i=100;i<=200;i++)
    {
        for(j=2;j<i;j++)
            {
        if(i%j==0)
        break;
    }
    if(i==j)
       prime++;
    }
    printf("There are %d prime number between 100 to 200\n",prime);

}

problem 5.15: 

 Write a program to read a double-type value x that represent angle in radiant 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 assigned to T,b)     Cos(x), if c or C is assigned to T, andc)      Tan(x), if t or T is assigned to T. Using (i) if……….else statement and (ii) switch statement.


#include<math.h>
void main()
{
    double x,temp,sum=0;
    char s,S,T,t,c,C;

    printf("enter s or S for sin(x)\nenter t or T for tan(x)\nenter c or C for cos(x)\n");
     scanf("%c",&T);
    printf("enter angle\n");
    scanf("%lf",&x);
    temp=x;

    x=(x*3.14159)/180;

   if(T=='s'||T=='S'){
    x=sin(x);
    printf("sin%0.2lf=%0.3lf\n",temp,x);
   }
   else if(T=='c'||T=='C'){
    x=cos(x);
    printf("cos%0.2lf=%0.3lf\n",temp,x);
   }else{
    x=tan(x);
    printf("tan%0.2lf=%0.3lf\n",temp,x);
   }

}

5.15 using switch case


#include<stdio.h>
#include<math.h>
void main()
{
    double x,temp,sum=0;
    char s,S,T,t,c,C;

    printf("enter s or S for sin(x)\nenter t or T for tan(x)\nenter c or C for cos(x)\n");
     scanf("%c",&T);
    printf("enter angle\n");
    scanf("%lf",&x);
    temp=x;

    x=(x*3.14159)/180;

    switch(T){

    case 's':

            x=sin(x);
            printf("sin%0.2lf=%0.3lf\n",temp,x);
            break;
    case 'S':

            x=sin(x);
            printf("sin%0.2lf=%0.3lf\n",temp,x);
            break;

    case 'c':

             x=cos(x);
            printf("cos%0.2lf=%0.3lf\n",temp,x);
            break;

    case 'C':
            x=cos(x);
            printf("cos%0.2lf=%0.3lf\n",temp,x);
            break;


    case 't':
            x=tan(x);
            printf("tan%0.2lf=%0.3lf\n",temp,x);
            break;

    case 'T':
        x=tan(x);
        printf("tan%0.2lf=%0.3lf\n",temp,x);
        break;
    }

}





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


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

No comments

Theme images by lishenjun. Powered by Blogger.