how is the answer of this code 1.000. Can anyone explain? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

how is the answer of this code 1.000. Can anyone explain?

#include <stdio.h> int main() { float a = 5%2*3/2; printf("%f", a); return 0; }

10th Jan 2024, 10:22 AM
Shubham Rampurkar
Shubham Rampurkar - avatar
16 Answers
+ 7
Shubham Rampurkar all three operators in the calculation for y have the same mathematical precedence, so the order of operations is left-to-right. Adding parentheses to clarify, it is equivalent to this: int y = ((5%2)*3)/2; 5%2 = 1 [5/2 has remainder 1] (1)*3 = 3 (3)/2 = (int) 1 [integer division] Integer division occurs when the divisor and dividend are both integer. So, y = 1, and the printf shows it. Note: In this case, even if you were to divide by 2.0 in order to force floating point division, the result would be converted to 1 because y is int.
10th Jan 2024, 10:59 AM
Brian
Brian - avatar
+ 3
5%2 = 1 3/2 = 1
10th Jan 2024, 10:41 AM
A͢J
A͢J - avatar
+ 3
Shubham Rampurkar yes, the integer division rule still applies. Both sides of the division are int (3 and 2). Try replacing dividend 2 with 2.0.
10th Jan 2024, 11:08 AM
Brian
Brian - avatar
+ 3
Shubham Rampurkar you're welcome. In C you do have to be precise and intentional about mixing types. By design the compiler makes few assumptions about what you are trying to accomplish. It just does what the code says. That makes it flexible, but less user-friendly. You must know all the rules well so you can carefully craft your intended results.
10th Jan 2024, 11:28 AM
Brian
Brian - avatar
+ 1
ok. Thanks Brian for your help. That's a bit confusing for me. but now I understand.
10th Jan 2024, 11:09 AM
Shubham Rampurkar
Shubham Rampurkar - avatar
+ 1
for sure.
10th Jan 2024, 11:29 AM
Shubham Rampurkar
Shubham Rampurkar - avatar
+ 1
Ankit Kumar In printf, the syntax is like this: printf("%[format specifier of datatype]",[variable]); so the actual code is like Rain's post above.
12th Jan 2024, 4:51 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
0
AJ 3/2 = 1.5
10th Jan 2024, 10:43 AM
Shubham Rampurkar
Shubham Rampurkar - avatar
0
Thanks Brian. but I've done this problem like below and it still gives answer 1.000 #include <stdio.h> int main() { float a = 5%2*3/2; printf("%f", a); return 0; }
10th Jan 2024, 11:06 AM
Shubham Rampurkar
Shubham Rampurkar - avatar
0
I haven't used C for a while, but doesn't printf("%f", a) mean to print a in float format?
10th Jan 2024, 7:35 PM
Rain
Rain - avatar
0
Rain yeah. That's true. But i think the problem lies in the code operation processing (a=5%2*3/2). As far as i know float data type rarely use modulo operation. It is more common in int data type
12th Jan 2024, 3:15 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
0
Bro to na variable kaha let kiya ?????
12th Jan 2024, 3:37 AM
Ankit Kumar
Ankit Kumar - avatar
0
!-- created by : SwapniL --> <!DOCTYPE html> <html> <head> <title>Loading Animation</title> <style> *, *::after, *::before{ padding: 0; margin: 0; box-sizing: border-box; } body{ height: 100vh; display: flex; align-items: center; justify-content: center; } .base{ height: 200px; width: 200px; display: flex; align-items: center; justify-content: center; position: relative; } .dot{ height: 20px; width: 20px; background-color: black; border-radius: 50%; opacity : 0; position: absolute; left: calc(50% - var(--x)); animation: fade 1s var(--delay) linear alternate-reverse infinite; } @keyframes fade { from{ opacity : 1; } to{ opacity: 0; transform: translateY(20px); } } .one{ --x : -50px; --delay :
12th Jan 2024, 4:56 AM
Ayan Hayat
Ayan Hayat - avatar
0
Ayan Hayat - hello, and welcome to Sololearn! It seems you pasted in the wrong place. We're happy to have you join us, though the irrelevant html code clutters the conversation. Kindly delete your reply.
12th Jan 2024, 10:55 AM
Brian
Brian - avatar
0
In this expression `5%2*3/2` is evaluated. Here's the step-by-step explanation: 1. `5 % 2` calculates the remainder when 5 is divided by 2, which is 1. 2. `1 * 3` is then multiplied by 3, resulting in 3. 3. Finally, `3 / 2` is divided by 2 using integer division, resulting in 1 (since the fractional part is discarded in integer division). So, the value assigned to variable `a` is 1.0 and when you print it using `printf("%f", a); it will display as "1.000000".
12th Jan 2024, 10:58 AM
Ankit Kumar
Ankit Kumar - avatar
0
Ok 👌
12th Jan 2024, 10:59 AM
Ankit Kumar
Ankit Kumar - avatar