-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsquare._without_using_operators.cpp
More file actions
48 lines (44 loc) · 1001 Bytes
/
square._without_using_operators.cpp
File metadata and controls
48 lines (44 loc) · 1001 Bytes
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
// Calculatae Square of number without using multiplication(*), division(/), power function (pow()).
#include <bits/stdc++.h>
int calculateSquare(int num)
{
// Method 1
// int n = abs(num);
// int s = 0;
// for (int i=1; i<=n; i++) {
// s+=n;
// }
// return s;
// Method 2
// int square = 1;
// for (int i=1; i<=2; i++) {
// square = square * num;
// }
// return square;
// Method 3
// int s = 0;
// for (int i=1; i<=abs(num); i++) {
// s += abs(num);
// }
// return s;
// Method 4
int count = 0;
if (num<0) {
num = -(num);
}
for (int i=1; i<=num; i++) {
count += num;
}
return count;
// // Method 5: Bitwise operator
// if (num == 0) {
// return 0;
// }
// if (num < 0) {
// return 0;;
// }
// if (num & 1) {
// return (calculateSquare(num>>1)<<2)+ ((num>>1)<<2) + 1;
// }
// return calculateSquare(num >> 1) << 2;
}