Creating Love Calculators in different ways to trick my friends was one of my hobbies. Here i’ll explain one method of doing it using C++. I’ll explain the basic logic and you’ll do the decorations and modifications.

**How to Calculate**

**
**I’ll explain it with an example. Take 2 names

*Leo John*and

*Mary Mathew*. We’ll start by calculating the number of occurrences of each letters L, O, V, E, S in both names.

no. of occurrences L = 1 + 0 = 1

no. of occurrences O = 2 + 0 = 2

no. of occurrences V = 0 + 0 = 0

no. of occurrences E = 1 + 1 = 2

no. of occurrences S = 0 + 0 = 0

Now write down the numbers in order

1, 2, 0, 2, 0

Now we should add each adjacent numbers to get the next corresponding number. ie, the next number will be

1+2, 2+0, 0+2, 2+0 = 3, 2, 2, 2

Again next step,

3+2, 2+2, 2+2 = 5, 4, 4

5+4, 4+4 = 9, 8

Now we’ve got a number between 0 and 100. So that’s it! We’ve got the percentage of love, 98 %. In some cases we may get a number which adds up to get a 2 digit number. For example,

*VineeshKumar*and

*Shreya Ghoshal*

1, 1, 1, 3, 3

2, 2, 4, 6

4, 6, 10 (Oh no.. It wrong, Write it as 4, 6, 1, 0)

4, 6, 1, 0

10, 7, 1 (We change it to 1, 0, 7, 1)

1, 0, 7, 1

1, 7, 8

8, 15

8, 1, 5

9, 6

So we’ve got 96 %. We’ll now continue to our program

**Implementation**

**To implement the same in C++, First we add 2 functions.**

1. countLetter(char c, char *s)

Returns the number of occurrences of character c in string s.

2. long findNextNumber(long num)

Returns the next number in the series. for example, If we pass 123 in to it, it gives back 35, after adding the adjacent numbers. Here are the steps we did to calculate the ‘next number’ of 12681

- separate the digits of
*12681*and store it in an array. Thus we get an array*1 8 6 2 1* - Set newNumber = 0
- Our array is in the reverse order, so we loop from last digit to first digit.
- We check whether the sum of current number and next number is greater than 9, if so we multiply it by 100, else we multiply by 10 and then add the sum of 2 numbers to it.

- newNumber = 0
- newNumber = (0 * 10) + (1 + 2) = 3
- newNumber = (3 * 10) + (2 + 6) = 38
- newNumber = (38 * 100) + (6 + 8) = 3814 (multiplied by 100 because 6+8 is > 9)
- newNumber = (3814 * 10) + (8 + 1) = 38149

Now we’ll read 2 strings name1 and name2. Then we’ll calculate the first number by joining the counts of letters in ‘LOVES’ using the above logic. After getting the first number, we continue finding the next number until we get a number less than or equal to 100. and that’s it. We get the result.

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 |
#include<iostream> #include<ctype.h> using namespace std; int countLetter(char, char*); long findNextNumber(long); int main() { char name1[51], name2[51]; /* Read the names */ cout<<"Enter your name :"; cin.getline(name1,51); cout<<"Enter your lover's name :"; cin.getline(name2,51); /* Calculate the first number */ long num = countLetter('l',name1) + countLetter('l',name2); int count; count = countLetter('o',name1) + countLetter('o',name2); num = count>9 ? (num*100)+count : (num*10)+count; count = countLetter('v',name1) + countLetter('v',name2); num = count>9 ? (num*100)+count : (num*10)+count; count = countLetter('e',name1) + countLetter('e',name2); num = count>9 ? (num*100)+count : (num*10)+count; count = countLetter('s',name1) + countLetter('s',name2); num = count>9 ? (num*100)+count : (num*10)+count; /* loop until the number becomes less than or equal to 100 */ while(num>=100) { num = findNextNumber(num); } /* Print the result */ cout<<"nLove percentage = "<<num<<" %"; return 0; } /* Function to find the number occurences of given letter in the given string */ int countLetter(char c, char *str) { int count = 0; for(int i=0; str[i]!=''; ++i) { if(tolower(str[i])==c) count++; } return count; } /* Function to generate the next number in our love calculator logic */ long findNextNumber(long num) { int digits[10],count = 0; /* Separate the digits of given number and store it in a array */ while(num > 0) { digits[count] = num % 10; num /= 10; count++; } /* set new number = 0 */ long newNum = 0; /* Loop through the array and generate new number */ for(int i=count-1 ; i>0; --i) { if(digits[i] + digits[i-1] > 9) newNum = (newNum * 100) + (digits[i] + digits[i-1]); else newNum = (newNum * 10) + (digits[i] + digits[i-1]); } return newNum; } |

**A little words more..**

- One common error is case-sensitivity. The user may enter uppercase and lowercase letters. In our program we’ve converted all letters to lowercase before counting, and have done it in countLetter function.
- The number of letters is limited to 50. Try implementing dynamic allocation instead, so that user can enter any number of letters.
- One other error that may occur, only in case user tries to test our program, if count of any of the letter LOVES in both names is greater than 99 i.e., a 3 digit number, we should multiply the previous number by 1000 instead of 100 in lines 22 to 29.