So, if we
try to perform some Arithmetic operation on different data types, i.e. int,
float and double etc then C/C++ calculates the result of such type of
Arithmetic expression without giving any error.
Type Conversion Example
#include
<iostream.h>
#include
<conio.h>
void
main(void)
{
clrscr();
int count=7;
float weight=200.5;
double totalweight = count * weight;
cout<<"Total weight calculated
is "<<totalweight;
getch();
}
Output : Total weight calculated is
1403.5
Temperature
Conversion Program
#include <conio.h>
void main(void)
{
clrscr();
int fah;
cout<<"Enter
Temperature in Fahrenheit ";
cin>>fah;
int cel = (fah - 32)
* 5/9;
cout<<"Equivalent
temperature in Celsius is "<<cel;
getch();
}
Type Casts
Cast is a way through which we change the type of the variable during
the execution of the program for a limited time, because variables previously
defined type can not calculate the values correctly due to its low range.
Example of Casts:
#include <conio.h>
void main(void)
{
clrscr();
int test=25000;
//range is -32,768 to +32,767
test = (test *
10)/10;
cout<<"Result
is "<<test<<endl;
test = 25000;
test =
(long(test)*10)/10;
cout<<"Result
now is "<<test;
getch();
}
No comments:
Post a Comment