lundi 13 juin 2016

How to find sum of first three digits

I'm new in Java and I try to find the sum of first three digits in int number. I have a number 123456 I want to find the sum of first three digits 123 and then find sum of last three digits 456 and then compare it. I can't understand how to do this. I code next:

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the number:");
        int number = scanner.nextInt();

        int lenght = (int) Math.ceil(Math.log10(number));

        for(int i = 1; i<=3; i++){
            System.out.println(i);
        }
        System.out.println(lenght);
        System.out.println(number);

    }

I think that I shoud to use for loop, but I'm not sure. I know how to count the lenght of number. But how to how to find the sum of the first three numbers and last three?

Aucun commentaire:

Enregistrer un commentaire