Saturday, 20 January 2018

Output Of Real Numbers | tutorial for beginners


Output Of Real Numbers

Output Of Real Numbers
The output of a real number may be displayed in decimal notation the following  format specification: %w.pf The integer w indicates the minimum number of positions
that are to be used for the display of the value and integer ‘p’ indicates the number of digits to be displayed after the decimal point. The value when displayed is rounded to ‘p’ decimal places and printed right justified in the field o f w columns. The default precision is 6 decimal places. we can
also display a real number in exponential notation by using %w.pe   
 
   The display takes the form [-]m.nnnne[+ or -]xx  where the length of the string of n’s is specified by the precision p. The default precision is 6.The field width w should satisfy the condition     
              w>=p+7.
   The value will be of the rounded off and printed right justified in the field of w columns.Padding
the leading blanks with zeros and printing with left justification is also possible by introducing 0 or – before the field width specifier w.
  The following example illustrate the output of the number
y=98.7654 under different format specifications:

1.   FORMAT
     printf("%7.4f",y);
     OUTPUT:
   
9 8 . 7 6 5 4


                           2.   FORMAT
                               printf("%7.2f",y);
OUTPUT:
9 9 8 . 7 7



                           3. FORMAT
                               printf("%-7.2f",y)
OUTPUT:
9 9 8 . 7 7




                            4.  FORMAT
                               printf("%f",y)
OUTPUT:
9 9 8 . 7 6 5 4



                              5.   FORMAT
                               printf("%10.2e",y)
OUTPUT:
9 9 . 8 8 e + 0 1



                               6.  FORMAT
                               printf("%11.4e",-y)
OUTPUT:
9 - 9 . 8 7 6 5 e + 0 1



                               7. FORMAT
                               printf("%-10.e",y)
OUTPUT:
9 9 8 8 e + 0 1




                            8. FORMAT
                               printf("%e",y)
OUTPUT:
9 9 8 7 6 5 4 0 e + 0 1




      Some systems also supports a special field at run-time.This takes the following form :
         printf("%*.*f",width,precision,number);
    In this case both the field width and the precision are given as arguments which will supply the values for w and p.
for example
        printf("%7.2f",number);
   The advantage of this format is that the values for width and precision may be supplied at runtime ,thus making the format a dynamic one.
For example the above statement can be used as follows:

     int width=7;
     int precision=2;         
    ----------------
    ---------------
    printf("%*.*f",width,precision,number);

Program: All the options of printing a real number
           /* printing of real numbers*/
           main()
           {
            float y=98.7654;
            printf("% 7.4f\n",y);
            printf("%f\n",y);
            printf("%7.2f\n",y);
            printf("%-7.2f\n",y);
            printf("%0. 72Ff\n",y);
            printf("%*.*f",7,2,y);
            printf("\n");
            printf("%10.2e\n",y);

            printf("%12.4e\n",-y);
            printf("%-10.2e\n",y);
            printf("%e\n",y);
           }

Output:   98.7654
                98.765404
                     98.77
               98.77
               0098.77
               98.77

          9.88e+001
         -9.8765e+001
           9.876540e+001

Printing of single  characters: 
          A single character can be displayed in a desired position using the format
      %wc   
       The character will be displayed right-justified in the field of w columns. We can make the display left- justified by placing a minus sign before the integer w.The

Related Posts:

  • Declaration of Arrays Declaration of Arrays  Like any other variables, arrays must be declared before they are used.The general form of array declaration is type variable-name [size]; The type specifies the type of elements that will be… Read More
  • Initialization of Arrays Initialization of Arrays We can initialize the elements of the array in the same way as the ordinary variables when they are declared.The general form of initialization of array is:  static  type  array-… Read More
  • Two-Dimentional Arrays Two-Dimentional Arrays So far we have discussed the array variable that can store a list of values .There will be situation where a table of values will have to be stored.Consider the following data table, which shows t… Read More
  • Pointers And Structures Pointers And Structures  We know that the name of an array stands for the address of its zeroth element.The same thing is true of the names of  arrays of structure variables.Suppose product is an array variabl… Read More
  • Introduction To ARRAYS Introduction To ARRAYS  An array is a group of related data items that share a common name.For instance,  we can define an array name Salary to represent a set of salaries of a group of employees. A particular … Read More

0 comments:

Post a Comment