Divisibility Test - Java Sample Program

//Program: Divisibility test by 3 and 9

import java.util.*;
public class DivisibilityTest
{
static Scanner console = new Scanner(System.in);

public static void main (String[] args)
{
int num;
int temp;
int sum;

System.out.print("Enter a positive integer: ");
num = console.nextInt();
System.out.println();
temp = num;
sum = 0;

do
{
sum = sum + num % 10; //extract the last digit
//and add it to sum
num = num / 10; //remove the last digit
}
while (num > 0);

System.out.println("The sum of the digits = " + sum);
if (sum % 3 == 0)
System.out.println(temp + " is divisible by 3");
else
System.out.println(temp + " is not divisible by 3");
if (sum % 9 == 0)
System.out.println(temp + " is divisible by 9");
else
System.out.println(temp + " is not divisible by 9");
}
}

//Sample Output: (In these sample runs, the user input is shaded.)
*****************************************
Sample Run 1:
Enter a positive integer: 27193257
The sum of the digits = 36
27193257 is divisible by 3
27193257 is divisible by 9
*****************************************
Sample Run 2:
Enter a positive integer: 609321
The sum of the digits = 21
609321 is divisible by 3
609321 is not divisible by 9
*****************************************
Sample Run 3:
Enter a positive integer: 161905102
The sum of the digits = 25
161905102 is not divisible by 3
161905102 is not divisible by 9
*****************************************

No comments: