-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
67 lines (59 loc) · 1.69 KB
/
Copy pathcode.cpp
File metadata and controls
67 lines (59 loc) · 1.69 KB
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
#include <bits/stdc++.h>
using namespace std;
int sumOfDoubleEvenPlace(int number);
int getDigit(int number);
int main(void)
{
int test1 = 89744563;
int test2 = 98756421;
int result1 = 0;
int result2 = 0;
result1 = sumOfDoubleEvenPlace(test1);
cout << "Expected Value: 23 Value: " << result1 << endl;
result2 = sumOfDoubleEvenPlace(test2);
cout << "Expected Value: 21 Value: " << result2 << endl;
}
/*
* Function returns the sum of the even placed
* digits(after being doubled) starting from the left.
* Note that the algorithm starts counting from 1 not 0. Therefore,
* given the number 1234, 4 is the first digit from the left.
* So the even placed digits are 3 and 1 and the odd place digits are
* 4 and 2 from the left.
*/
int sumOfDoubleEvenPlace(int number) {
int sum = 0;
int digit;
//Remove first odd digit
number = number / 10;
while (number > 0) {
//Grab even placed digit
digit = (number % 10);
//Double the digit and pass it to getDigit,
//Add result to sum
sum += getDigit(digit*2);
//Remove current even digit and the next odd digit.
number = number/100;
}
return sum;
}
/* getDigit returns the sum of the digits in
* a 1 or 2 digit number.
* if number is < 10,
* then we return the number.
* else we return the sum of the digits in the 2 digit
* number.
* For example:
* 1 would return 1
* 11 would return 2
* 18 would return 9
*/
int getDigit(int number) {
int sum = 0;
if (number < 10) {
sum = number;
} else {
sum = number%10 + number/10;
}
return sum;
}