Wednesday, December 29, 2010

PROGRAMMING SOLUTION OF C:CHAPTER-3


EXERCISE NO.3.1:  Given the values of the variables X,Y and Z write a program to rotate their values such that X has the value of Y,Y has the value of Z and Z has the value of X.
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z,temp;
clrscr();
printf("Enter the value of x,y,z\n");
scanf("%d %d %d",&x,&y,&z);
temp=x;
x=y;
y=z;
z=temp;
printf("%d %d %d",x,y,z);
getch();
}

EXERCISE NO.3.2:  write a program that reads a floating-point number and then displays right-most digit of the integral part of the number.
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
 int a,e;
 float p;
clrscr();
 printf("Enter the value of p\n");
 scanf("%f",&p);
 a=int(p);
 printf("%d\n",a);
 e=a%10;
 if(a>10)
 printf("%d\n",e);

 getch();
}
EXERCISE NO.3.3. Modify the above program to display to right-most digits of the integral part of the number.
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
 int a,e;
clrscr();
 printf("Enter the value of a\n");
 scanf("%d",&a);
 e=a%100;

 if(a>100)
 printf("%d\n%d\n",a,e);

 getch();
 }


3.4: Write a program that will obtain the length and width of a rectangle from the user and compute its area and perimeter.
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{

  int length,width;
 clrscr();
  float area,perimeter;
  printf("Enter the value of length,width\n");
  scanf("%d %d",&length,&width);
  area=(length*width);
  perimeter=2*(length+width);
  printf("%f %f",area,perimeter);
getch();
  }

EXERCISE NO.3.5: Given an integer number, write a program that displays the number as follows:
First line: all digits
Second line: all except first digit
Third line: all except first two digits
…………
Last line: The last digit
For example the number 5678 will be displayed as:
5 6 7 8
6 7 8
8
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b,c,e,x;
 float p;
 clrscr();
 printf("Enter the value of p\n");
 scanf("%f",&p);
 a=int(p);
 printf("%d\n",a);
 e=a%10000;
 b=e%1000;
 c=b%100;
  x=c%10;
 if(a>10000)
 printf("%d\n%d\n%d\n%d\n",a,e,b,c,x);
 else if(a>1000)
 printf("%d\n%d\n%d\n",a,b,c,x);
 else if(a>100)
 printf("%d\n%d\n",a,c,x);
 else if(a>10)
 printf("%d\n%d\n",a,x);

 getch();
 }


EXERCISE NO.3.6:   The straight-line method of computing the yearly  depreciation of the value of an item is given by
Depreciation=

Write a program to determine the salvage value of an item when the purchase price , years of service, and the annual depreciation are given.
SOLUTION:

#include<stdio.h>
#include<conio.h>
void main()
{
  int years;
  float s, d,p;
 clrscr();
  printf("Enter the value of years,d,p\n");
  scanf("%d %f %f",&years,&d,&p);
  s=p-(years*d);
  printf("%f",s);
  getch();
  }

EXERCISE NO.3.7: Write the program that will read a real number from the keyboard and print the following output in one line:
Smallest integer                  The given               Largest integer
not less then                         number               not greater than
the number                                                          the number
SOLUTION:

#include<stdio.h>
#include<conio.h>
void main()
{
 float m;
 int n,p;
clrscr();
 printf("Enter the value of m\n");
 scanf("%f",&m);
 n=(m/1)+1;
 p=m;
 printf("%d %f %d",n,m,p);
getch();
 }

EXERCISE NO.3.8:  The total distance travelled by a vehicle in t seconds is given by
                Distance= ut+(at2)/2
Where u is the initial velocity( meter per second),a is the acceleration (meter per second2). Write a program to evaluate the distance travelled at intrevales of time, give the value of u and a. the program should provide the flexibility to the user to select his own time intervals and repeat the calculation for different value of u and a.

SOLUTION:
#include<conio.h>
void main()
{
 int a,u,t;
 float distance;
clrscr();
 printf("Enter the value of a,u,t\n");
 scanf("%d %d %d",&a,&u,&t);
 distance=u*t+(a*t*t)/2;
 printf("%f",distance);
getch();
 }

EXERCISE NO.3.9: In inventory management ,the Economic Order Quantity for a single item is given by
       EOQ=sqrt { ( 2*demand rate*setup rate ) / ( holding cost per item per unit time ) }

And the Time Between Orders

     TBO =sqrt { ( 2* setup cost ) / (demand rate * holding cost per item per unit time ) }

