Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package src;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Carry {
- public static void main(String[] args) throws IOException {
- Carry carry = new Carry();
- BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
- String line;
- while ((line = input.readLine()) != null && line.length() != 0) {
- String[] inputNumbers = line.split(" ");
- int first = Integer.parseInt(inputNumbers[0]);
- int second = Integer.parseInt(inputNumbers[1]);
- int carryCount = carry.computeCarry(first, second);
- if (carryCount == 0) {
- System.out.println("No carry operation.");
- }
- else {
- String s = (carryCount == 1) ? "" : "s";
- System.out.println(carryCount + " carry operation" + s + ".");
- }
- }
- }
- /*
- * Computes and prints the number of 'carry' operations when adding two
- * positive integers of <10 digits.
- */
- private int computeCarry(int first, int second) {
- // ensure first is > second
- if (second > first) {
- int temp = second;
- second = first;
- first = temp;
- }
- int carryCount = 0;
- int carryOver = 0;
- for (int i = 1; i < (Integer.toString(second).length()); i ++) {
- int carry = (int) ((first % Math.pow(10, i)) + (second % Math.pow(10, i))) + carryOver;
- if (carry >= 10) {
- carryCount++;
- carryOver = carry % 10;
- }
- }
- return carryCount;
- }
- }
Add Comment
Please, Sign In to add comment