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

Monday 21 July 2014

Programming exercise 2

2.1) Write a program to determine and print the sum of the following harmonic series for a given value of n:
                           1 + 1/2 + 1/3 + ...... + 1/n

       #include<stdio.h>
       #include<conio.h>
       void main()
        {
                int n,i;
                float sum;
           clrscr();
                printf("Enter the value of n:");
                scanf("%d",&n);

                for(i=1;i<=n;i++)
                 {
                          printf("1/%d+",i);
                          sum=sum+float()1/i;
                 }

               printf("\nSum=%f\n",sum);
          getch();
        }


2.2)Write a program to read the price of an item in decimal form(like 15.95) and print the output in paise(like 1595 paise).
   
     #include<stdio.h>
     #include<conio.h>
     void main()
      {
                       int ans;
                       float price;
              clrscr();
                       price=15.95;

                      printf("Price of an item:%f\n",price);

                      ans=100*price;

                      printf("In paise=%d\n",ans);
            getch();
      }


2.3) Write a program that prints the even numbers form 1 to 100.

        #include<stdio.h>
        #include<conio.h>
        void main()
         {
                           int i;
                clrscr();
       
                          for(i=2;i<=100;i++)
                           {
                                     printf("%d",i);
                           }
               getch();
          }


2.4) Write a program that requests two float type numbers from the user and then divides the first number by the second and display the result along with the numbers.

        #include<stdio.h>
        #include<conio.h>
        void main()
          {
                             float num1,num2,ans;
                 clrscr();
                             printf("Enter two float numbers:");
                             scanf("%f%f",&num1,&num2);

                             printf("Number1=%f\n",num1);
                             printf("Number2=%f\n",num2);

                             printf("Answer=%f\n",ans);
                getch();
           }


2.5)The price of 1kg of rice is Rs. 16.75 and 1kg of sugar is Rs. 15. Write a program to get these values from the user and display the prices as follows:

         ***LIST OF ITEMS***
         Item                    Price
         Rice                    Rs. 16.75
         Sugar                  Rs. 15.00

       #include<stdio.h>
       #include<conio.h>
       void main()
        {
                           float rice,sugar;
                           rice=16.75;
                           sugar=15;
                clrscr();

                           printf("Price of 1kg of rice=%f\n",rice);
                           printf("Price of 1kg of sugar=%f\n",sugar);

                           printf("***LIST OF ITEMS***\n");
                           printf("Item                    Price\n");
                           printf("Rice                    Rs. 16.75\n");
                           printf("Sugar                  Rs. 15.00\n");
               getch();
        }


2.6) Write program to count and print the number of negative and positive numbers in a given set of numbers. Test your program with a suitable set of numbers. Use scanf to read the numbers. Reading should be terminated when the value 0 is encountered.

        #include<stdio.h>
        #include<conio.h>
        void main()
         {
                              int totnum,num,i,pcount=0,ncount=0;
                 clrscr();
                              printf("Enter the total numbers you want to enter:");
                              scanf("%d",&totnum);

                              for(i=1;i<=totnum;i++)
                               {
                                             printf("Enter number:");
                                             scanf("%d",&num);
 
                                            if(num>0)
                                             {
                                                             printf("Positve\n");
                                                             pcount++;
                                             }

                                            else if(num<0)
                                             {
                                                             printf("Negative\n");
                                                             ncount++;
                                             }

                                            else if(num==0)
                                             {
                                                             break;
                                             }
                         }

                            printf("Total number of +ve numbers=%f\n",pcount);
                            printf("Total number of -ve numbers=%f\n",ncount);
                getch();
            }


2.7) Write a program to do the following:
      (a) Declare x and y as integer variables and z as a short integer value.
      (b) Assign two 6 digit numbers to x and y.
      (c) Assign the sum of x and y to z.
      (d)Output the values of x,y and z.

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

                              z=x+y;
                              printf("X=%d\n",x);
                              printf("Y=%d\n",y);

                             printf("Z=%u\n",z);
                    getch();
        }


