Saturday 8 December 2012

(a) Z =     (a + b ) 2 / c - 0.5 + 2 a / (q + r)

                           ( a + b) * ( 1 / m)

          Answer

 Z = (( 8.8 * ( a + b ) * 2 / c ) - ( 0.5 + 2 * a / ( q + r ))) / (( a + 5 ) * ( 1 / m))

(b)  X =    - b + ( b * b ) + 2 4ac
                            2a


        Answer

X  = ( - b + ( b * b ) + 2 * 4 * a * c ) / ( 2 * a )

(c)  R =      2v + 6.22 ( c + d )
                          g + v 

      Answer

R = ( 2 * v + 6.22 * ( c + d )) / ( g + v )

(d)  A =    7.7b ( xy + a ) / c - 0.8 + 2b
                         ( x + a ) ( l / y ) 

        Answer

A = (( 7.7 * b ) * ( x * y + a ) / c - 0.8 + 2 * b ) / (( x + a ) * ( 1 / y ))

[E]  What would be the output of the following programs:

(a)     main ( )
            {
                   int i = 2 , j = 3 , k , l ;
                   float  a , b ;
                   k = i / j *  j ;
                   l = j / i * i ;
                  a = i / j * j ;
                  b = j / i * i ;
                  printf ( " %d %d %f %f " , k , l , a , b ) ;
                             }

                        Output

                           0 2 0. 000000   2.000000  


(b)     main ( )
            {
                    int a , b ;
                    a = -3 - -3 ;
                    b = -3 - - ( - 3 ) ;
                    printf ( * a = %d b = %d* , a , b ) ;
             }
                      
                        Output

                         a = 0  b = -6


(c)      main ( )
             {
                     float a = 5, b = 2 ;
                     int c ;
                     c = a % b ;
                     printf ( "%d" , c ) ;
              }

                        Output

                         Error. Mod ( % ) operator cannot be used on floats


(d)     main ( )
            {
                       printf ( "nn \n\n nn\n" ) ;
                       printf ( "nn /n/n nn/n" ) ;
             }

                        Output

                        nn
                        nn /n/n nn/n


(e)      main ( )
             {
                        int a , b ;
                        printf ( "Enter values of a and b" ) ;
                        scanf ( " %d %d " , &a , &b ) ;
                        printf ( " a = %d b = %d" , a , b ) ;
               }

                         Output

     "Since spaces are given after and before double quotes in scanf ( ) it waits even after enterning two values. We must enter one more value to complete scanf ( ) execution."
     


[F]   Write C programs for the following


(a)  Zain's basic salary is input through the keyboard. His dearness allowance is 40% of basic salary , and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.
      
             Program:

/* To calculate gross salary of Zain */

#inculde <stdio.h>
#inculde <conio.h>

       main ( )
        {
                float bp , da , hra , grpay ;

               clrscr ( ) ; /* Clears Screen */
               printf ( *\n Enter the Basic Pay of Zain : * ) ;
               scanf ( * "%f" , &bp ) ;
               da = 0.4 * bp ;
               hra = 0.2 * bp ;
               grapy = bp + da + hra ; /* Gross Pay = sum of basic & all allowances */
                      
               printf ( "\nBasic Pay of Zain = %f" , bp ) ;
               printf ( "\nDearness Allowance = %f" , da ) ;
               printf ( "\nHouse Rent Allowance = %f" , hra ) ;
               printf ( "\nGross Pay of Zain is %f" , grapy ) ;

               printf ( *\n\n\n\n\nPress any key to exit...* ) ;
               getch ( ) ; /* reads a characteer from keyboard */
                 
           }

(b)  The distance between two cities (in km) is input through the keyboard. Write a program to convert and print this distance in meter , feet , inches and centimeters.

                 Program :

/* Conversion  of distance */

#inculde (stdio.h>
#inculde <conio.h>

       main ( )
        {
                  float km , m , cm , ft , inch ;

                  clrscr ( ) ;
                  printf ( "\nEnter the distance in kilometers :" ) ;
                  scanf ( "%f" , &km ) ;
         
                  m = km * 1000 ;
                  cm = m * 100 ;
                  inch = cm / 2.54 ;
                  ft = inch / 12 ;

                  printf ( "\nDistance in meters = %f" , m ) ;
                  printf ( "\nDistance in  centimeters = %f" , cm ) ;
                  printf ( "\nDistance in feet = %f" , ft ) ;
                  printf ( "\nDistance in inches = %f" , inch ) ;

                  printf ( "\n\n\n\n\n Press any key to exit....." ) ;
                  getch ( ) ;
        }


(c)  If the mark obtained by a student in five different subjects are input through the keyboard , find out the aggregate marks and percentage marks obtained by the student. Assume that the maximum marks that can be obtained by a student in each subject is 100.

             Program :

/* Calculation of aggregate & percentage marks */ 

#inculde <stdio.h>
#inculde <conio.h>

   main ( )
      {

               int m1 , m2 , m3 , m4 , m5 , aggr ;
               float per ;

              clrscr ( ) ;
              printf ( "\nEnter marks in 5 subjects :" ) ;
              scanf ( "%d %d %d %d %d" , &m1 , &m2 , &m3 , &m4 , &m5 ) ;

              aggr = m1 + m2 + m3 + m4 + m5 ;
              per = aggr /  5 ;

             printf ("\nAggregate Marks = %d " , aggr ) ;
             printf ( "\nPercentage Marks = %f " , per ) ;


              printf ("\n\n\n\n\n Press any key to exit.....") ;
               getch ( ) ;
     }

(d)  Temperature of a city in Fahrenheit degree is input through the keyboard. Write a program to convert this temperature into centigrade degrees.

                    Program :