SOLUTION 1:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{ float EOQ,d,s,h,x;
Clrscr();
printf("Enter the value of d,s,h\n");
scanf("%f %f %f",&d,&s,&h);
x=(2*d*s)/h;
EOQ=sqrt(x);
printf("%f",EOQ);
getch();
}
SOLUTION 2:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
            float x,s,d,h,TOB;
            clrscr();
            printf("Enter the value of s,d,h\n");
            scanf("%f%f%f",&s,&d,&h);
            x=(2*s)/(d*h);
            TOB=sqrt(x);
            printf("%f",TOB);
            getch();
  }

EXERCISE NO.3.10: For a certain electrical circuit with an inductance L and resistance R,the damped natural frequency is given by

Frequency =sqrt  { (1/L*C ) - ( R*R/4*C*C ) }
It is desired to study the variation of this frequency with C(capacitance).Write a program to calculate the frequency for different values of C starting from 0.01 to 0.1 in steps of 0.01.

SOLUTION:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float L,R,C,x,a,b,F;
clrscr();
printf("Enter the value of L,R,C\n");
scanf("%f %f %f",&L,&R,&C);
a={ (1/L*C) – (R*R/4*C*C) };
F=sqrt(a);
Printf(“%f”,F);
getch();
}

EXERCISE NO.3.11: Write program to read a four digit integer and print the sum of its digit. Hints: Use / and % operators.

SOLUTION:
 #include<stdio.h>
#include<conio.h>
void main()
{
int num,a,b,c,d,x,y,result;
clrscr();
printf("Enter a number");
scanf("%d",&num);
a=num%10;
x=num/10;
b=x%10;
y=x/10;
c=y%10;
d=y/10;
result=a+b+c+d;
printf("%d",result);
getch();
}






EXERCISE NO. 3.12: Write a program to print the size of various data types in C.

SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
int m;
clrscr();
m=sizeof(10);
printf("Size=%d",m);
getch();
}

EXERCISE NO.3.13: Given three values, write a program to read three values from keyboard and print out the largest of them without using if statement.

SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
 int x,y,z,a,b;
 printf("Enter the value of x,y,z\n");
 scanf("%d%d%d",&x,&y,&z);
 printf("largest\n");
 a=(x>y)?x:y
 b=(a>z)?a:z
 printf("%d",b);
 }
EXERCISE NO.3.14: Write a program to read two integer values m and n and to decide and print whether m is multiple of n.

SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n;
printf("Enter m & n,m>=n:");
scanf("%d %d",&m,&n);
if(m%n==0)
  printf("m is a multiple of n");
else
  printf("m is not a multiple of n");
getch();
}

EXERCISE N0.3.15: Write a program to read three values using scanf statement and print the following results:
      (a)Sum of the values
 (b) Average of the three values
 (c) Largest of the three
 (d) Smallest of the three.

SOLUTION:
#include<stdio.h>
#include<conio.h>

void main()

{
  int a,b,c,x,y;
  float sum, average;
 clrscr();
  printf("Enter the value of a,b,c\n");
  scanf("%d%d%d",&a,&b,&c);
  sum=(a+b+c);
  printf("sum=%f\n",sum);
  {
  average=sum/3;
  printf("average=%f\n",average);
            }
              {
              printf("Largest\n");
            x=(a>b)?a:b;
            y=(x>c)?x:c;
            printf("%d\n",y);
             }
                                     {
                                     printf("Smallest\n");
                                     x=(a<b)?a:b;
                                     y=(x<c)?x:c;
                                     printf("%d\n",y);
                                      }
  getch();


}

EXERCISE NO.3.16: The cost of one type of mobile service is Rs.250 plus Rs.1.25 for each call made over and above 100 calls. Write a program to read customer codes and calls made and print the bill for each customer.

SOLUTION:

#include<stdio.h>
#include<conio.h>
void main()
{
int code,call;
float bill;
clrscr();
printf("Enter customer code and number of calls made:");
scanf("%d %d",&code,&call);
bill=250+(call*1.25);
printf("Bill=%f",bill);
getch();
}


EXERCISE NO.3.17: Write a program to print a table of sin and cos functions for the interval 0 180 degrees in increments of 15 as shown below.
-----------------------------------------------------------------------------------------------x(degees)                                            sin(x)                                cos(x)
O                                                           …….                                   …….
15                                                          .……                                   …….
…..                                                         …….                                    …….

SOLUTION:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define p1 3.1416
#define MAX 180

