Comparing sum of string's digit by splitting string in 2 halfs

import java.util.Scanner;


public class Lottry {
static boolean bLot = false;


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
String sNum = sc.next();
if(blottry(sNum)){
System.out.println("Won the lottry");
}else{

System.out.println("Not won the lottry");
}
sc.close();
}

static boolean blottry(String s){
int sum1 = 0;
int sum2 = 0;
char[]cLot = s.toCharArray();
System.out.println("Lenght of String digit::"+ cLot.length);
if(cLot.length>=4 && cLot.length%2==0){

for(int i = 0; i<cLot.length/2; i++){
sum1 = sum1 + Character.getNumericValue(cLot[i]);
}
System.out.println("Sum1:::::::::::::::"+ sum1);
for(int i=cLot.length-1; i >= cLot.length/2; i--){
sum2 = sum2 + Character.getNumericValue(cLot[i]);

}
System.out.println("Sum2:::::::::::::::"+ sum2);
}
if(sum1==sum2){
bLot = true;
}
return bLot;
}
}

Comments

Popular posts from this blog

Parsing xml node tags using documentbuilderfactory

How to generate string with given range.