DivisibleByNineEleven ESTANOCO

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

import java.util.

Scanner;

public class DivisibleByNineEleven_ESTANOCO {


public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
int number;
int digit1, digit2, digit3, digit4, digit5;
int sum;

System.out.println("Input any 5-digit integer number:");


number = keyboard.nextInt(); // example: 42351

digit1 = number / 10000; // 42351 / 10000 --> 4


digit2 = number % 10000 / 1000; // 42351 % 10000 --> 2351 / 1000 --> 2
digit3 = number % 1000 / 100; // 42351 % 1000 --> 351 / 100 --> 3
digit4 = number % 100 / 10; // 42351 % 100 --> 51 / 10 --> 5
digit5 = number % 10; // 42351 % 10 --> 1

// example: divisible by 3
if ((digit1 + digit2 + digit3 + digit4 + digit5) % 3 == 0) {
System.out.println(number + " is divisible by 3");
}

// Update the condition to check if a 5-digit number is divisible by 9


if ((digit1 + digit2 + digit3 + digit4 + digit5) % 9 == 0) {
System.out.println(number + " is divisible by 9");
}

// Update the condition to check if a 5-digit number is divisible by 11


if ((digit1 - digit2 + digit3 - digit4 + digit5) % 11 == 0) {
System.out.println(number + " is divisible by 11");
}

System.out.println("Done.");
}
}

You might also like