GATE CSE
First time here? Checkout the FAQ!
x
+3 votes
39 views

Cosider the code snippet

CODE 1

float a= 10.25;

if(a==10.25)
       ptint " A "
else 
   print "B"
print "C"


CODE 2

float a =10.25;
n=15.25;
while(n!=10.25)
{
    print "*";
    n--;
}
Number of times * printed ?

 

Please explain the output in both cases

asked in Programming by Loyal (2.6k points)  
edited by | 39 views

I have read somewhere floating point numbers should not be compared, because they are not exactly what they are looking, they give the result after rounding.

1 Answer

0 votes

Yes, your argument is quite ostensible.Though the output may vary with compilers.

For CODE1, OUTPUT = > AC

For CODE2, OUTPUT = > ***** (5 times)   (You havn't specified the data type of n , so I am assuming it to                                                                                be FLOAT) 

If you don't specify "10.25"  as "10.25f"  ,then the program will store it in "Double Precision Floating Point format " albeit the right hand side of the expression i.e. variable "a"  is declared as FLOAT (Single Precision Format )  due to which the program may give "an unexpected output"

answered by (325 points)  


Top Users Jun 2017
  1. Bikram

    3704 Points

  2. Arnab Bhadra

    1502 Points

  3. Hemant Parihar

    1502 Points

  4. Niraj Singh 2

    1481 Points

  5. junaid ahmad

    1432 Points

  6. Debashish Deka

    1384 Points

  7. Rupendra Choudhary

    1220 Points

  8. rahul sharma 5

    1220 Points

  9. Arjun

    1158 Points

  10. srestha

    1006 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 Jun 26 - Jul 02
  1. Arjun

    198 Points

  2. akankshadewangan24

    152 Points

  3. Debashish Deka

    138 Points

  4. Hira Thakur

    130 Points

  5. Soumya29

    104 Points


23,399 questions
30,111 answers
67,489 comments
28,424 users