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


No comments:

Post a Comment