C++ Typecasting. Math Library Functions.
Operator / Operands A = x + y
Typecasting What is the integral expression? What is a decimal expression? What is mixed expression? What will be the result of those expression?
Typecasting What is typecasting? What are the two types of typecasting? Examples…
Implicit Typecasting 3.4 / 2 = 3.4 / 2.0 = 1.7
Explicit Typecasting int x=3, y=2; double z; z = x/y; //integer division, z=1.0 z = double(x)/y; // z=1.5 What is the datatype of x after this operation?
Explicit Typecasting double(x) is equivalent to (double)x is equivalent to static_cast x
int number; cout << “Enter a number”; //User enters 10 cin >> number; What is stored in variable “number”? 10
int number; cout << “Enter a number”;//User enters 10.3 cin >> number; What is stored in variable “number”? 10
float number; cout << “Enter a number”; //User enters 10.3 cin >> number; What is stored in variable “number”? 10.3
float number; cout << “Enter a number”; //User enters 10 cin >> number; What is stored in variable “number”? 10.0
int num1=3; int num2=5; int product; product = num1 * num2; What is stored in variable “product”? 15
float num1=3.1; int num2=5; int product; product = num1 * num2; What is stored in variable “product”? 15
float num1=3.1; int num2=5; float product; product = num1 * num2; What is stored in variable “product”? 15.5
int num1=3; int num2=5; float product; product = num1 * num2; What is stored in variable “product”? 15.0
Integer Division ( / ) int/int int 10/3 = 30/5 = 5/6 = 9/8 = 22/5 =
Mod Operator ( %) int % int = int 10%3 = 30%5 = 5%6 = 9%8 = 22%5 =
int num1= 13; int num2= 4; int quotient; quotient = num1 /num2; What is stored in variable “quotient”? 3
int num1= 13; int num2= 4; float quotient; quotient = num1 /num2; What is stored in variable “quotient”? 3.0
int num1=13; int num2=4; float quotient; quotient = float(num1) /num2; What is stored in variable “quotient”? What is stored in variable “num1”?
int num1=13; int num2=4; float quotient; quotient = float(num1/num2); What is stored in variable “quotient”? 3.0
Character Typecasting char letter = ‘a’; cout<< int(letter); // What is displayed? int x=98; cout<<char(x); //What is displayed? 97 b
Character Typecasting char letter='a'; cout<< int(letter) << endl; letter= letter + 1; cout<< letter << endl; cout<< letter + 1 << endl; cout<< char (letter+1) << endl; cout<< int(letter) << endl; 97 b 99 c 98
Find an average of three numbers: int x, y, z; float average; average=(x+y+z)/3; average=(float(x)+y+z)/3.0; average=(x+y+z)/3.0;