/* Conversion of temperature from Fahrenheit to Centigrade */

#inculde <stdio.h>
#inculde <conio.h>

      main  ( )
         {
                 float fr , cent ;

                 clrscr ( ) ;
                 printf ( *\nEnter the temperature (F) : * ) ;
                 scanf ( " %f " , & fr ) ;

                 cent = 5.0 / 9.0 * ( fr - 32 ) ;
                 printf ( " \n Temperature in centigrade = %f " , cent ) ;

                 printf " \n\n\n\n\n Press any key to exit....* ) ;
                 getch ( ) ;
            }

(e)  The length & breadth of rentangle and radius of a circle are input through the keyboard. Write a program to calculate the area & perimeter of the rectangle, and the area &circumference of the circle.

        Program: 

/* Calculation of perimeter & area of rectangle and circle */

#inculde<stdio.h>
#inculde<conio.h>

        main ( )
           {
                      int l , b , r , area1 , perimeter ;
                      float area2 , circum ;
                      
                      clrscr ( ) ;
                      printf  ("\nEnter Length & Breadth of Rectangle ") ;
                      scanf ( "%d %d" , &l , &b ) ;
                      area1 = l * b ; /* Area of a rectangle */
                      perimeter = 2 * 1 + 2 * b ; /* Perimeter of a rectangle */

                      printf ( "\nArea of Rectangle = %d" , area1 ) ;
                      printf ( "\nPerimeter of Rectangle = %d " , perimeter ) ;

                      printf ("\n\nEnter Radius of circle " ) ;
                      scanf (" %d " , &r ) ;

                      area2 = 3.14 * r *r ; /* Area of Circle */
                      circum = 2 * 3.14 * r ; /* Circumference of circle */

                      printf ( "Area of Circle = %f " , area2 ) ;
                      printf ( "\nCircumference of Circle = f% , circum ) ;

                      printf ( "\n\n\n\n\nPress any key to exit...") ;
                      getch ( ) ;
           
         }

(f) Two number are input through the keyboard into two locations C and D. Write a program to interchange the contents of C and D.

               Program :

/* Interchanging of contents of two variables c & d */

#include <stdio.h>
#inculde (conio.h>

        main ( )
            {
                       int c , d , e ;

                       clrscr ( ) ;
                       printf ( "\nEnter the number at location C: " ) ;
                       scanf ( "%d" , &c ) ;
                       printf ( "\nEnter the number at location D: " ) ;
                       scanf ( "%d" , &d ) ;

                      /* Interchange the contents of two variables using a third as temporary store */
            
                      e = c ;
                      c = d ;
                      d = e ;

                      printf ( "\nNew Number at location C = %d" , c ) ;
                      printf ( "\nNew Number at location D = %d , d ) ;

                      printf ( "\n\n\n\n\nPress any key to exit....") ;
                      getch ( ) ;
              }

(g) If a five-digit number is input through the keyboard , write a program to calculate the sum of its digits.
(Hint: Use the modulus operator '%')

            Program :

/* sum of digits of a 5 digit number */

#inculde <stdio.h>
#inculde <conio.h>

           main ( )
              {
                      int num , a , n ;
                      int sum = 0 ; /* sum initialised to zero as otherwise it will contain a garbage value */

                      clrscr ( ) ;
                      printf ( "\nEnter a 5 digit number(less than 32767)" ) ;
                      scanf ( "%d" , &num ) ;

                      a = n % 10 ; /* last digit extracted as remainder */
                      n = num/10 ; /* remaining digits */
                      sum = sum + a ; /* sum updated with addition of extracted digit "/

                      a = n % 10 ; /* 4 th digit */
                      n = n /10 ;
                      sum = sum + a ;

                      a = n % 10 ; /*3 rd digit */
                      n = n/10 ;
                      sum = sum + a ;

                      a = n % 10 ; /* 2 nd digit */
                      n = n/10 ;
                      sum = sum + a ;

                      a = n % 10 ; /* 1st digit */
                      sum = sum + a ;

                      printf ( "\n The sum of the 5 digits of %d is %d" , num , sum ) ;

                      printf ( "\n\n\n\n\nPress any key to exit...") ;
                      getch ( ) ;
            }

(h) If a five-digit number is input through the keyboard , write a program to reverse the number.

                Program:

/* To reverse the digits of a 5-digit number */

#inculde <stdio.h>
#inculde <conio.h>

          main ( )
              {
                      int n , a , b ;
                      long int revnum = 0 ;
                      / * Initialised otherwise it will contain garbage value. Declared as long as after reversing it
                      may not be in the int range */

                     clrscr ( ) ;
                     printf ( "\nEnter a five digit number (less than 32767) " ) ;
                     scanf ( "%d" , &n ) ;

                     a = n % 10 ; /* last digit */
                     a = n / 10 ; /* remaining digits */
                     revnum = revnum + a * 10000L ; /* revnum updated with value of extracted digit */

                     a = n % 10 ; /* 4 th digit */
                     a = n / 10 ; /* remaining digits */
                     revnum = revnum + a * 1000 ;

                     a = n % ;  /*3 rd digit */
                     n = n / 10 ; /* remaining digits */
                     revnum = revnum + a * 100 ;
                     a = n % 10 ; /* 2 nd digit /*
                     n = n 10 ; /* remaining digits */
                     revnum = revnum + a * 10 ;
                     a = n % 10 ; /* 1 st digit */
                     revnum revnum + a ;

                     printf ( "\nThe reversed number is %d" , revnum ) ;
                     printf ( "\n\n\n\n\nPress any key to exit...") ;
                     getch ( ) ;
           }



                    

                    

No comments:

Post a Comment