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    :)                     




No comments:

Post a Comment