Inputting Real Numbers

Unlike integer numbers, the field width of real numbers is not to be specified and therefore scanf reads real numbers using the simple specification %f for both the notations, namely, decimal point notation and exponential notation. For example:

scanf (“%f%f%f”, &p, &q, &r);

with the input data

123.45 23.12E-1   457

will assign the value 123.45 to q and 457 to r. The input field specification may be separated by any arbitrary blank spaces. If the numbers to be read is double type, than the specification should be %If instead of simple %f. A number may be skipped using %*f specification.


Program – Illustration for reading real numbers
#include <stdio.h>
void main ()
{
    float a, b;
    double p, q;
    printf (“Enter two real numbers\n”);
    scanf (“%f %e”, &a, &b);
    printf (“\na = %f\b = %f”, a, b);
    printf (“\nEnter values of p and q\n”);
    scanf (“% lf %If”, &p, &q);
    printf (“\np = %lf\tq = %lf”, p, q);
    printf (“\np = %.12lf\tq = %.12e”, p, q);
}

Output

Enter two real numbers
11.1234    15.4e – 2
a = 11. 123400    b = 0.154000
Enter values of p and q
2.123456789012    25.0987654321123456
P = 2.123457    q = 25. 098765
P = 2.123456789012    q = 2. 509876543211e+01



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext