JAVA&C simple algorithm