Modify the main method that tests your sumDigits method to do the following: input an identification number (a positive integer), then determine if the sum of the digits in the identification number is divisible by 7 (use your sumDigits method but don't change it -- the only changes should be in main). If the sum is not divisible by 7 print a message indicating the id number is in error; otherwise print an ok message. (FYI: If the sum is divisible by 7)
I know to check this soultion i would use:
(sum % 7 == 0)
import java.util.Scanner;
// *******************************************************************
// DigitPlay.java
//
// Finds the number of digits in a positive integer.
// *******************************************************************
public class DigitPlay
{
public static void main (String[] args)
{
Scanner conIn = new Scanner(System.in);
int num; //a number
System.out.println ();
System.out.print ("Please enter a positive integer: ");
if (conIn.hasNextInt())
num = conIn.nextInt();
else
{
System.out.println("Error: you must enter an integer.");
System.out.println("Terminating program.");
return;
}
System.out.println();
if (num <= 0)
System.out.println ( num + " isn't positive -- start over!!");
else
{
// Call numDigits to find the number of digits in the number
// Print the number returned from numDigits
System.out.println ("\nThe number " + num + " contains " +
+ numDigits(num) + " digits.");
System.out.println ();
System.out.println ("\nThe sum " + num + " contains " +
+ sumDigits(num));
}
}
// -----------------------------------------------------------
// Recursively counts the digits in a positive integer
// -----------------------------------------------------------
public static int numDigits(int num)
{
if (num < 10)
return (1);
else
return (1 + numDigits(num/10));
}
public static int sumDigits(int num)
{
if (num ==1)
return (1);
else
return (sumDigits(num-1)+num);
}
}

New Topic/Question
Reply



MultiQuote



|