void main()
{

            int i;
            float x,y,z;
          clrscr();
            i=0;
            printf("x(degree)    sin(x)    cos(x)\n");
            while(i<=MAX)
            {
            x=(p1/MAX)*i;
            y=sin(x);
            z=cos(x);
            printf("%d\n %f\n %f\n",i,y,z);
            i=i+15;
            }
  getch();
  }


EXERCISE NO.3.18: Write a program to compute the values of square-roots and squares of the number 0 to 100 in steps 10 print the output in a tabular form as shown below.
-----------------------------------------------------------------------------------------------number                               Square-root                                     square
0                                            0                                                         0
100                                       10                                                        10000

SOLUTION:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
/*......square root and square of numbers 0 to 100.....*/
int i,y;
float x;
clrscr();
printf("Number\tSquare root\tSquare\n\n");
for(i=0;i<=100;i++)
{
x=sqrt(i);
y=i*i;
printf("%d\t%f\t%d\n",i,x,y);
}
getch();
}

EXERCISE NO.3.19: Write a program that determines whether a given integer is odd or even and displays the number and description on the same line.

SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("Enter the integer number:");
scanf("%d",&x);
if(x%2==0)
  printf("THe number %d is even",x);
else
printf("The number %d is odd",x);
getch();
}
EXERCISE NO.3.20: Write a program to illustrate the use of cast operator in a real life situation.

SOLUTION:
include<stdio.h>
#include<conio.h>
void main()
{
float sum;
int n;
clrscr();
sum=0;
for(n=1;n<=10;++n)
{
sum=sum+1/(float)n;
printf("%2d %6.4f\n",n,sum);
}
getch();
}

7 comments:

  1. Thank you for your tutorial and lectures. I am just posting a more simple code to your question 3.2

    EXERCISE NO.3.2: Write a program that reads a floating-point number and then displays right-most digit of the integral part of the number.



    #include

    main()
    {
    float a;
    int num;

    printf("Enter a real number:\t");
    scanf("%f", &a);

    num = (int)a%10;
    printf("Rightmost integer\t %d", num);
    }

    ReplyDelete
  2. Code edit for solution 3.10. Please edit the given values appropriately to suit your solution needs. The code written below is edited and is more in accordance to the question asked.


    #include
    #include

    main()
    {
    float l = 1000.0, r = 500.0;
    float step = 0.01, limit = 0.1, c = 0.01;
    float frequency = 0;

    while(c<=limit)
    {
    frequency = sqrt( (1/l*c) - ((r*r)/(4*c*c)) );
    printf("Step - %f\tFrequency - %f\n", step, frequency);
    c = c+step;
    }
    }

    ReplyDelete
  3. 3.11 A more elegant and easy to understand code for beginners...



    #include

    main()
    {
    int a;

    printf("Enter four digit integer: \t");
    scanf("%d", &a);

    //Remember always division operator gives quotient wheras the modulo operator gives remainder
    int a1 = a/1000; // a = 2356, a1 = 2
    int a2 = a%1000; // a2 = 356
    int a3 = a2/100; // a3 = 3
    int a4 = a2%100; //a4 = 56
    int a5 = a4/10; //a5 = 5;
    int a6 = a4%10; //a6 = 6;

    printf("\n\n%d", a1);
    printf("\n%d", a3);
    printf("\n%d", a5);
    printf("\n%d", a6);
    printf("\n-----------------------------------------------------------------------------------\n\n");

    int sum = a1+a3+a5+a6;
    printf("Sum of Digits - %d", sum);
    }

    ReplyDelete
  4. 3.12 - Write a program to print the size of various data types in C.

    Commentor's note - The code published above by the blogger is flawed. Below I present the correct solution.

    #include

    main()
    {
    int i = sizeof(int);
    int f = sizeof(float);
    int d = sizeof(double);
    int c = sizeof(char);

    printf("Size of data type integer - %d", i);
    printf("\nSize of data type float - %d", f);
    printf("\nSize of data type double - %d", d);
    printf("\nSize of data type char - %d\n\n\n", c);

    }

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Below is may be better solution for 3.5
    #include
    #include
    #include
    int main()
    {
    int a,b,e,f,coun=0;
    printf("Please enter the number:");
    scanf(" %d",&a);
    b=a;
    /*Number size counter */
    while(b!=0){
    b/=10;
    coun++;
    }
    float c= pow(10,coun);
    int d = c;
    //printf("\n %d",d);
    /*counting complete */
    for(e=d,f=1;f<=coun;e/=10,f++){
    printf("\n%d",a%e);
    }
    return 0;
    }

    ReplyDelete