-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperators.cpp
393 lines (293 loc) · 8.61 KB
/
operators.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*Lecture 3.3: Operators in C++ */
//Pre and Post Incrementor example
#include<iostream>
using namespace std;
int main() {
/* ############################# OPERATOR 1. INCREMENT AND DECREMENT OPERATOR ########################### */
/* Example : 1 */
int x = 1;
//1 + //3
x = x++ + ++x;
cout<<x<<endl; //output: 4
/* Example : 2 */
int i = 1;
int j = 3;
int sum;
//1 //2 //1 //4 //3 //5
sum = i + j + i++ + j++ + ++i + ++j;
cout<<i<<" "<<j<<" "<<" "<<sum<<endl; //output: 1=3 j=5 sum=16
/* Example : 3 */
int k = 0;
//0 //0 //1 //1
k = k++ - --k + ++k - k--;
//1
cout<<k<<endl; //output: 0
/* Example : 4 */
int m = 2;
int n = 4;
int o = 8;
int total;
//2 //4 //8
total = m-- - n-- - o--;
cout<<m<<endl; //1
cout<<n<<endl; //3
cout<<o<<endl; //7
cout<<total<<endl; //1 3 7 -10
/* Example : 5 */
int e = 20;
int f = 30;
int sum;
//20 //19 //29 //30 //19 //30 //20 //29
sum = e-- - e++ + --f - ++f + --e - f-- + ++e - f++;
cout<<e<<endl; //20
cout<<f<<endl; //30
cout<<sum<<endl; //-20
/* ############################# OPERATOR 2. RELATIONAL OPERATOR ########################### */
/* Example 1: == Equal Too */
int a = 6;
int b= 6;
cout <<boolalpha;
cout << (a == b)<<endl; //output: true, Gives true if both operands have equal value
/* Example 2: != Not Equal Too */
int x = 6;
int y= 5;
cout <<boolalpha;
cout << (x != y)<<endl; //output: true, gives true if both operands are not equal
/* Example 3: > Greater than */
int m = 15;
int n = 12;
cout <<boolalpha;
cout << (m > n)<<endl; //output: true , gives true if left operands is more than right operand
/* Example 4: < Less than */
int o = 14;
int p = 18;
cout <<boolalpha;
cout << (o < p)<<endl; //output: true , gives true if left operands is less than right operand
/* Example 5: >= Greater than and Equal To */
int e = 21;
int f = 13;
cout <<boolalpha;
cout << (e >= f)<<endl; //output: true , gives true if left operands is more than or equal to right operand
int l = 21;
int m = 21;
cout <<boolalpha;
cout << (l >= m)<<endl; //output: true , gives true if left operands is more than or equal to right operand
/* Example 6: <= Less than and Equal To */
int q = 8;
int r = 13;
cout <<boolalpha;
cout << (q <= r)<<endl; //output: true , gives true if left operands is less than or equal to right operand
int s = 8;
int t = 8;
cout <<boolalpha;
cout << (s <= t)<<endl; //output: true , gives true if left operands is less than or equal to right operand
//Program to input a number n and tell whether it is equal to, less than or more than 10
int num;
cout<<"Enter A Number: ";
cin>>num;
if(num==10) {
cout<<"This number are Equal to 10"<<endl;
}
else if(num<10) {
cout<<"This number Less than 10"<<endl;
}
else if (num>10) {
cout<<"This number Greater than 10"<<endl;
} else {
cout<<"Number Not Exist"<<endl;
}
/* Output:
Enter A Number: 1
This number Less than 10
Enter A Number: 12
This number Greater than 10
Enter A Number: 10
This number are Equal to 10
*/
/* ############################# OPERATOR 3. LOGICAL OPERATOR ########################### */
//LOGICAL OPERATOR : used to connect multiple conditions/ expressions together or to reverse logical value
/* 1. && operator: AND gives us true if both operand are true else false
*/
int u = 0;
int v = 23;
cout <<boolalpha;
cout << ((u == 0) && (u > v))<<endl; //output: false , AND gives us true if both operand are true else false
int g = 32;
int h = 12;
cout <<boolalpha;
cout << ((g == 32) && (g > h))<<endl; //output: true , && AND gives us true if both operand are true else false
/* 2. || operator: OR give us true if at least one of the operands are true */
int q = 65;
int r = 88;
cout <<boolalpha;
cout << ((q == 65) || (q > r))<<endl; //output: true , || OR give us true if at least one of the operands are true
/* 3. ! operator: Not gives the opposite logical value of the operand.True becames false and false became true */
int w = 89;
int x = 89;
cout <<boolalpha;
cout << !(w == x)<<endl; //output: false , ! Not gives the opposite logical value of the operand.True becames false and false became true
/* T-->1
F--> 0
*/
//Write a program to output whether a number is divisible by both 2 and 3 or divisible by only one of them
int number;
cout<<"Enter The Number To Check: ";
cin>>number;
if(number%2 == 0 && number%3 == 0) {
cout<<"Divisible by Both Numbers"<<endl;
} else if (number%2==0) {
cout<<"Divisible by only 2"<<endl;
} else if (number%3==0) {
cout<<"Divisible by only 3"<<endl;
}
else {
cout<<"Divisible by None"<<endl;
}
/* Output:
Enter The Number To Check: 2
Divisible by only 2
Enter The Number To Check: 87
Divisible by only 3
Enter The Number To Check: 6
Divisible by Both Numbers
Enter The Number To Check: 78784654658445454544548787848455
Divisible by None
*/
/* ############################# OPERATOR 4. BITWISE OPERATOR ########################### */
//operate on bits and perform bit by bit operations
/*1. & AND OPERATOR
0101
& 0110
_____
0100
*/
/* 2. | OR OPERATOR
0101
| 0110
_____
0111
*/
/* 3. ^ XOR OPERATOR
0101
^ 0110
_____
0011
*/
/* 4. ~ Ones complement OPERATOR
~ 0101
_____
1010
*/
/* 5. << Left shift OPERATOR
4<<1
(0100) //4 ka binary number
=1000 //Ans 8 in decimal
formula: a<<n -----> a*2^n
*/
/* 6. << Right shift OPERATOR
4>>1
(0100) //4 ka binary number
=0010 //Ans 2 in decimal
formula: a>>n -----> a/2^n
*/
/* ############################# OPERATOR 5. ASSIGNMENT OPERATOR ########################### */
/* = , Assign value of right operand to left operand */
int i = 3;
int j;
j = i;
cout<<j; //output: 3
/* += , Assign sum of two operands to left operand */
int x = 6323;
int j = 323;
x+=j; //short form of a=a+b
cout<<x; //output: 6646
/* -= , Assign difference of two operands to left operand */
int a = 76;
int b = 34;
int result;
result = a-=b; //short form of a=a-b
cout<<result; //output: 42
/* *= , Assign product of two operands to left operand */
cout<<"Assignment Operators Example of *= operand"<<endl;
int m;
cout<<"Enter the First Value: ";
cin>>m;
int n;
cout<<"Enter the Second Value: ";
cin>>n;
int sum;
sum = m*=n; //short form of a=a*b
cout<<sum;
/*Output:
Enter the First Value:
12
Enter the Second Value:
32
384
*/
/* /= , Assign the quotient of two operands to left operand */
cout<<"Assignment Operators Example of /= operand"<<endl;
float s;
cout<<"Enter the First Value: ";
cin>>s;
float t;
cout<<"Enter the Second Value: ";
cin>>t;
t= s/=t; //short form of a=a/b
cout<<t;
/*
Enter the First Value: 43
Enter the Second Value: 8
5.375
*/
/* ############################# OPERATOR 6. MISCELLANEOUS OPERATOR ########################### */
/* 1. sizeof() operator return the size of variable */
int x;
cout<<sizeof(x); //output: 4
/* 2. condition?a:b operator/ternary operator return the value of a if condition is true or else value of b */
int a;
cout<<"Enter the First Value: ";
cin>>a;
int b;
cout<<"Enter the Second Value: ";
cin>>b;
int c;
if(c= a>b? a-b: b-a) {
cout << "Value of c is " << c << endl;
}
/* Output:
Enter the First Value: 56
Enter the Second Value: 76
Value of c is 20
*/
/* 3. cast operator, convert one data type to another data type*/
char ch = 'a';
cout<<int(ch); //Output: 97, ye humko a ki ascii value de dega which is 97
/* 4. comma(,) operator, causes a sequence of operations to be performed */
/* isme kya hota hai ki jo last value hoti hai, ya fhir operations ki jo last value aati hai voo variable me assign
ho jaati hai, below example me 12 a variable me assign ho jaayega.
*/
int a = (2,4,12);
cout<<a; //output: 12
/* 5. & reference operator, return the address of the variable */
int x = 12;
cout<<&(x); //Output: 0x61ff0c, ye humko x variable ka memory location ka address return karega
/* 6. * pointer operator, pointer to a variable */
int m;
int *ptr;
int result;
m = 4000;
//take the address of m by using the pointer
ptr = &m;
// take the result available at ptr
result = *ptr;
cout << "Value of M is : " << m << endl; //output: 4000
cout << "Value of ptr is : " << ptr << endl; //output: Value of ptr :0x61ff08
cout << "Value of Result is: " << result << endl; //output: 4000
/* Final Output:
Value of M is : 4000
Value of ptr is : 0x61ff04
Value of Result is: 4000
*/
return 0;
}