41 41 votes Consider the following C program: #include <stdio.h> int main() { float sum = 0.0, j=1.0, i=2.0; while (i/j > 0.0625) { j=j+j; sum=sum+i/j; printf("%f\n", sum); } return 0; } The number of times the variable sum will be printed, when the above program is executed, is _________ Programming in C gatecse-2019 numerical-answers programming-in-c programming two-marks + – Arjun 25.6k views answer comment Share Follow Print See all 7 Comments 7 7 Comments reply akash.dinkar12 commented Feb 7, 2019 reply Follow flag 5 will be answer 10 10 replyShare great_gater commented Feb 7, 2019 i edited by Hira Thakur Dec 9, 2023 reply Follow flag Initially i = 2.0, j = 1.0 J = 1 => i/j = 2/1 > 0.0625 // First print J = 1+1 => i/j = 2/2 > 0.0625 //second print J = 2+2 => i/j = 2/4 > 0.0625// Third print J = 4+4 => i/j = 2/8 > 0.0625 // fourth print J = 8+8 => i/j = 2/16 > 0.0625 // Fifth print J = 16+16 => i/j = 2/32 > 0.0625 (False) 10 10 replyShare `JEET commented Dec 9, 2019 reply Follow flag $\mathbf{5}$ times. 2 2 replyShare shashankrustagi commented Dec 23, 2020 i edited by shashankrustagi Jan 31, 2021 reply Follow flag Use normal calculator and look 2/1 -------------------1 2/(1+1)---------------2 2/(2+2)-------------3 2/(4+4)--------------4 2/(8+8)--------------5 2/(16+16) while condition fails as 0.0625 is not greater than 0.0625 0 0 replyShare Satish_kr_Verma-CSE- commented Oct 14, 2024 reply Follow flag i want to know how float divide by divde is store as integer by compiler ; give me proper explanation 0 0 replyShare merohan17 commented Nov 8, 2024 reply Follow flag @Satish_kr_Verma-CSE- there is none integer data type variable used in above code :) even it is explicitly mentioned all variables are float data type,float sum = 0.0, j=1.0, i=2.0; 0 0 replyShare tajammulbasheer commented Nov 8, 2024 reply Follow flag @Satish_kr_Verma-CSE- When dividing two floating-point numbers (such as float or double) in C, the result of the division is also a floating-point number. If the operation involves one operand as a float (or double), the other operand is automatically promoted to float (or double), ensuring that the result remains a floating-point type. This automatic promotion preserves the decimal part of the result, maintaining precision.However, if this floating-point result is stored in an integer variable, the fractional portion is discarded, and only the integer part is retained. This process is known as type casting or type conversion, and in C, it occurs automatically when a float or double result is assigned to an int variable. Additionally, explicit type casting can be used to manually convert a floating-point result to an integer if needed. For instance, (int)(a / b) would store only the integer part of the division result by truncating the decimal portion.In the given code, only float data types are used, meaning that both the operands and the result of the division remain in float form, preserving the decimal part throughout the calculation. No integer data types are involved, so the result stays a floating-point value. However, if desired, this result could be stored in an integer by applying implicit or explicit type conversion, which would truncate the decimal and keep only the integer portion. 2 2 replyShare Please log in or register to add a comment.
Best answer 64 64 votes $i = 2.0, j=1.0$ while $\left( \frac{i}{j} > 0.0625\right)$ $j = 1$ $ \frac{i}{j} = \frac{2}{1} > 0.0625$ $j=j+j, 1^{\text{st}}$ PRINT $j=2$ $ \frac{i}{j} = \frac{2}{2} > 0.0625$ $j=j+j, 2^{\text{nd}}$ PRINT $j=4$ $ \frac{i}{j} = \frac{2}{4} > 0.0625$ $j=j+j, 3^{\text{rd}}$ PRINT $j=8$ $ \frac{i}{j} = \frac{2}{8} > 0.0625$ $j=j+j, 4^{\text{th}}$ PRINT $j=16$ $ \frac{i}{j} = \frac{2}{16} > 0.0625$ $j=j+j, 5^{\text{th}}$ PRINT $j=32$ $ \frac{i}{j} = \frac{2}{32} = 0.0625$ $\text{Break}$ Total $5$ times sum will be printed. Digvijay Pandey answered Feb 7, 2019 • edited Jun 20, 2021 by Lakshman Bhaiya Digvijay Pandey comment Share Follow See 1 comment 1 1 comment reply Nitesh Singh 2 commented Dec 15, 2019 reply Follow flag Typo $2/32$ 1 1 replyShare Please log in or register to add a comment.
30 30 votes 5 times while loop will iterate.. Sourav Basu answered Apr 21, 2019 Sourav Basu comment Share Follow 0 reply Please log in or register to add a comment.
15 15 votes while loop condition is :- $\frac{2}{2^{k}}> 0.0625$ this condition will be true for k=0,1,2,3,4 but fails for k=5 so it'll print sum 5 times. Asim Siddiqui 4 answered Mar 23, 2019 Asim Siddiqui 4 comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes sum = 0.0 i = 2.0 j = 1.0 IterationCurrent j ValueCondition Check: 2/j > 1/16Is it True?Action Inside Loop1st1.02/1 = 2 > 0.0625Yesj becomes 2.0, prints sum (1st time)2nd2.02/2 = 1 > 0.0625Yesj becomes 4.0, prints sum (2nd time)3rd4.02/4 = 0.5 > 0.0625Yesj becomes 8.0, prints sum (3rd time)4th8.02/8 = 0.25 > 0.0625Yesj becomes 16.0, prints sum (4th time)5th16.02/16 = 0.125 > 0.0625Yesj becomes 32.0, prints sum (5th time)6th32.02/32 = 0.0625 > 0.0625NoLoop terminates immediatelyFinal Answer: 5 VIPIN_CHANDRA answered May 29 VIPIN_CHANDRA comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes So answer is 5. ansurajjaishankar answered Jul 10 ansurajjaishankar comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes Firstly, for simplicity convert 0.0625 into 1/16.0.0625 = 625/10000 = 1/16The loop executes while i/j > 1/16. Here, i always remains 2 and j is doubled (j = j + j) after every iteration. Hence, the value of i/j is halved in each iteration. Since the question only asks how many times sum is printed, we only need to check the loop condition.Initially: i = 2, j = 1(1) 2/1 > 1/16 → True → Print↓(2) 2/2 > 1/16 → True → Print↓(3) 2/4 > 1/16 → True → Print↓(4) 2/8 > 1/16 → True → Print↓(5) 2/16 > 1/16 → True → Print↓2/32 > 1/16 → FalseLoop terminates.Hence, sum is printed 5 times. ceo answered Jul 25 ceo comment Share Follow 0 reply Please log in or register to add a comment.