Program p2: /*A simple program showing use of casting operation*/
using system; //namespace
class Casting
{
public static void Main()
{
float sum;
int i;
sum=0.0F;
/*value initialized to a real variable must followed by F,otherwise it will be
considered as decimal value*/
for(i=1;i<=10;i++)
{
sum+=1/(float)i; //type casting int to float
Console.Write("i==>"+i);
Console.WriteLine("Sum==>"+sum);
}
}
}
So,
Casting into a smaller type may result in loss of data.
For example from above example if we try to convert the float type into int type,it may result in the loss of fractional part.
using system; //namespace
class Casting
{
public static void Main()
{
float sum;
int i;
sum=0.0F;
/*value initialized to a real variable must followed by F,otherwise it will be
considered as decimal value*/
for(i=1;i<=10;i++)
{
sum+=1/(float)i; //type casting int to float
Console.Write("i==>"+i);
Console.WriteLine("Sum==>"+sum);
}
}
}
So,
Casting into a smaller type may result in loss of data.
For example from above example if we try to convert the float type into int type,it may result in the loss of fractional part.
No comments:
Post a Comment