Sunday 3 August 2014

PROGRAMMING EXERCISE 3

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.

     #include<stdio.h>
     #include<conio.h>
     void main()
      {
                    int x,y,z,dummy;
          clrscr();

                   printf("Enter values of x,y and z:");
                   scanf("%d%d%d",&x,&y,&z);

                  dummy=x;
                  x=y;
                  y=z;
                  z=dummy;
               
                  printf("x=%d\ny=%d\nz=%d\n",x,y,z);
          getch();
      }


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

     #include<stdio.h>
     #include<conio.h>
     void main()
      {
                      float num;
                      int i,last;
            clrscr();
                      printf("Enter a floating point number:");
                      scanf("%f",&num);

                      i=num;

                      while(i>0)
                       {
                                        last=i%10;
                                        break;
                       }

                     printf("%d\n",last);
            getch();
      }


3.3) Modify the above program to display the two right-most digits of the integral part of the number.

     #include<stdio.h>
    #include<conio.h>
    void main()
     {
                   int num,slast,i;
           clrscr();
 
                  printf("Enter an integral number:");
                  scanf("%d",&num);

                 i=num;
                 while(i>0)
                  {
                               slast=i%100;
                               break;
                  }

                printf("%d\n",slast);
        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.

     #include<stdio.h>
     #include<conio.h>
     void main()
      {
                    int length,width,area,per;
            clrscr();

                   printf("Enter length and width of rectangle:");
                   scanf("%d%d",&length,&width);

                   per=2*(length+width);
                   area=length*width;

                   printf("Perimeter=%d\n",per);
                   printf("Area=%d\n",area);
            getch();
      }


3.5) Given an integer number,write a program that displays the numbers 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
     7 8
     8

     #include<stdio.h>
     #include<conio.h>
     void main()
      {
                         int num,i,a,b,c;
                clrscr();

                        printf("Enter a four digit number:");
                        scanf("%d",&num);

                        a=num%1000;
                        b=a%100;
                        c=b%10;

                        printf("%d\n%d\n%d\n%d\n",num,a,b,c);
              getch();
      }


3.6) The straight line method of computing the yearly depreciation of the value of an item is given by:

                Depreciation = Purchase Price - Salvage Value 
                                                Years of Service
       Write a program to determine the salvage value of an item when the purchase price,years of service, and the annual depreciation are given.

     #include<stdio.h>
     #include<conio.h>
     void main()
      {
                        int dep,sal,pur,year;
                clrscr();
                   
                       printf("Enter purchase price of item:"):
                       scanf("%d",&pur);

                       printf("Enter years of service:");
                       scanf("%d",&year);

                       printf("Enter annula depreciation:");
                       scanf("%d",&dep);

                       sal=pur - (dep*year);

                       printf("Salvage Value=%d\n",sal);
            getch();
      }


3.7) Write a program that will read a real number from the keyboard and print the following output in one line:
       Smalllest integer                          The given                                 Largest integer
       not less than                                number                                    not greater than
       the number                                                                                 the number

     #include<stdio.h>
     #include<conio.h>
     void main()
      {
                            float m;
                            int n,p;
                 clrscr();

                           printf("Enter the value of m:");
                           scanf("%f",&m);

                          n=(m/1)+1;
                          p=m;

                          printf("%d%f%d",n,m,p);
               getch();
       }


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(metres per second),a is the acceleration(metres per second2).Write a program to evaluate the distance travelled at regular intervals of time,given the values of u and a.The program should provide the flexibility to the user to select his own time intervals and repeat the calculations the different values of u and a.

     #include<stdio.h>
     #include<conio.h>
     void main()
      {
                         float u,d,a,t;
               clrscr();

                        printf("Enter initial velocity:");
                        scanf("%f",&u);
                        printf("Enter acceleration:");
                        scanf("%f",&a);
                        printf("Enter time taken in seconds:");
                        scanf("%f",&t);

                       d=(u*t) + (a*t*t)/2;

                       printf("Distance Travelled=%f\n",d);
             getch();
      }


3.9) In inventory management,the Economic Order Quantity for a single item is given by:

                    EOQ=sqrt[(2 x demand rate x setup costs)/(holding cost per item per unit time)]
      And the optimal Time Between Orders
               
                    TBO=sqrt[(2 x setup costs)/(demand rate x holding cost per unit time)]

      Write a program to compute EOQ and TBO,given demand rate(items per unit time),setup costs(per order),and the holding cost(per item per unit time).

     #include<stdio.h>
     #include<conio.h>
     #include<math.h>
     void main()
      {
                           int drate,scost,hcost;
                           float eoq,tbo,xeoq,xtbo;
               clrscr();

                          printf("Enter Demand Rate:");
                          scanf("%d",&drate);
                          printf("Enter setup cost:");
                          scanf("%d",&scost);
                          printf("Enter holding cost:");
                          scanf("%d",&hcost);

                          xeoq=(float)(2*drate*scost)/(hcost);

                          eoq=sqrt(xeoq);

                          xtbo=(float)(2*scost)/(drate*hcost);

                          tbo=sqrt(xtbo);

                          printf("Economic Order Quality=%f\n",eoq);
         
                          printf("Optimal Time Between Orders=%f\n",tbo);
              getch();
      }