2.8) Write a program to read two floating point numbers using a scanf statement,assign their sum to an integer variable and then output the values of all three variables.

       #include<stdio.h>
       #include<conio.h>
       void main()
        {
                                  float num1,num2;
                                  int sum;
                    clrscr();
                                 printf("Enter two floating point numbers:");
                                 scanf("%f%f",&num1,&num2);

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


Monday 14 July 2014

Programming exercise 1

1.1) Write a program that will print your mailing address  in the following form:

                 First Line            :      Name
                 Second line         :      Door No, Street
                Third line             :      City, Pin Code

     #include<stdio.h>
     #incude<conio.h>
     void main()
      {
           clrscr();
                        printf("First Line : Name\nSecond line : Door No,Street\nThird line : City,Pin Code");
          getch();
      }

1.2) Modify the above program to provide border lines to the address.

     #include<stdio.h>
     #include<conio.h>
     void main()
      {
          clrscr();
                       printf("*************************\n");
                       printf("* First line : Name                     *\n");
                       printf("* Second line : Door No, Street*\n");
                       printf("* Third line : City, Pin Code      *\n");
                       printf("*************************\n");
         getch();
      }

1.3) Write a program using one print statement to print the pattern of asterisks as shown below:
        *
        *  *
        *  *  *
        *  *  *  *

     #include<stdio.h>
     #include<conio.h>
     void main()
      {
          clrscr();
                       printf("*\n* *\n* * *\n* * * *\n");
          getch();
      }

1.4) Write a program that will print the following figure using suitable characters:

       ******                                         ******
       *        *                                         *        *
       *        *         >>--------------->   *        *
       *        *                                         *        *
       ******                                         ******

    #include<stdio.h>
    #include<conio.h>
    void main()
     {
         clrscr();
                       printf("******                                      ******\n");
                       printf("*        *                                      *        *\n");
                       printf("*        *    >>-------------->      *        *\n");
                       printf("*        *                                      *        *\n");
                       printf("******                                      ******\n");
         getch();
     }

1.5) Given the radius of circle, write a program to compute and display its area. Use a symbolic constant to define the pi value and assume a suitable value for radius.

    #include<stdio.h>
    #include<conio.h>
    #define pi 3.14
    void main()
     {
           clrscr();
                       int r;
                       float area;

                       printf("Enter radius of circle:");
                       scanf("%d",&r);

                       area=(float)pi*r*r;

                      printf("Area=%f\n",area);
           getch();
    }

1.6) Write a program to output the following multiplication table:
 
            5 x 1 = 5
            5 x 2 = 10
            5 x 3 = 15
              .      .
              .      .
            5 x 10 = 50

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

               for(i=1;i<=10;i++)
                {
                         ans=5*i;
             
                         printf("5 x %d = %d\n",i,ans);
                }
         getch();
     }

1.7) Given two integers 20 and 10, write a program that uses a function add() to add these two numbers and sub() to find the difference of these two numbers and then display the sum and difference in the following form:
       20+10=30
       20-10=10

     #include<stdio.h>
     #include<conio.h>
     int no1=20,no2=10;
     void main()
      {
              void add(),sub();
         clrscr();
               add();
               sub();
         getch();
      }

     void add()
      {
             int sum;
             sum=no1+no2;

             printf("%d + %d = %d\n",no1,no2,sum);
      }

     void sub()
       {
             int diff;
             diff=no1-no2;

            printf("%d - %d = %d\n",no1,no2,diff);
       }

1.8) Given the values of three variables a,b, and c write a program to compute and display the value of x,where

                                 x=a/(b-c)
Execute the program for the following values:
(A) a=250,b=85,c=25
(B) a=300,b=70,c=70

      #include<stdio.h>
      #include<conio.h>
      void main()
       {
               int a,b,c;
               float x;

          clrscr();

             printf("Enter value of a,b, and c:");
             scanf("%d%d%d",&a,&b,&c);

            x=(float)a/(b-c);

            printf("X=%f\n",x);

         getch();
       }


1.9) Relationship between Celsius and Fahrenheit is governed by the formula:
                     F=9C/5+32

       Write a program to convert the temperature
      (A) From Celsius to Fahrenheit
      (B) From Fahrenheit to Celsius

(A) #include<stdio.h>
      #include<conio.h>
      void main()
       {
                float cels,fahr,x;

           clrscr();

                  printf("Enter temperature in Ceslius:");
                  scanf("%f",&cels);

                  x=(9*cels)/5;
                  fahr=x+32;

                  printf("Temperature in Fahrenheit=%f\n",fahr);
           getch();
        }

