-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatatypes.cpp
75 lines (69 loc) · 1.51 KB
/
datatypes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
Problem statement
Data type refers to the type of value a variable has and the way the computer interprets it.
Each data type has a different size. You’ve studied 5 different data types and the sizes of the data types:
Integer: 4 bytes
Long: 8 bytes
Float: 4 bytes
Double: 8 bytes
Character: 1 byte
You’re given a data type. Print its size in bytes.
Example :
Input: Long
Output: 8
Explanation: The size of a Long variable is given as 8 bytes.
Detailed explanation ( Input/output format, Notes, Images )
Sample Input 1:
Long
Sample Output 1:
8
Explanation of sample input 1 :
The size of a Long variable is given as 8 bytes.
Sample Input 2:
Float
Sample Output 2:
4
Explanation of sample input 2 :
The size of a Float variable is given as 4 bytes.
Expected time complexity :
The expected time complexity is O(1).
Constraints :
‘type’ is one of the data types given above.
Time limit: 1 second
*/
#include<iostream>
using namespace std;
int dataTypes(string type) {
if(type == "Integer"){
return 4;
}
else if(type == "Long"){
return 8;
}
else if(type == "Float"){
return 4;
}
else if(type == "Double"){
return 8;
}
else if(type == "Character"){
return 1;
}
else{
return -1;
}
}
int main()
{
string datatype;
cout<<"enter the type:"<<endl;
cin>>datatype;
int size = dataTypes(datatype);
if(size != -1){
cout<<size<<endl;
}
else{
cout<<"invalid input"<<endl;
}
return 0;
}