C# Programming - Operators - Discussion

Discussion Forum : Operators - General Questions (Q.No. 7)
7.
Which of the following is the correct output for the C#.NET code snippet given below?
Console.WriteLine(13 / 2 + " " + 13 % 2); 
6.5 1
6.5 0
6 0
6 1
6.5 6.5
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
20 comments Page 2 of 2.

Abel said:   1 decade ago
If the data type is double then the correct answer is 6.5 and 1.

If it is int data type then it is 6 and 1.
(1)

Kazeem mohammad said:   1 decade ago
13/2 = 6 as the operator " / " behavior depends on the operand type only(both the operands are of integer type so the return value of 13/2 is 6).

Same for 13%2 = 1. (both the operands are of integer type so the return value of 13%2 is 1).

Yashi said:   1 decade ago
6 and 1 is OK.

But it is written as:

Console.WriteLine(13 / 2 + " " + 13 % 2);

So, what is (+ " "+) for?

I mean is there no need to add 6 and 1.

K.gowda said:   1 decade ago
By default write line method accepts only integer value not any float or double value, and here 13/2 is float that's why its giving the int value 6 only instead of 6.5.

Gaurav Patil said:   1 decade ago
Hey @Shivakant tell me properly.

(13 / 2 + " " + 13 % 2)

So 13/2 = 6.5 but decimal values are not allows only = 6.

I'm confuse here not declare any data type how you take here a int datatype and here we take float then how it possible?

I agree with 13%2 = 1 this is returns remainder value answer.

Andrew Indayang said:   1 decade ago
I know now, if anyone that get confused lets see my solution.

In this case 13/2 is 6. Why? because you divide in an integer value 13 and 2 is an integer value.

Try this 13.0 / 2.0 the result will be 6.5 because you divide double and double.

And how about 13.0/2? it will gives you an double value as well because you have 1 value which is a double.

Hopefully this will help.

Somu Minajagi said:   1 decade ago
13/2 = 6.5 Decimal value = 6.
13%2 remainder value = 1.

Answer is : 6 1.

Jaffa said:   1 decade ago
13/2 = returns 6 as 13, 2 are integers.

13%2 remainder value = 1.

as + indicates concatenation of strings.

Thus gives (13 / 2 + " " + 13 % 2) treated as (13/2)as string concatenated to space(" ")concatenated to (13%2) which gives 6 1.

Sulabh Jain said:   1 decade ago
int/int = int.
float /int = float.

So 13 / 2 = 6.

13%2 remainder value = 1.

Falgun_P said:   1 decade ago
Whenever you use some number to perform arithmetic operation in C# the number use System.Int32 namespace. And this namespace represents a 32-bit signed int.

So when compile compute the value of 13/2 the actual answer is 6.5 but int value can't handle with floating point number. So it removes the part after point so answer is 6.

And % gives us a Remainder so answer of 13%2 is 1. So answer of question is 6 1.


Post your comments here:

Your comments will be displayed after verification.