3.10) For a certain electrical circuit with an inductance L and resistance R,the damped natural frequency is given by:
                Frequency=sqrt[(1/LC) - (R2/4C2)]
     
        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.

     #include<stdio.h>
     #include<conio.h>
     #include<math.h>
     void main()
      {
                         float L,R,freq,i,xfreq;
                clrscr();
                         printf("Enter Inductance L:");
                         scanf("%f",&L);
                         printf("Enter Resistance R:");
                         scanf("%f",&R);

                         for(i=0;i<=0.1;i+=0.01)
                          {
                                   xfreq=1/(L*i) - (R*R)/(4*i*i);
                                   freq=sqrt(xfreq);

                                   printf("Frequency=%f\n",freq);
                          }
              getch();
      }


3.11) Write a program to read a four digit integer and print the sum if its digits.
          HINT : Use / and % operators.

       #include<stdio.h>
       #include<conio.h>
       void main()
        {
                            int no,last,sum=0;
                    clrscr();
                            printf("Enter a 4-digit number:");
                            scanf("%d",&no);

                            while(no!=0)
                             {
                                       last=no%10;
                                       sum=sum+last;
                                       no=no/10;
                             }

                            printf("Sum=%d\n",sum);
                    getch();
        }


3.12) Write a program to print the size of various data types in C.
                 
        #include<stdio.h>
        #include<conio.h>
        void main()
         {
                                int size,no;
                       clrscr();

                               printf("Enter number:");
                               scanf("%d",&no);

                               size=sizeof(no);

                               printf("Size=%d\n",size);
                    getch();
         }


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.

       #include<stdio.h>
       #include<conio.h>
       void main()
        {
                            int x,y,z,a,b;
                 clrscr();
                            printf("Enter the value of x,y,z:");
                            scanf("%d%d%d",&x,&y,&z);

                            a=(x>y)?x:y;
                            b=(a>z)?a:z;

                            printf("%d",b);
               getch();
        }


3.14) Write a program to read two integer values of m and n and to decide and print whether m is a multiple of n.

      #include<stdio.h>
      #include<conio.h>
      void main()
       {
                           int m,n;
                 clrscr();
                           printf("Enter m and n:");
                           scanf("%d%d",&m,&n);

                           if(m%n==0)
                            {
                                     printf("m is a multiple of n\n");
                            }

                           else
                            {
                                     printf("m is not a multiple of n\n");
                            }
                   getch();
        }


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

      #include<stdio.h>
      #include<conio.h>
      void main()
       {
                              int no1,no2,no3,sum,a,b;
                   clrscr();
                              printf("Enter no1,no2 and no3:");
                              scanf("%d%d%d",&no1,&no2,&no3);

                              sum=no1+no2+no3;

                              avg=(float)(no1+no2+no3)/3;

                              printf("Sum=%d\n",sum);
                              printf("Average=%f\n",avg);

                              a=(no1<no2)?no1:no2;
                              b=(a<no3)?a:no3;

                              printf("Smallest of three=%d\n",b);
                     getch();
       }


3.16) The cost of one type of mobile service is Rs. 250 plus Rs. 1.25 fro 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.

        #include<stdio.h>
        #include<conio.h>
        void main()
         {
                               int code,calls,amt,x=1.25;
                   clrscr();
                               printf("Enter code:");
                               scanf("%d",&code);
                               printf("Enter calls made:");
                               scanf("%d",&calls);

                               if(calls>100)
                                {
                                             amt=250 + (calls*x);
                                }

                               else
                                {
                                             amt=250;
                                }

                               printf("Customer Code                  Calls Made                  Total Amount\n");
                               printf("%d                                          %d                               %d\n",code,calls,amt);
                  getch();
         }


3.17) Write a program to print a table of sin and cos functions for the interval from 0 to 180 degrees in increments of 15 as shown below.

        #include<stdio.h>
        #include<conio.h>
        #include<math.h>
        #define pi 3.14
        #define max 180
        void main()
         {
                              int i;
                              float sine,cosine,x;
                  clrscr();

                              for(i=0;i<=max;i+=15)
                               {
                                           x=(float)(pi/max)*i;
                                           sine=sin(x);
                                           cosine=cos(x);

                                           printf("%d\t%f\t%f\n",i,sine,cosine);
                              }
                     getch();
         }


3.18) Write a program to compute the values of square-roots and squares of the numbers 0  to 100 in steps and print the output in a tabular form.

       #include<stdio.h>
       #include<conio.h>
       #include<math.h>
       void main()
        {
                           int i,sqr;
                           float sqroot;
                  clrscr();
     
                           printf("Number\tSquare-root\tSquare\n");
                         
                           for(i=0;i<=100;i+=10)
                            {
                                         sqroot=sqrt( (float) (i) );
                                         sqr=i*i;
                                         printf("%d\t%f\t%d\n",i,sqroot,sqr);
                            }
                 getch();
          }


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.

        #include<stdio.h>
        #include<conio.h>
        void main()
         {
                                int no;
                       clrscr();

                                printf("Enter number:");
                                scanf("%d",&no);

                                if(no%2==0)
                                 {
                                               printf("%d is even\n",no);
                                 }
                                else
                                 {
                                               printf("%d is odd\n",no);
                                 }
                        getch();
          }

No comments:

Post a Comment