C# Programming - Functions and Subroutines - Discussion
Discussion Forum : Functions and Subroutines - General Questions (Q.No. 7)
7.
What will be the output of the C#.NET code snippet given below?
namespace IndiabixConsoleApplication
{
class SampleProgram
{
static void Main(string[ ] args)
{
int i = 10;
double d = 34.340;
fun(i);
fun(d);
}
static void fun(double d)
{
Console.WriteLine(d + " ");
}
}
}
Discussion:
9 comments Page 1 of 1.
Omik said:
8 years ago
Let's Make it simple.
Actual Output : 10 34.340
If in case,
You have initialized value of i and d as follows;
int i = 10;
double d = 34.00;
fun(i);
fun(d);
The Output will be:
10
34
Even though 34 is double because if numbers are zero's after decimal then
It will consider the only number a before decimal place.
Here, 34.
Actual Output : 10 34.340
If in case,
You have initialized value of i and d as follows;
int i = 10;
double d = 34.00;
fun(i);
fun(d);
The Output will be:
10
34
Even though 34 is double because if numbers are zero's after decimal then
It will consider the only number a before decimal place.
Here, 34.
Anindita said:
8 years ago
The first thing is, fun(i) is getting called, though the parameter defined here as double as in fun(double d).
Because,fun(i) is implicitly converting int i from int to double.
Secondly,double does not understand the difference between 34.340 and 34.34 so it removes trailing zeros.
But if you want to still display the trailing zeros you have to specify the format inside Console.WriteLine.
You can refer the following example:
namespace IndiabixConsoleApplication
{
class SampleProgram
{
static void Main(string[] args)
{
int i = 10;
double d = 34.340;
fun(i);
fun(d);
Console.ReadKey();
}
static void fun(double d)
{
Console.WriteLine(d.ToString("##.000"));
Console.WriteLine(d + " ");
}
}
}
Because,fun(i) is implicitly converting int i from int to double.
Secondly,double does not understand the difference between 34.340 and 34.34 so it removes trailing zeros.
But if you want to still display the trailing zeros you have to specify the format inside Console.WriteLine.
You can refer the following example:
namespace IndiabixConsoleApplication
{
class SampleProgram
{
static void Main(string[] args)
{
int i = 10;
double d = 34.340;
fun(i);
fun(d);
Console.ReadKey();
}
static void fun(double d)
{
Console.WriteLine(d.ToString("##.000"));
Console.WriteLine(d + " ");
}
}
}
Prasanth S said:
8 years ago
When fun(i) is called then print the value of 'i' like 10 first and then the second function is executed to print 34.34.
Sachin shrivastava said:
9 years ago
Didn't get it. Please elaborate.
Shaikh salman said:
9 years ago
I did not know the answer, how it gives the output 10 before 34.34 when int i not call by function?
Please help me.
Please help me.
Kartik said:
1 decade ago
I too want know how int type was called by fun().
Lavanya said:
1 decade ago
I did not get this answer. Please can any one give how print 10 before 34.34.
Praneet said:
1 decade ago
Answer Why its not C : The floating-point data types do not have any internal representation of trailing zero characters. For example, they do not distinguish between 4.2000 and 4.2. Consequently, trailing zero characters do not appear when you display or print floating-point values.
Akki said:
1 decade ago
10 will be called automatically and then while calling d it eliminates 0 because it contains double quotes at the consolewriteln.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers