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