Download presentation
Presentation is loading. Please wait.
1
C++ Typecasting. Math Library Functions.
2
Operator / Operands A = x + y
3
Typecasting What is the integral expression? What is a decimal expression? What is mixed expression? What will be the result of those expression?
4
Typecasting What is typecasting? What are the two types of typecasting? Examples…
5
Implicit Typecasting 3.4 / 2 = 3.4 / 2.0 = 1.7
6
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?
7
Explicit Typecasting double(x) is equivalent to (double)x is equivalent to static_cast x
8
int number; cout << “Enter a number”; //User enters 10 cin >> number; What is stored in variable “number”? 10
9
int number; cout << “Enter a number”;//User enters 10.3 cin >> number; What is stored in variable “number”? 10
10
float number; cout << “Enter a number”; //User enters 10.3 cin >> number; What is stored in variable “number”? 10.3
11
float number; cout << “Enter a number”; //User enters 10 cin >> number; What is stored in variable “number”? 10.0
12
int num1=3; int num2=5; int product; product = num1 * num2; What is stored in variable “product”? 15
13
float num1=3.1; int num2=5; int product; product = num1 * num2; What is stored in variable “product”? 15
14
float num1=3.1; int num2=5; float product; product = num1 * num2; What is stored in variable “product”? 15.5
15
int num1=3; int num2=5; float product; product = num1 * num2; What is stored in variable “product”? 15.0
16
Integer Division ( / ) int/int int 10/3 = 30/5 = 5/6 = 9/8 = 22/5 =
17
Mod Operator ( %) int % int = int 10%3 = 30%5 = 5%6 = 9%8 = 22%5 =
18
int num1= 13; int num2= 4; int quotient; quotient = num1 /num2; What is stored in variable “quotient”? 3
19
int num1= 13; int num2= 4; float quotient; quotient = num1 /num2; What is stored in variable “quotient”? 3.0
20
int num1=13; int num2=4; float quotient; quotient = float(num1) /num2; What is stored in variable “quotient”? What is stored in variable “num1”? 3.25 13
21
int num1=13; int num2=4; float quotient; quotient = float(num1/num2); What is stored in variable “quotient”? 3.0
22
Character Typecasting char letter = ‘a’; cout<< int(letter); // What is displayed? int x=98; cout<<char(x); //What is displayed? 97 b
23
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
24
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;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.