(B) #include<stdio.h>
      #include<conio.h>
      void main()
       {
                  float fahr,cels,x;
           clrscr();

                   printf("Enter temperature in Fahrenheit:");
                   scanf("%f",&fahr);

                   x=fahr-32;
                   cels=(5*x)/9;

                  printf("Temperature in Celsius=%f\n",cels);
          getch();
       }


1.10) Area of a triangle is given by the formula:
                A=sqrt(S*(S-a)*(S-b)*(S-c))
         Where a,b and c are sides of the triangle and 2S=a+b+c. Write a program to compute the area of the triangle and given the values of a,b and c.

          #include<stdio.h>
          #include<conio.h>
          #include<math.h>
          void main()
            {
                    int a,b,c,S;
                    float area;
              clrscr();

                    printf("Enter a,b,c the sides of triangle:");
                    scanf("%d%d%d",&a,&b,&c);

                   S=(a+b+c)/2;

                   area=(float)sqrt(S*(S-a)*(S-b)*(S-c));

                  printf("Area of triangle=%f\n",area);
              getch();
           }


1.11) Distance between two points (x1,y1) and (x2,y2) is governed by the formula:
                  D^2=(x2-x1)^2 + (y2-y1)^2
         Write a program to compute D given the coordinates of the points.

          #include<stdio.h>
          #include<conio.h>
          #include<math.h>
          void main()
           {
                             int x1,x2,y1,y2;
                             float D;
                    clrscr();
                                   
                             printf("Enter value of the coordinates x1,x2,y1,y2");
                             scanf("%d%d%d%d",&x1,&x2,&y1,&y2);

                            D=(float)sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));

                            printf("Distance between points=%f\n",D);
                   getch();
            }

1.12) A point on the circumference of a  circle whose centre is (0,0) is(4,5). Write a program to compute perimeter and area of the circle.

          #include<stdio.h>
         #include<conio.h>
         #include<math.h>
         #define pi 3.14
         void main()
          {
                               int x1,y1,x2,y2;
                               float r,area,per;
                    clrscr();

                               x1=0;
                               x2=4;
                               y1=0;
                               y2=5;
                 
                               r=(float)sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));

                               per=2*pi*r;
                               printf("Perimeter of circle=%f\n",per);

                               area=pi*r*r;
                               printf("Area of cicle=%f\n",area);
                      getch();
              }


1.13) The line joining the points (2,2) and (5,6) which lie on the circumference of a circle is the diameter of the circle. Write a program to compute the area of the circle.

           #include<stdio.h>
           #include<conio.h>
           #include<math.h>
           #define pi 3.14
           void main()
            {
                                int x1,x2,y1,y2;
                                float area,r,D;
                     clrscr();
                                x1=2;
                                y1=2;
                                x2=5;
                                y2=6;

                               D=(float)sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));

                               r=D/2;
                               area=pi*r*;

                              printf("Area of circle=%f\n",area);
                  getch);
            }

1.14) Write a program to display the equation of line in the form:
                     ax+by=c
          for a=5,b=8 and c=18.

           #include<stdio.h>
           #include<conio.h>
           void main()
            {
                                int a,b,c;
                    clrscr();
                               a=5;
                               b=8;
                               c=18;

                               printf("%dx + %dy = %d",a.b,c);
                   getch();
            }


1.15) Write a program to display the following simple arithmetic calculator:
             x=              ___________                y=                   ____________
            sum=           ___________               difference=       ___________
            product=     ___________               division=           ___________
                     
         #include<stdio.h>
         #include<conio.h>
         void main()
          {
                                    float x,y,sum,diff,mul,div;
                      clrscr();

                                   printf("Enter X and Y:");
                                   scanf("%d%d",&x,&y);

                                  printf("X=%f\nY=%f\n",x,y);

                                  sum=x+y;
                                  diff=x-y;
                                  mul=x*y;
                                  div=x/y;

                                 printf("Sum=%f\nDifference=%f\nProduct=%f\nDivision=%f\n",sum,diff,mul,div);
                      getch();
           }


      (:    End of Chapter 1    :)