Create a method for summarizing digits in an int. Help please!

Just need help working through programming this. Not the answer. Thank

Page 1 of 1

2 Replies - 7336 Views - Last Post: 11 November 2010 - 12:10 PM Rate Topic: -----

#1 VonWheeler  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 14
  • Joined: 14-October 10

Create a method for summarizing digits in an int. Help please!

Posted 11 November 2010 - 11:18 AM

//Exercise5_2.javac: Create a method for summarizing digits in an int
/*
Page 188
5.2 (summing the digits in an integer)
Write a method that computes the sum of the digits in an integer.
	Use the following method header:
	
		public static int numDigits(long n)
		
	For example, sumDigits(234) returns 2 + 3 + 4 = 9.
	
Hint
Use the % operator to extract digits, and
	use the / operator to remove the extracted digit.
	For instance, to extract 4 from 234, use 234 % 10 (= 4).
	to remove 4 from 234, use 234 / 10 (= 23)
	Use a loop to repeatedly extract and remove the digit until
		all the digits are extracted
	Write a test program that
	prompts the user to enter an integer and displays the sum off all its digits.
*/
import java.util.Scanner;

public class Exercise5_2 {
	public static void main(String[] args) {
	
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a positive integer: ");
			int integer = input.nextInt();
			/// call method sumDigits and then display the result
			
		} // end of main
		
		public static int sumDigits(long n) {
			int temp = (int)Math.abs(n);  // temp value
			int sum = 0; 
			 	// the sum of the digits
 				// while (loop until all the digits are extracted)
				while (temp > 0)
				// subtract a digit (%)
				
				// add the extracted digit into sum
				
				// remove the extracted digit (/)
			// }
			// return the sum of the digits
			
			return sum;
		}  // end of sumDigits
	} // end of Exercise 5_2

/* Output
	 The sum of digits for 234 is 9
*/



Okay I'm doing this for my class and I'm not sure how to put the code in for how the directions say. Input variables and right order to put them and what not. Anything is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Create a method for summarizing digits in an int. Help please!

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4187
  • View blog
  • Posts: 11,847
  • Joined: 18-April 07

Re: Create a method for summarizing digits in an int. Help please!

Posted 11 November 2010 - 11:39 AM

Well it is not really that difficult.. the instructions they supply say it all. You just have to plug it into your loop and then call sumDigits from main...

public static void main(String[] args) {
	
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a positive integer: ");
    int integer = input.nextInt();
    /// call method sumDigits and then display the result
    System.out.println("Sum is: " + sumDigits(integer));
} // end of main
		
public static int sumDigits(long n) {
    int temp = (int)Math.abs(n);  // temp value
    int sum = 0; 
    // the sum of the digits
    // while (loop until all the digits are extracted)

    while (temp > 0) {
        // Extract off a digit, add it to sum
        sum += temp % 10;

        // Now set temp to the value minus the extracted digit
        temp = temp / 10;
    }
    // return the sum of the digits
			
    return sum;
}  // end of sumDigits




The code above says it all. Notice the two pieces we put in the while loop. The % extracts the number and adds it to sum. The / then returns the value minus the last digit. This is because it is integer division and will return only whole integers. In other words 6 / 5 is not 1.2... it is 1. A whole integer.

I wouldn't have shown the answer, but the process of showing you is only two lines and those lines were given to you in the example code.

Enjoy! :)

This post has been edited by Martyr2: 11 November 2010 - 11:43 AM

Was This Post Helpful? 0
  • +
  • -

#3 VonWheeler  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 14
  • Joined: 14-October 10

Re: Create a method for summarizing digits in an int. Help please!

Posted 11 November 2010 - 12:10 PM

Your reply was def. much appreciated. I just couldn't figure out how to put the codes you have me but now I do. thank you so much. :bananaman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1