Friday, September 02, 2016

Solution of ANSIC Excercies Chapter 01



                                                              CHAPTER    01   
                                        (Mahmud_Jamil)..BRUR // 02-09-2016


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

 Solution:
               #include<stdio.h>
               int main()
               {
                printf("\n\tName    :   Md. Jamil Mahmud\n");
                printf("\tDoor No :   02 , Street: Park Road\n");
                printf("\tCity    :   Rangpur , Pin Code: 7864\n\n");
                return 0;
                }



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


Solution:
              #include<stdio.h>
              int main()
             {
              printf("\n\t  _________________________________________\n");
              printf("\n\t |  Name : Md.Mahmud Jamil                 |\n");
              printf("\n\t |   Door No : 02 , Street : Park Road     |\n");
              printf("\n\t |   City : Rangpur , Pin Code : 7864      |\n");
              printf("\n\t |_________________________________________|\n\n\n");
              return 0;
              }



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

Solution:
               #include<stdio.h>
               int main()
              {
                  int i,j,k;
                  for(i=1;i<=4;i++){
                 for(j=1;j<=i-4;j++)
                  printf("  ");
                  for(k=1;k<=i;k++)
                  printf("   *  ");
                  printf("\n");
                }
               return 0;
                }



1.4    Write a program that will print the following figure using suitable charactes.

 ____________                                      ___________

|___________| >>------------------>   |___________|

     

 Solution:
                #include<stdio.h>
               int main()
               {
               printf("\n\t ___________              ___________\n");
               printf("\t|\t    |            |\t     |\n");
                printf("\t|\t    |>>--------->|\t     |\n");
               printf("\t|\t    |            |\t     |\n");
                printf("\t|___________|            |___________|\n\n");
               return 0;
              }



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

     

Solution:
              #include <stdio.h>
              int main ()
            {
            int r;
            float area,pi;
             printf("enter radius   ");
            scanf("%d",&r);
             pi=3.1416;
             area=pi*r*r;
             printf("\n\tArea=%.2f\n",area);
            return 0;
             }




1.6    Write a program to output the following multiplication table.
5 * 1 =5
5 * 2 =10
5 * 3 =15
  ……….

  ……….

5 * 10 =50


Solution:
               #include <stdio.h>
              int main ()
              {
              int i=1,x=5;
             while(i<=10) {
             printf("\t%d X %d= %d\n",x,i,x*i);
             i=i++;
             }
             return 0;
            }




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

Solution:
               #include<stdio.h>
               int main()
               {
              int Sum,Diff;
              Sum=20+10;
              Diff=20-10;
              printf("\t20 + 10 = %d\n", Sum);
              printf("\t20 - 10 = %d\n", Diff);
              return 0;
              }





1.8   Given the values of three variables a, b and c, write a program to compute and display
        the values of x, where
                                                 X= a / (b - c)
      Execute your program for the following values:
      (a) a=250, b==85,c=25
      (b) a=300, b=70, c=70
     Comment on the output in each case.
     


Solution:
                 #include <stdio.h>
                  int main ()
                 {
                 int a,b,c;
                  float x;
                 printf("Enter a= ");
                 scanf("%d",&a);
                 printf("Enter b= ");
                scanf("%d",&b);
                printf("Enter c= ");
                scanf("%d",&c);
                x=a/(b-c);
                printf("\nresult=%.2lf\n",x);
                return 0;
                }
                     /* comment:
                             Any number can not be divided by zero
                                  so is not possible to determine the correct answer is B
                       */





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 and
                                            (b) from Fahrenheit to Celsius.


Solution: (a)
                           #include <stdio.h>
                            int main ()
                              {
                              float celsius,farenheit;
                               printf("enter the temperature in celsius:");
                               scanf("%f",&celsius);
                               farenheit=(1.8*celsius)+32;
                               printf("temperature in farenheit is:%.2f\n",farenheit);
                                return 0;
                                }




Solution: (b)
                    #include <stdio.h>
                    int main ()
                    {
                   double C,F;
                   printf("enter the temparature in F:");
                   scanf("%lf",&F);
                    C=5*(F-32)/9;
                   printf("temparature in C is:%lf\n",C);
                    return 0;
                    }




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 given the values of a, b and c.
      



Solution:
               #include<stdio.h>
               #include<math.h>
                int main( )
              {
              double  S,a,b,c,Area;
               printf("a=");
               scanf("%lf",&a);
               printf("b=");
               scanf("%lf",&b);
               printf("c=");
               scanf("%lf",&c);
               S=(a+b+c)/2;
              Area=sqrt(S*(S-a)*(S-b)*(S-c));
              printf("Area=%lf\n",Area);
              return 0;
               }






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.
     


 Solution:
                #include<stdio.h>
                #include<math.h>
                 int main()
                 {
                  float x1,x2,y1,y2;
                  float D;
                 printf("Enter the value of x1 y1 x2 y2\n");
                 scanf("%f %f %f %f",          &x1,&y1,&x2,&y2);
                 D=sqrt((x2-x1)*(x2-x1)+ (y2-y1)* (y2-y1));
                  printf("\nD= %.2f\n",D);
                   return 0;
                }






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

    



 Solution:
                #include<stdio.h>
                #include<math.h>
                int main()
                 {
                  double r, c1=0, c2=0, x1=4, y1=5, area, PI, perimeter;
                    r = sqrt((c1 - x1)*(c1 - x1) + (c2 - y1)*(c2 - y1));
                 PI = acos(-1);
                  perimeter = 2 * PI * r;
                area = PI * r * r;
                 printf("Perimeter = %.2lf\n", perimeter);
                 printf("Area = %.2lf\n", area);
                 return 0;
                }






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.

      


Solution:
                #include<stdio.h>
                #include<math.h>
                int main()
                 {
                 int x1=2,y1=2,x2=5,y2=6;
                 float Diameter,Radius,Area,Pi=3.1416;
                 Diameter=sqrt(((x2-x1)*(x2-x1))+ ((y2-y1)* (y2-y1)));
                  Radius= Diameter/2;
                 Area=Pi*Radius*Radius;
                 printf("\nArea= %.2f\n",Area);
                return 0;
                 }






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

         

Solution:
              #include<stdio.h>
              int main()
                {
                int a,b,c;
                 printf("Enter a= ");
                 scanf("%d",&a);
                 printf("Enter b= ");
                 scanf("%d",&b);
                 printf("Enter c= ");
                 scanf("%d",&c);
                 printf("\n %dx+%dy =%d\n",a,b,c);
                 return 0;
                 }




1.15   Write a program to display the following simple arithmetic calculator
                  x=                                  y=
                 Sum=                            Difference=
                Product=                       Division=

          


Solution:
               #include<stdio.h>
                int main( )
                 {
                     float x,y,sum,Difference, product,division;
                      scanf("%f",&x);
                      scanf("%f",&y);
                      printf("\nx=%.2f\n",x);
                     printf("Y=%.2f\n",y);
                    printf("\tsum=%.2f\n",x+y);
                    printf("\tDifference=%.2f\n",x-y);
                    printf("\tproduct=%.2f\n",x*y);
                    printf("\tdivision=%.2f\n",x/y);
                     return 0;
                     }



  
>>>>>END<<<<<