Predict the output of following program:
Can % be used with floating point numbers in C++?
The above program fails in compilation and compiler report the following error in line 5:
invalid operands of types 'float' and 'float' to binary 'operator%'
% operator cannot be used with floating point numbers in C & C++.
What about Java and C#?
This behavior is different in Java & C#. % operator can be used on floating point numbers in these languages.
Consider following example of Java program:
Same way try this C# program. It works fine:
This article is contributed by Meet Pravasi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())
- Problem in comparing Floating point numbers and how to compare them correctly?
- How to count set bits in a floating point number in C?
- Convert a floating point number to string in C
- Floating Point Operations & Associativity in C, C++ and Java
- C++ Floating Point Manipulation (fmod(), remainder(), remquo() ... in cmath)
- Rounding Floating Point Number To two Decimal Places in C and C++
- How can we use Comma operator in place of curly braces?
- vector::operator= and vector::operator[ ] in C++ STL
- deque::operator= and deque::operator in C++ STL
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Operator Overloading '<<' and '>>' operator in a linked list class
- 3-way comparison operator (Space Ship Operator) in C++ 20
- Rotation of a point about another point in C++
- Can we use function on left side of an expression in C and C++?
- Do not use sizeof for array parameters
- When do we use Initializer List in C++?
- Use of explicit keyword in C++
- How to use getline() in C++ when there are blank lines in input?
- Why strcpy and strncpy are not safe to use?