Tuesday, 9 February 2016

Love Calculator logic implementation in java


This is a java program which calculates the love percentage between two people.
The logic is it converts the letters in the name to its ASCII equivalent and then sums up all the letters and perform a modulus operation with 100 to get the result.




Example  :  If 2 names are Kete and Rade
                    ASCII value of K   =     75
                    ASCII value of E    =     69
                    ASCII value of T    =     84
                    ASCII value of E    =     69
                    ASCII value of R    =     72
                    ASCII value of A    =     65
                    ASCII value of D    =     68
                    ASCII value of E    =     69

                    Sum of  ASCII values of K+E+T+E + R + A + D + E =     581
                    now 581 % 100 = 81

                  So, the love % between Kete and  Rade= 81%


Download code

PROGRAM :

        package mycodingcorner;

 import java.util.Scanner;

 public class LoveCalculator {

  public static void main(String[] args) {

   String name1, name2, concat;
   Scanner scan = new Scanner(System.in);

   System.out.print("\nEnter your name :\t");
   while (!scan.hasNext("[A-Za-z]+")) {
    System.out.println("Enter alphabets only");
    scan.next();
   }
   name1 = scan.next().toUpperCase();
   System.out.print("\nEnter your crush name :\t");
   while (!scan.hasNext("[A-Za-z]+")) {
    System.out.println("Enter alphabets only");
    scan.next();
   }
   name2 = scan.next().toUpperCase();
   concat = name1.concat(name2);
   int sum = 0;
   for (int i = 0; i < concat.length(); i++) {
    char character = concat.charAt(i);
    int ascii = (int) character;
    sum += ascii;
   }
   System.out.println("The love between " + name1 + " and " + name2 + " is " + sum % 100 + "%");
   scan.close();

  }
 }

 
Download code

OUTPUT :

Java Program - Love Calculator